Go back to Richel Bilderbeek's homepage.

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

 

 

 

 

 

(C++) QtExample31

 

QtQt CreatorLubuntu

 

Qt example 31: moveable thin lines is a Qt example to create some lines that are moveable. Note that because the lines are one pixel thin, it is difficult to really move them! QtExample32 solves this problem.

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: ./CppQtExample31/CppQtExample31.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 += \
    qtmain.cpp \
    dialog.cpp \
    qtarrowswidget.cpp \
    qtarrowitem.cpp

HEADERS += \
    dialog.h \
    qtarrowswidget.h \
    qtarrowitem.h

FORMS += dialog.ui

 

 

 

 

 

./CppQtExample31/dialog.h

 

#ifndef DIALOG_H
#define DIALOG_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 Dialog;
}

class Dialog : public QDialog
{
  Q_OBJECT
    
public:
  explicit Dialog(QWidget *parent = 0);
  Dialog(const Dialog&) = delete;
  Dialog& operator=(const Dialog&) = delete;
  ~Dialog() noexcept;
    
private:
  Ui::Dialog *ui;
};

#endif // DIALOG_H

 

 

 

 

 

./CppQtExample31/dialog.cpp

 

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

Dialog::Dialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::Dialog)
{
  ui->setupUi(this);
  ui->layout->addWidget(new QtArrowsWidget);
}

Dialog::~Dialog() noexcept
{
  delete ui;
}

 

 

 

 

 

./CppQtExample31/qtarrowitem.h

 

#ifndef QTTEXTPOSITIONITEM_H
#define QTTEXTPOSITIONITEM_H

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

struct QtArrowItem : public QGraphicsLineItem
{
  QtArrowItem(
    const double x1,
    const double y1,
    const bool tail,
    const double x2,
    const double y2,
    const bool head,
    QGraphicsItem *parent = 0);

  protected:
  ///Paint a QtTextPositionItem
  void paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidget *);

  ///The rectangle that determines the edges of the item
  QRectF boundingRect() const;

  ///Show arrow at head
  const bool m_head;

  ///Show arrow at tail
  const bool m_tail;

};

#endif // QTTEXTPOSITIONITEM_H

 

 

 

 

 

./CppQtExample31/qtarrowitem.cpp

 

#include <iostream>

#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Weffc++"
#pragma GCC diagnostic ignored "-Wunused-local-typedefs"
#include <QCursor>
#include <QFont>
#include <QGraphicsSceneMouseEvent>
#include <QPainter>
#include "qtarrowitem.h"
#pragma GCC diagnostic pop

QtArrowItem::QtArrowItem(
  const double x1,
  const double y1,
  const bool tail,
  const double x2,
  const double y2,
  const bool head,
  QGraphicsItem *parent)
  : QGraphicsLineItem(x1,y1,x2,y2,parent),
    m_head(head),
    m_tail(tail)
{
  this->setFlags(QGraphicsItem::ItemIsMovable);
}

QRectF QtArrowItem::boundingRect() const
{
  return QRectF(this->line().p1(),this->line().p2());
}

void QtArrowItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidget *)
{
  painter->drawLine(this->line());
}

 

 

 

 

 

./CppQtExample31/qtarrowswidget.h

 

#ifndef QTTEXTPOSITIONWIDGET_H
#define QTTEXTPOSITIONWIDGET_H

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

struct QtArrowsWidget : public QGraphicsView
{
  QtArrowsWidget();
};

#endif // QTTEXTPOSITIONWIDGET_H

 

 

 

 

 

./CppQtExample31/qtarrowswidget.cpp

 

#include <cassert>

#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Weffc++"
#pragma GCC diagnostic ignored "-Wunused-local-typedefs"
#include <QGraphicsScene>
#include <QGraphicsPixmapItem>
#include "qtarrowitem.h"
#include "qtarrowswidget.h"
#pragma GCC diagnostic pop

QtArrowsWidget::QtArrowsWidget()
{
  QGraphicsScene * const scene = new QGraphicsScene(this);
  this->setScene(scene);

  const int n_lines = 10;
  for (int i=0; i!=n_lines; ++i)
  {
    const double x1 = ((static_cast<double>(std::rand()) / static_cast<double>(RAND_MAX)) * 600.0) - 300.0;
    const double y1 = ((static_cast<double>(std::rand()) / static_cast<double>(RAND_MAX)) * 400.0) - 200.0;
    const bool tail = (std::rand() >> 4) % 2;
    const double x2 = ((static_cast<double>(std::rand()) / static_cast<double>(RAND_MAX)) * 600.0) - 300.0;
    const double y2 = ((static_cast<double>(std::rand()) / static_cast<double>(RAND_MAX)) * 400.0) - 200.0;
    const bool head = (std::rand() >> 4) % 2;

    QtArrowItem * item = new QtArrowItem(x1,y1,tail,x2,y2,head);
    scene->addItem(item);
  }
}

 

 

 

 

 

./CppQtExample31/qtmain.cpp

 

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

int main(int argc, char *argv[])
{
  QApplication a(argc, argv);
  Dialog w;
  {
    //Resize the dialog and put it in the screen center
    w.setGeometry(0,0,600,400);
    const QRect screen = QApplication::desktop()->screenGeometry();
    w.move( screen.center() - w.rect().center() );
  }
  w.show();
  return a.exec();
}

 

 

 

 

 

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