Need a Little Help Drawing On Screen [Qt]
-
I have already read all the of examples etc on this topic and I am still confused.
I'm working on a project, the general idea is I need to give the user a space within a layout when they can click and the program will use the x & y coordinates of those clicks in several calculations. The program must then draw in that same area the shapes generated by those calculations. I am doing this through visual studios as required by my professor.
Right now I have constructed my layout and have placed a QWidget in the area I wish to use. I have attempted to use QPainter with the following section in an attempt to just draw a line and figure out how to use it. I am having a lot of trouble here and could use some help. Any incite into how to use QPainter or accomplish this with a better widget would be greatly appreciated.
@ middle_box = new QGroupBox(tr("Middle Box"));
QHBoxLayout *middle_layout = new QHBoxLayout();
QGL_1 = new QWidget();
QGL_1->setMinimumSize(500,500);
QPainter paint(QGL_1);
paint.drawLine(10,10,1000,1000);
button_2 = new QPushButton(tr("M2 Button"));
button_3 = new QPushButton(tr("M3 Button"));
button_4 = new QPushButton(tr("M4 Button"));
middle_layout->addWidget(QGL_1);
middle_layout->addWidget(button_2);
middle_layout->addWidget(button_3);
middle_layout->addWidget(button_4);
middle_box->setLayout(middle_layout);@Thank you in advance for the help.
-
QPainter allows you to draw to any QPaintDevice subclass (typically QPixmap and QWidget). The most common usecase is to subclass QWidget, to reimplement the paintEvent() method and to use a QPainter to do custom painting.
Be aware that you cannot use QPainter to draw on a widget outside of its paintEvent() method!
Reimplementing various *Event() methods is "quite common":http://qt-project.org/doc/qt-4.8/QWidget.html#events in Qt, and is amongst others used for handling mouse events.
The "programming tutorial":http://qt-project.org/doc/qt-4.8/gettingstartedqt.html, the "QWidget":http://qt-project.org/doc/qt-4.8/QWidget.html#events and "QPainter":http://qt-project.org/doc/qt-4.8/QPainter.html#details documentation as well as the "examples":http://qt-project.org/doc/qt-4.8/painting-basicdrawing.html and freely available books like "C++ GUI Programming with Qt 4":http://www.qtrac.eu/C++-GUI-Programming-with-Qt-4-1st-ed.zip are a good place to start reading.