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. The page is displayed and quickly closed after clicking on a button

The page is displayed and quickly closed after clicking on a button

Scheduled Pinned Locked Moved General and Desktop
20 Posts 5 Posters 7.1k Views 1 Watching
  • 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.
  • D Offline
    D Offline
    dhay06
    wrote on last edited by
    #1

    hey, i have some troubles displaying a page after clicking on a button.
    this is my code
    @ajoutport p;
    p.show();
    @

    when i click in the button the new page is displayed and so quickly closed !! what is the problem? thank's

    1 Reply Last reply
    0
    • G Offline
      G Offline
      goetz
      wrote on last edited by
      #2

      The call to slot show immediately returns. If you do this in a slot, e.g. like

      @
      void myClass:fancySlot()
      {
      ajoutport p;
      p.show();
      }
      @

      your widget in variable p will be destructed immediately after your call to show.

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

      1 Reply Last reply
      0
      • D Offline
        D Offline
        dhay06
        wrote on last edited by
        #3

        could you please explain it more? I didn't understand you! :)

        1 Reply Last reply
        0
        • D Offline
          D Offline
          dangelog
          wrote on last edited by
          #4

          http://tinyurl.com/qt-factoids/ -> basics.

          Software Engineer
          KDAB (UK) Ltd., a KDAB Group company

          1 Reply Last reply
          0
          • D Offline
            D Offline
            dhay06
            wrote on last edited by
            #5

            i resolve the problem, as i'm a beginner in Qt and c++ i commit a simple error in c++.

            must only use new to declare and initialize ajoutport. thank you

            1 Reply Last reply
            0
            • S Offline
              S Offline
              Smar
              wrote on last edited by
              #6

              Let’s have yet one useless response...

              http://doc.qt.nokia.com/latest/qwidget.html#show
              http://doc.qt.nokia.com/latest/qdialog.html#exec

              Function names are really consistent across Qt, learn what some basic ones do and you can do it without even first studying half of the framework... Doc is the way to go, anyway.

              1 Reply Last reply
              0
              • G Offline
                G Offline
                goetz
                wrote on last edited by
                #7

                [quote author="dhay06" date="1296510964"]i resolve the problem, as i'm a beginner in Qt and c++ i commit a simple error in c++.

                must only use new to declare and initialize ajoutport. thank you [/quote]

                Nice. That creates a fine memory leak.

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

                1 Reply Last reply
                0
                • G Offline
                  G Offline
                  giesbert
                  wrote on last edited by
                  #8

                  @
                  void myClass:fancySlot()
                  {
                  ajoutport* p = new ajoutport(this);
                  p->show();
                  }
                  @

                  this would not create the leak :-)

                  Nokia Certified Qt Specialist.
                  Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

                  1 Reply Last reply
                  0
                  • D Offline
                    D Offline
                    dangelog
                    wrote on last edited by
                    #9

                    It stills creates garbage if one activates that slot more than once (and closes that subwindow everytime).

                    Software Engineer
                    KDAB (UK) Ltd., a KDAB Group company

                    1 Reply Last reply
                    0
                    • G Offline
                      G Offline
                      giesbert
                      wrote on last edited by
                      #10

                      but it is deleted, when myClass is deleted, so it's no memory leak, but yes, it's not optimal...

                      Nokia Certified Qt Specialist.
                      Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

                      1 Reply Last reply
                      0
                      • G Offline
                        G Offline
                        giesbert
                        wrote on last edited by
                        #11

                        To make it a bit better, you can use:

                        @
                        void myClass:fancySlot()
                        {
                        ajoutport* p = new ajoutport(this);
                        p->setAttribute (Qt::WA_DeleteOnClose, true);
                        p->show();
                        }
                        @

                        then the widget is deleted, when it is closed.

                        Nokia Certified Qt Specialist.
                        Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

                        1 Reply Last reply
                        0
                        • S Offline
                          S Offline
                          Smar
                          wrote on last edited by
                          #12

                          [quote author="peppe" date="1296553193"]It stills creates garbage if one activates that slot more than once (and closes that subwindow everytime).[/quote]

                          They’ll be destroyed as designed; when the object that has this specified slot is destroyed. Not before. And regardless of amount of created objects.

                          1 Reply Last reply
                          0
                          • D Offline
                            D Offline
                            dangelog
                            wrote on last edited by
                            #13

                            [quote]
                            @
                            p->setAttribute (Qt::WA_DeleteOnClose, true);
                            @
                            [/quote]

                            Exactly.

                            [quote]when the object that has this specified slot is destroyed[/quote]

                            ... which was exactly my point. You don't destroy it => you're creating garbage.

                            Software Engineer
                            KDAB (UK) Ltd., a KDAB Group company

                            1 Reply Last reply
                            0
                            • S Offline
                              S Offline
                              Smar
                              wrote on last edited by
                              #14

                              [quote author="peppe" date="1296555543"][quote]
                              @
                              p->setAttribute (Qt::WA_DeleteOnClose, true);
                              @
                              [/quote]

                              Exactly.

                              [quote]when the object that has this specified slot is destroyed[/quote]

                              ... which was exactly my point. You don't destroy it => you're creating garbage.[/quote]

                              Often that’s desired, though.

                              1 Reply Last reply
                              0
                              • G Offline
                                G Offline
                                goetz
                                wrote on last edited by
                                #15

                                [quote author="Smar" date="1296554983"]
                                [quote author="peppe" date="1296553193"]It stills creates garbage if one activates that slot more than once (and closes that subwindow everytime).[/quote]

                                They’ll be destroyed as designed; when the object that has this specified slot is destroyed. Not before. And regardless of amount of created objects.
                                [/quote]

                                With a long running application, this can chew up a reasonable/too big amount of the memory. This is an issue on mobile/embedded platforms.

                                So, as this forum is a learning resource for many, this should not be left uncommented and strongly advised against. Leaving the additional aspects of proper design in the box for now.

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

                                1 Reply Last reply
                                0
                                • G Offline
                                  G Offline
                                  goetz
                                  wrote on last edited by
                                  #16

                                  [quote author="Smar" date="1296555675"]
                                  [quote author="peppe" date="1296555543"][quote]
                                  @
                                  p->setAttribute (Qt::WA_DeleteOnClose, true);
                                  @
                                  [/quote]

                                  Exactly.

                                  [quote]when the object that has this specified slot is destroyed[/quote]

                                  ... which was exactly my point. You don't destroy it => you're creating garbage.[/quote]

                                  Often that’s desired, though.[/quote]

                                  One want's to create garbage? I'd better not comment further...

                                  Back to topic, setting Qt::WA_DeleteOnClose on a window does not create garbage. It leads to calling "deleteLater() ":http://doc.qt.nokia.com/latest/qobject.html#deleteLater. The event loop eventually destroys the object once it runs again. Although this is not a good solution either. If you want to peek some data out of your dialog after it is closed, your event loop might have run in the meantime and the object is destroyed, leading to a nice segmentation fault. Better call deleteLater() from the managing code.

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

                                  1 Reply Last reply
                                  0
                                  • S Offline
                                    S Offline
                                    Smar
                                    wrote on last edited by
                                    #17

                                    [quote author="Volker" date="1296556565"]

                                    One want's to create garbage? I'd better not comment further...
                                    [/quote]

                                    Imagine a situation where you do some intensive cleanup in destructor of a class. Then you create 100 popups of them, which automatically are getting closed down like in interval of 5 seconds.

                                    Would you rather make application use CPU all of the time or just at the end after nothing else needs the resources? Sure, it’s all application and usage related, but alternatives are here.

                                    Going to not so excessive example, often you just are creating few widgets in modular manner (and not in QApplication or whatever main window you are using), so they’ll get killed in exactly same place they’d do when you’d use manual, and something that needs maintenance, work.

                                    1 Reply Last reply
                                    0
                                    • G Offline
                                      G Offline
                                      goetz
                                      wrote on last edited by
                                      #18

                                      [quote author="Smar" date="1296557025"][quote author="Volker" date="1296556565"]

                                      One want's to create garbage? I'd better not comment further...
                                      [/quote]

                                      Imagine a situation where you do some intensive cleanup in destructor of a class. Then you create 100 popups of them, which automatically are getting closed down like in interval of 5 seconds.
                                      [/quote][/quote]

                                      I would spank any programmer that mauls me with that amount of popups. And before doing that I would kill -9 the application, not giving any bit of that sh...iny program even the smallest chance to clean up its mess...

                                      [quote author="Smar" date="1296557025"]
                                      Would you rather make application use CPU all of the time or just at the end after nothing else needs the resources? Sure, it’s all application and usage related, but alternatives are here.

                                      Going to not so excessive example, often you just are creating few widgets in modular manner (and not in QApplication or whatever main window you are using), so they’ll get killed in exactly same place they’d do when you’d use manual, and something that needs maintenance, work. [/quote]

                                      In that case, I create a single instance of that dialog and reuse it.

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

                                      1 Reply Last reply
                                      0
                                      • S Offline
                                        S Offline
                                        Smar
                                        wrote on last edited by
                                        #19

                                        [quote author="Volker" date="1296558150"]
                                        [quote author="Smar" date="1296557025"]
                                        Would you rather make application use CPU all of the time or just at the end after nothing else needs the resources? Sure, it’s all application and usage related, but alternatives are here.

                                        Going to not so excessive example, often you just are creating few widgets in modular manner (and not in QApplication or whatever main window you are using), so they’ll get killed in exactly same place they’d do when you’d use manual, and something that needs maintenance, work. [/quote]

                                        In that case, I create a single instance of that dialog and reuse it.[/quote]

                                        Be it single or not, it’s still easier to leave the deletion to system.

                                        1 Reply Last reply
                                        0
                                        • G Offline
                                          G Offline
                                          goetz
                                          wrote on last edited by
                                          #20

                                          [quote author="Smar" date="1296558384"]
                                          [quote author="Volker" date="1296558150"]
                                          In that case, I create a single instance of that dialog and reuse it.[/quote]

                                          Be it single or not, it’s still easier to leave the deletion to system.
                                          [/quote]

                                          That's nonsense for at least two reasons.

                                          Deletion by system (read: the Qt event loop) takes the very same resources (say CPU time), but you have no exact control over when it happens. And if you create a big bunch of such heavyweight (in sense of resource allocation/cleanup work) and their deletion is deferred to the end your program will eventually hang a reasonable time on shutting down - leading to our well beloved "this application does not respond" messages (eventually leading the user to abort the application)...

                                          If you call deleteLater() (what actually called with the window flag) the event loop will destroy the object. If it is really that time consuming, the deletion will block your program and you cannot even warn your users, eg. by setting a busy cursor.
                                          And sorry, if the destruction is such big work it does matter if you have to clean up once or 10 times.

                                          It might be easier for you as a programmer, in the sense of "I'm too lazy to think about it, so let Qt do the nasty work for me, I don't care".

                                          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