Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Update: Forum Guidelines & Code of Conduct

    How to freeze a window after .show()

    General and Desktop
    2
    8
    3621
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • S
      sheehan1234 last edited by

      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!

      1 Reply Last reply Reply Quote 0
      • F
        Franzk last edited by

        Could you provide some code (if possible building & only doing that what you describe in a zip) that reproduces the issue, so we can try and see what happens? It's probably a pointer gone astray. Did you try to use a debugger to pin-point the issue?

        "Horse sense is the thing a horse has which keeps it from betting on people." -- W.C. Fields

        http://www.catb.org/~esr/faqs/smart-questions.html

        1 Reply Last reply Reply Quote 0
        • S
          sheehan1234 last edited by

          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);@

          1 Reply Last reply Reply Quote 0
          • F
            Franzk last edited by

            Ah well, could you provide your paintEvent() implementation? This is rather sparse.

            "Horse sense is the thing a horse has which keeps it from betting on people." -- W.C. Fields

            http://www.catb.org/~esr/faqs/smart-questions.html

            1 Reply Last reply Reply Quote 0
            • S
              sheehan1234 last edited by

              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();
              }@

              1 Reply Last reply Reply Quote 0
              • F
                Franzk last edited by

                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.

                "Horse sense is the thing a horse has which keeps it from betting on people." -- W.C. Fields

                http://www.catb.org/~esr/faqs/smart-questions.html

                1 Reply Last reply Reply Quote 0
                • S
                  sheehan1234 last edited by

                  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!

                  1 Reply Last reply Reply Quote 0
                  • F
                    Franzk last edited by

                    Just a matter of design left then.

                    I would pass the painter to the helper functions, rather than keep it in the header. It will allow widgets inheriting from yours to use their own painter. It's really much nicer if you think about it.

                    "Horse sense is the thing a horse has which keeps it from betting on people." -- W.C. Fields

                    http://www.catb.org/~esr/faqs/smart-questions.html

                    1 Reply Last reply Reply Quote 0
                    • First post
                      Last post