QMessageBox and memory deallocation for a QStackedWidget and QListWidget
-
Hi,
I have two questions:-
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?
- 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.
-
-
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.
-
- 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?
- 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.
-
<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.
-
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(); if ( ret == QMessageBox::Yes ) { for(;;); // <<iinfinity, but box is hiden qDebug() << "Yes clicked"; } else { qDebug() << "No clicked"; } return a.exec();
}
@