Go back to Richel Bilderbeek's homepage.

Go back to Richel Bilderbeek's C++ page.

 

 

 

 

 

(C++) QtExample20

 

QtQt CreatorLubuntuUbuntu

 

This Qt example shows how to develop a sketch widget.

 

Technical facts

 

Application type(s)

Operating system(s) or programming environment(s)

IDE(s):

Project type:

C++ standard:

Compiler(s):

Libraries used:

 

 

 

 

 

Qt project file: ./CppQtExample20/CppQtExample20.pro

 

exists(../../DesktopApplication.pri) {
  include(../../DesktopApplication.pri)
}
!exists(../../DesktopApplication.pri) {
  QT += core gui
  greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
  TEMPLATE = app

  CONFIG(debug, debug|release) {
    message(Debug mode)
  }

  CONFIG(release, debug|release) {
    message(Release mode)
    DEFINES += NDEBUG NTRACE_BILDERBIKKEL
  }

  QMAKE_CXXFLAGS += -std=c++11 -Wall -Wextra

  unix {
    QMAKE_CXXFLAGS += -Werror
  }
}

exists(../../Libraries/Boost.pri) {
  include(../../Libraries/Boost.pri)
}
!exists(../../Libraries/Boost.pri) {
  win32 {
    INCLUDEPATH += \
      ../../../Projects/Libraries/boost_1_55_0
  }
}

SOURCES += \
  main.cpp \
  sketchdialog.cpp \
  sketchwidget.cpp

HEADERS  += \
  sketchdialog.h \
  sketchwidget.h
FORMS += sketchdialog.ui

#CONFIG += mobility
#MOBILITY =
#symbian {
#    TARGET.UID3 = 0xe83b2efd
#    # TARGET.CAPABILITY +=
#    TARGET.EPOCSTACKSIZE = 0x14000
#    TARGET.EPOCHEAPSIZE = 0x020000 0x800000
#}

 

 

 

 

 

./CppQtExample20/main.cpp

 

#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Weffc++"
#pragma GCC diagnostic ignored "-Wunused-local-typedefs"
#include <QApplication>
#include "sketchdialog.h"
#pragma GCC diagnostic pop

int main(int argc, char *argv[])
{
  QApplication a(argc, argv);
  SketchDialog w;
  w.show();
  return a.exec();
}

 

 

 

 

 

./CppQtExample20/sketchdialog.h

 

#ifndef SKETCHDIALOG_H
#define SKETCHDIALOG_H

#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Weffc++"
#pragma GCC diagnostic ignored "-Wunused-local-typedefs"
#include <QDialog>
#pragma GCC diagnostic pop

namespace Ui {
  class SketchDialog;
}

class SketchDialog : public QDialog
{
  Q_OBJECT

public:
  explicit SketchDialog(QWidget *parent = 0);
  SketchDialog(const SketchDialog&) = delete;
  SketchDialog& operator=(const SketchDialog&) = delete;
  ~SketchDialog();

private:
  Ui::SketchDialog *ui;

private slots:
    void on_button_clear_clicked();
};

#endif // SKETCHDIALOG_H

 

 

 

 

 

./CppQtExample20/sketchdialog.cpp

 

#include "sketchdialog.h"

#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Weffc++"
#pragma GCC diagnostic ignored "-Wunused-local-typedefs"
#include "ui_sketchdialog.h"
#pragma GCC diagnostic pop

SketchDialog::SketchDialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::SketchDialog)
{
  ui->setupUi(this);
}

SketchDialog::~SketchDialog()
{
  delete ui;
}

void SketchDialog::on_button_clear_clicked()
{
  ui->sketchwidget->clear();
}

 

 

 

 

 

./CppQtExample20/sketchwidget.h

 

#ifndef SKETCHWIDGET_H
#define SKETCHWIDGET_H

#include <vector>

#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Weffc++"
#pragma GCC diagnostic ignored "-Wunused-local-typedefs"
#include <QWidget>
#pragma GCC diagnostic pop

class SketchWidget : public QWidget
{
  Q_OBJECT
public:
  explicit SketchWidget(QWidget *parent = 0);
  void clear();

protected:
  void mouseMoveEvent(QMouseEvent *);
  void mousePressEvent(QMouseEvent *);
  void paintEvent(QPaintEvent *);

private:
  std::vector<std::vector<QPoint>> m_points;
};

#endif // SKETCHWIDGET_H

 

 

 

 

 

./CppQtExample20/sketchwidget.cpp

 

#include "sketchwidget.h"

#include <cassert>

#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Weffc++"
#pragma GCC diagnostic ignored "-Wunused-local-typedefs"
#include <QMouseEvent>
#include <QPainter>
#pragma GCC diagnostic pop

SketchWidget::SketchWidget(QWidget *parent)
  : QWidget(parent),
    m_points{}
{
  this->setMouseTracking(true);
}

void SketchWidget::clear()
{
  m_points.clear();
  this->repaint();
}

void SketchWidget::mouseMoveEvent(QMouseEvent * e)
{
  if (e->buttons() & Qt::LeftButton)
  {
    m_points.back().push_back(e->pos());
    this->repaint();
  }
}

void SketchWidget::mousePressEvent(QMouseEvent * e)
{
  if (e->buttons() & Qt::LeftButton)
  {
    m_points.push_back(std::vector<QPoint>());
    m_points.back().push_back(e->pos());
  }
}

void SketchWidget::paintEvent(QPaintEvent *)
{
  QPainter painter(this);
  {
    QPen pen = painter.pen();
    pen.setCapStyle(Qt::RoundCap);
    pen.setWidth(10);
    painter.setPen(pen);
  }

  const int n_lines = static_cast<int>(m_points.size());
  for (int line=0; line!=n_lines; ++line)
  {
    const std::vector<QPoint>& points = m_points[line];
    const int n_points = static_cast<int>(points.size());
    for (int point=1; point!=n_points; ++point)
    {
      painter.drawLine(points[point-1],points[point]);
    }
  }
}

 

 

 

 

 

Go back to Richel Bilderbeek's C++ page.

Go back to Richel Bilderbeek's homepage.

 

Valid XHTML 1.0 Strict

This page has been created by the tool CodeToHtml