(C++) QtExample36
February 24, 2017 · View on GitHub
(C++) QtExample36



Qt example 36: arrows with multiple midpoints is a Qt example elaborates on Qt example 34: moveable, selectable and editable arrows, but has arrows with an angle in them instead.
Technical facts
Operating system(s) or programming environment(s)
Lubuntu 15.04 (vivid)
Qt Creator 3.1.1
- G++ 4.9.2
Libraries used:
Qt project file: ./CppQtExample36/CppQtExample36.pro
QT += core gui greaterThan(QT_MAJOR_VERSION, 4): QT += widgets TEMPLATE = app SOURCES += main.cpp \ gamewidget.cpp HEADERS += \ gamewidget.h FORMS += RESOURCES += \ CppQtExample36.qrc
./CppQtExample36/gamewidget.h
#ifndef GAMEWIDGET_H #define GAMEWIDGET_H #include <QWidget> class GameWidget : public QWidget { Q_OBJECT public: GameWidget(); void paintEvent(QPaintEvent *); private: int m_x; int m_y; private slots: ///Responds to internal QTimer void OnTimer(); }; #endif // GAMEWIDGET_H
./CppQtExample36/gamewidget.cpp
#include "gamewidget.h" #include <QBrush> #include <QColor> #include <QDialog> #include <QPainter> #include <QTimer> GameWidget::GameWidget() : m_x(0), m_y(0) { ///Create the heartbeat of the program QTimer * const timer = new QTimer(this); QObject::connect(timer,SIGNAL(timeout()),this,SLOT(OnTimer())); timer->setInterval(20); timer->start(); } void GameWidget::OnTimer() { ++m_x; ++m_y; this->repaint(); if (m_x > 256) { m_x = 0; m_y = 0; } } void GameWidget::paintEvent(QPaintEvent *) { QPainter painter(this); QPixmap pixmap(":/PicBeer.png"); painter.drawPixmap(this->rect(),pixmap); painter.drawPixmap(100-m_x ,100+m_y ,120,120,pixmap); painter.drawPixmap(100-m_x+100,100-m_y ,120,120,pixmap); painter.drawPixmap(100+m_x ,100+m_y+100,120,120,pixmap); painter.drawPixmap(100+m_x+100,100-m_y+100,120,120,pixmap); }
./CppQtExample36/main.cpp
#include <QApplication> #include "gamewidget.h" int main(int argc, char *argv[]) { QApplication a(argc, argv); GameWidget w; w.show(); return a.exec(); }