PDA

View Full Version : unable to pop a dialog



ajax
2nd December 2010, 19:05
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:


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);
}


The dialog works as expected if I invoke it from main():



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

ShareDialog * shareDialog = new ShareDialog;
shareDialog->show();

HistoryWindow w;

w.show();

return app.exec();
}

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();
}

HistoryWindow::HistoryWindow(QWidget *parent) : QMainWindow(parent),
ui(new Ui::HistoryWindow)
{
ui->setupUi(this);
createModelAndView();

PopShareDialog(); // invoking the dialog from here doesn't work
}

I do not understand why invoking show() from within PopShareDialog() causes a crash. Does anyone have a clue?

Zlatomir
2nd December 2010, 19:15
Post a cout or qDebug() output in if, to see if the pointer is initialized:


if (!dlg)
{
std::cout << "About to initialize the dialog\n";
dlg = new ShareDialog(this);
}

In C++ the pointers are not initialized with NULL when they are declared, some compilers might do that (in release build) but it is still not safe to assume that.

Timoteo
2nd December 2010, 19:17
FYI, an uninitialized pointer does not have to be 0. It can be anything. Just because it is not null, does not mean it is valid. Checking for null only makes sense if you previously initialized it to 0. Is dlg a global or what? How is it initialized (if at all)?

ajax
2nd December 2010, 19:44
DOH! (slaps forehead)

I forgot to init the dlg member to NULL.

(curses self)

Thank you, Timoteo. Good catch.

Added after 6 minutes:

Hmmm....


std::cout << "some text\n";

Isn't working for me. Not even from within main():


int main(int argc, char *argv[])
{
QApplication app(argc, argv);
std::cout << "yoohoo!\n" << endl;

HistoryWindow w;

w.show();

return app.exec();
}



I have only been working with Qt for about a week and this is the first time I attempted to use std::cout.

I expected to see the output in the "application output" window. I also ran the app from the command line and expected to see something barfed back to the screen. It didn't happen.

Why wouldn't I get something back in the "application output" window?

Timoteo
2nd December 2010, 20:22
qDebug() does go to the application output window in Creator. You can use that. As for why std::cout doesn't do the same, it is because you need an actual console for that.

CONFIG += console

Creator is passing /susbsystem:windows to the linker (you are using windows, right?) instead of /subsystem:console when you specifiy
QT += gui by default. Which makes stdout not exist.

Clarification: qmake is what ends up passing this to the linker, not Creator. Still the same concept.

Second edit: The 2nd code fragment originally read "CONFIG += gui" and this is wrong.:) Changed for those who might find this later so it isn't confusing. I guess I do need that 2nd pot of coffee.

ajax
2nd December 2010, 20:39
Thanks Timoteo! That worked like a charm!

Yes, I am developing on Windows, for now.

It's going to be very nice having cout around the house. ;)

wysota
3rd December 2010, 00:01
Try to familarize yourself with qDebug(), it's much more useful than cout when it comes to Qt apps.