Go back to Richel Bilderbeek's homepage.

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

 

 

 

 

 

(C++) QtExample25

 

QtQt CreatorLubuntuUbuntu

 

Qt example 25: a transparent widget shows a transparent 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: ./CppQtExample25/CppQtExample25.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 \
  sketchwidget.cpp \
  transparentsketchdialog.cpp
HEADERS  += \
  sketchwidget.h \
  transparentsketchdialog.h
FORMS    += \
  transparentsketchdialog.ui

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

 

 

 

 

 

./CppQtExample25/main.cpp

 

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

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

 

 

 

 

 

./CppQtExample25/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();
  virtual ~SketchWidget() {}

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

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

signals:

public slots:

};

#endif // SKETCHWIDGET_H

 

 

 

 

 

./CppQtExample25/sketchwidget.cpp

 

#include "sketchwidget.h"

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

#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]);
    }
  }
}

 

 

 

 

 

./CppQtExample25/transparentsketchdialog.h

 

#ifndef TRANSPARENTSKETCHDIALOG_H
#define TRANSPARENTSKETCHDIALOG_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 TransparentSketchDialog;
}

class TransparentSketchDialog : public QDialog
{
  Q_OBJECT

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

protected:
  void paintEvent(QPaintEvent *);

private:
  Ui::TransparentSketchDialog *ui;
  QPixmap m_pixmap;
};

#endif // TRANSPARENTSKETCHDIALOG_H

 

 

 

 

 

./CppQtExample25/transparentsketchdialog.cpp

 

#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Weffc++"
#pragma GCC diagnostic ignored "-Wunused-local-typedefs"
#include <QDesktopWidget>
#include <QPainter>
#include <QPixmap>
#include "transparentsketchdialog.h"
#include "ui_transparentsketchdialog.h"
#pragma GCC diagnostic pop

TransparentSketchDialog::TransparentSketchDialog(QWidget *parent)
  : QDialog(parent),
    ui(new Ui::TransparentSketchDialog),
    m_pixmap(QPixmap::grabWindow(QApplication::desktop()->winId()))
{
  ui->setupUi(this);
}

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

void TransparentSketchDialog::paintEvent(QPaintEvent *)
{
  QPainter painter(this);
  painter.drawPixmap(
    this->rect(),
    m_pixmap,
    this->geometry()
  );
}

 

 

 

 

 

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