Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to freeze a window after .show()

How to freeze a window after .show()

Scheduled Pinned Locked Moved General and Desktop
8 Posts 2 Posters 4.1k Views
  • 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 Offline
    S Offline
    sheehan1234
    wrote on last edited by
    #1

    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
    0
    • F Offline
      F Offline
      Franzk
      wrote on last edited by
      #2

      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
      0
      • S Offline
        S Offline
        sheehan1234
        wrote on last edited by
        #3

        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
        0
        • F Offline
          F Offline
          Franzk
          wrote on last edited by
          #4

          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
          0
          • S Offline
            S Offline
            sheehan1234
            wrote on last edited by
            #5

            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
            0
            • F Offline
              F Offline
              Franzk
              wrote on last edited by
              #6

              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
              0
              • S Offline
                S Offline
                sheehan1234
                wrote on last edited by
                #7

                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
                0
                • F Offline
                  F Offline
                  Franzk
                  wrote on last edited by
                  #8

                  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
                  0

                  • Login

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • Users
                  • Groups
                  • Search
                  • Get Qt Extensions
                  • Unsolved