Hello,
I'm new to Qt and I'm working on a program where I need to paint on different Widgets. In the end, I would like to display these widgets frameless and use the main window to change the paintings in these widgets step by step (text, picture, basic drawings). I'm using Qt 5.5 under Windows.
Currently my program consists of a main window with three buttons:
- Start -> creates five Widgets (class OutputWindow)
- Test -> paints within the main window
- Quit -> ends the program
By clicking inside one of the five OutputWindow(s) (A, B, C, D and E), I can paint within each of these Widgets. Unfortunately, I can only paint within one Widget at a time. But I need to preserve the paintings in each Widget and change them individually. How can I preserve the current paintings while drawing on another Widget? (How should I handle update() / this situation?)
The second problem is that I'm not able to trigger the paintEvent for the OutputWindow(s) from within the Main Window. How can I use (for example) a button from the Main Window (class ControlWindow) to call a paintEvent from another class (OutputWindow) to paint on a Widget derived from this class?
I'm not sure if I've understod the use / reimplementation of paintEvent correctly 
To make my problem clear, I've attached some screenshots. And of course here is a part of the Code (you can find the whole project attached as a zip-file!):
controllerWindow.cpp | set variable true if button "Test" (btnTest) was pushed
void ControllerWindow::tempButtonPushed()
{
vtempButtonPushed = true;
update();
}
void ControllerWindow::tempButtonPushed()
{
vtempButtonPushed = true;
update();
}
To copy to clipboard, switch view to plain text mode
controllerWindow.cpp | paintEvent
{
if (vtempButtonPushed == true)
{
QPen pen
(Qt
::red,
10, Qt
::SolidLine, Qt
::SquareCap, Qt
::BevelJoin);
painter.setPen(pen);
painter.drawLine(0, 0, 800, 800);
vtempButtonPushed = false;
}
}
void ControllerWindow::paintEvent(QPaintEvent*)
{
if (vtempButtonPushed == true)
{
QPainter painter(this);
QPen pen(Qt::red, 10, Qt::SolidLine, Qt::SquareCap, Qt::BevelJoin);
painter.setPen(pen);
painter.drawLine(0, 0, 800, 800);
vtempButtonPushed = false;
}
}
To copy to clipboard, switch view to plain text mode
outputWindow.cpp | set variable true if mouse button was clicked
{
vtempButtonPushed = true;
update();
}
void OutputWindow::mousePressEvent(QMouseEvent*)
{
vtempButtonPushed = true;
update();
}
To copy to clipboard, switch view to plain text mode
outputWindow.cpp | paintEvent
{
if (vtempButtonPushed == true)
{
QPen pen
(Qt
::blue,
10, Qt
::SolidLine, Qt
::SquareCap, Qt
::BevelJoin);
painter.setPen(pen);
painter.drawLine(0, 0, 800, 800);
vtempButtonPushed = false;
}
}
void OutputWindow::paintEvent(QPaintEvent*)
{
if (vtempButtonPushed == true)
{
QPainter painter(this);
QPen pen(Qt::blue, 10, Qt::SolidLine, Qt::SquareCap, Qt::BevelJoin);
painter.setPen(pen);
painter.drawLine(0, 0, 800, 800);
vtempButtonPushed = false;
}
}
To copy to clipboard, switch view to plain text mode
Thank you in advance!
Max
Bookmarks