Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QMessageBox and memory deallocation for a QStackedWidget and QListWidget
Qt 6.12 LTS is out! See what's new in the release blog

QMessageBox and memory deallocation for a QStackedWidget and QListWidget

Scheduled Pinned Locked Moved General and Desktop
6 Posts 4 Posters 3.5k 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.
  • ? Offline
    ? Offline
    A Former User
    wrote on last edited by
    #1

    Hi,
    I have two questions:

    1. I have a QMessageBox as follows:
      @
      QMessageBox msg;
      msg.setText(msgStr);
      msg.setStandardButtons(QMessageBox::Yes | QMessageBox::No);
      msg.setDefaultButton(QMessageBox::No);
      int ret = msg.exec();

      if(ret == QMessageBox::No)
      return 1;
      else if(ret == QMessageBox::Yes)
      {
      //msg.accept(); //close message box

      ...  //long code
      

      }
      @

    I want to close the messagebox immediately after the Yes button has been clicked by calling its accept slot, but unfortunately it closes only after that block of long code has finished executing. How can I make this possible?

    1. How can I release the memory allocated for a QStackedWidget and QListWidget using the new operator. I tried to use:
      @delete myStackedWidget;@ at the destructor but my program crashes.
    1 Reply Last reply
    0
    • A Offline
      A Offline
      andre
      wrote on last edited by
      #2

      There are several ways to do that. The underlying problem is, that the eventloop needs to be running for visual changes to happen. Your long block of code blocks that. One way to do it, would be to put that long block of code in a separate slot, and trigger it via a single shot timer at 0 interval. Something like: @

      QMessageBox msg;
      msg.setText(msgStr);
      msg.setStandardButtons(QMessageBox::Yes | QMessageBox::No);
      msg.setDefaultButton(QMessageBox::No);
      int ret = msg.exec();
      
      if(ret == QMessageBox::No)
          return 1;
      else if(ret == QMessageBox::Yes)
      {
           //msg.accept(); //close message box  <-- Don't do this, the message box has already been accepted by clicking the Yes button in the first place!
           QTimer::singleShot(0, this, SLOT(longOperation()));
      }
      

      // ....

      void longOperation()
      {
      // the actual work
      }
      @

      If you just pass this as the parent parameter of the constructor, there is no need for you to explicitly destruct the classes again. They will be destructed when this is destructed.

      Moderators note: It would be better to ask unrelated questions (like the above two) in two separate topics. Please don't open multiple topics on the same issue, but also don't try to discuss multiple issues in the same topic.

      1 Reply Last reply
      0
      • S Offline
        S Offline
        stima_ua
        wrote on last edited by
        #3
        1. Hm... After user click yes/not exec should return result code and dialog should close. You not need call accept. Mb in your "long code" you show dialog again?
        2. Debug help you. For most case program crash means that you work with invalid or null pointer.
          You should check this. Mb some part of you code call "myStackedWidget" or his children, after you delete him.
        1 Reply Last reply
        0
        • ? Offline
          ? Offline
          A Former User
          wrote on last edited by
          #4

          <code>msg.exec()</code> shows the message box as a modal dialog, blocking until the user closes it by pressing a button.

          This means that your applications stops at line #5 and the message box is shown. If the user presses a button the message box is closed, the button returned and the application continues executing at line #7. There is no need to explicitly close the message box using <code>msg.accept()</code>.

          The counterpart of <code>new</code> is <code>delete</code>. If your application crashes, you're most probably deleting an object that either has already been deleted or is still in use.

          You might provide a small, preferably compilable example that reprodcues your problem for further assistance.

          1 Reply Last reply
          0
          • S Offline
            S Offline
            stima_ua
            wrote on last edited by
            #5

            Its test with "long code". Box hide after exec return code.

            @
            #include <QtGui/QApplication>
            #include <QMessageBox>
            #include <QDebug>

            int main(int argc, char *argv[])
            {
            QApplication a(argc, argv);

            QMessageBox box;
            
            box.setStandardButtons(QMessageBox::Yes | QMessageBox::No);
            box.setDefaultButton(QMessageBox::No);
            
            int ret = box.exec&#40;&#41;;
            
            if ( ret == QMessageBox::Yes )
            {
                for(;;); // <<iinfinity, but box is hiden
                qDebug() << "Yes clicked";
            }
            else
            {
                qDebug() << "No clicked";
            }
            
            return a.exec&#40;&#41;;
            

            }
            @

            1 Reply Last reply
            0
            • ? Offline
              ? Offline
              A Former User
              wrote on last edited by
              #6

              Thank you very much for the explanations and illustrations. And for multiple issues on the same topic, point taken, thanks Andre.

              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
              • Unsolved