How to freeze a window after .show()
-
I have written a program that saves some coordinate points to a txt file and another program that reads those points and plots them to a graph. My class extends QWidget and it draws the graph using QPainter. It plots the graph fine but if I click outside of the window, to something that is or isn't being used by the program, I get the "...exe has stopped working, windows is looking for a solution"
Does anyone know why this happens? my desire would be for the paintevent to happen and then freeze the drawing once its finished.
Thanks in advance!
-
-
sorry to be so late getting back to you, but thank you for your response.
when I debug it, it crashes inside of my paintEvent function when I use my QPainter to call @setPen(Qt::gray);@
-
of course, but even if I reduce my paintevent to doing not really anything, I still get the same error. I've commented out most of my code so the compiler sees just the following, maybe this helps;
@
class MyWidget : public QWidget
{
public:
MyWidget();
QPainter painter;protected:
void paintEvent(QPaintEvent *);
};MyWidget::MyWidget()
{
QPalette palette(MyWidget::palette());
palette.setColor(backgroundRole(), Qt::white);
setPalette(palette);
}void MyWidget::paintEvent(QPaintEvent *)
{
painter.begin(this);
painter.setRenderHint(QPainter::Antialiasing);
painter.setPen(Qt::gray);
}
int main(int argc, char *argv[])
{
QApplication app(argc, argv);MyWidget widget;
widget.show();
return app.exec();
}@ -
I see two options. The first is properly ending the painting in your paintEvent():
@void MyWidget::paintEvent(QPaintEvent *)
{
painter.begin(this);
painter.setRenderHint(QPainter::Antialiasing);
painter.setPen(Qt::gray);
painter.end();
}
@The second has my preference:
@
void MyWidget::paintEvent(QPaintEvent *)
{
QPainter painter(this);
painter.setRenderHint(QPainter::Antialiasing);
painter.setPen(Qt::gray);
}
@Simple fact is that you only need the painter inside the paintEvent. Actually, you should only use it inside your paintEvent(). Best way to ensure that is creating it there. The cost of creating the painter is negligible compared to the painting itself. It also ensures proper ending of the painting.
-
thank you very much! the painter.end() worked perfectly. I originally had it the second way as your preference, but I made it public because my paintevent became to many lines to be readable so I broke it up into smaller helper functions. nevertheless, I appreciate your help!