Hi All,
I am attempting to write a function that pops a dialog. My ultimate goal is to call this function via signal/slot. But for now I am just trying to validate that the function will pop a dialog.
I designed a toy dialog for testing purposes. Here is the code for the dialog:
{
//Q_OBJECT
public:
private:
};
{
label
= new QLabel("Hello! This this is the 'Select' dialog!");
connect(exitButton, SIGNAL(clicked()), this, SLOT(close()) );
theLayout->addWidget(label);
theLayout->addWidget(exitButton);
setLayout(theLayout);
}
class ShareDialog : public QDialog
{
//Q_OBJECT
public:
ShareDialog(QWidget *parent=0);
private:
QPushButton *exitButton;
QLabel *label;
};
ShareDialog::ShareDialog(QWidget *parent) : QDialog(parent)
{
label = new QLabel("Hello! This this is the 'Select' dialog!");
exitButton = new QPushButton("Bugout!");
connect(exitButton, SIGNAL(clicked()), this, SLOT(close()) );
QHBoxLayout * theLayout = new QHBoxLayout;
theLayout->addWidget(label);
theLayout->addWidget(exitButton);
setLayout(theLayout);
}
To copy to clipboard, switch view to plain text mode
The dialog works as expected if I invoke it from main():
int main(int argc, char *argv[])
{
ShareDialog * shareDialog = new ShareDialog;
shareDialog->show();
HistoryWindow w;
w.show();
return app.exec();
}
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
ShareDialog * shareDialog = new ShareDialog;
shareDialog->show();
HistoryWindow w;
w.show();
return app.exec();
}
To copy to clipboard, switch view to plain text mode
When I invoke show() from main it works just as I expected. But, when I attempt to the same thing from a different function it crashes the app:
void HistoryWindow::PopShareDialog()
{
if (!dlg)
{
dlg = new ShareDialog(this);
}
Q_ASSERT( dlg != NULL);
dlg->show(); // app crashes if this executes
//shareDialog->raise();
//shareDialog->activateWindow();
}
ui(new Ui::HistoryWindow)
{
ui->setupUi(this);
createModelAndView();
PopShareDialog(); // invoking the dialog from here doesn't work
}
void HistoryWindow::PopShareDialog()
{
if (!dlg)
{
dlg = new ShareDialog(this);
}
Q_ASSERT( dlg != NULL);
dlg->show(); // app crashes if this executes
//shareDialog->raise();
//shareDialog->activateWindow();
}
HistoryWindow::HistoryWindow(QWidget *parent) : QMainWindow(parent),
ui(new Ui::HistoryWindow)
{
ui->setupUi(this);
createModelAndView();
PopShareDialog(); // invoking the dialog from here doesn't work
}
To copy to clipboard, switch view to plain text mode
I do not understand why invoking show() from within PopShareDialog() causes a crash. Does anyone have a clue?
Bookmarks