QFileDialog Signal / Class Slot issue
I'm just starting out on Qt (having a primary use for its file-picking dialogs) and created a very small code to test it out. The code is so far meaningless (doesn't do anything relevant), but I'm just testing the waters for now. (Nevermind the empty constructor and destructor, I deleted their contents since they're related to other things that don't matter here.
Code:
{
Q_OBJECT
public:
System();
~System();
public slots:
};
System::System()
{
}
{
a.show();
}
System::~System()
{
}
int main(int argc, char *argv[])
{
System c;
fd.show();
return app.exec();
}
It's my understanding that the File Dialog should appear (which it does) and after I select a file and press okay, it should send a signal to c's OpenFile() function. If you notice, that function has nothing to do with the address because I'm just testing the SIGNAL-SLOT concept. I'm just expecting to see another FileDialog pop up, which it doesn't.
So, the program runs, the first file dialog (fd) appears, I select a file, press okay, the dialog disappears and, instead of showing a second dialog, the program simply comes to an end.
So, either there's something wrong with my understanding/construct of the QFileDialog, of the SIGNAL-SLOT mechanism or of the workings of a function that is called by this mechanism. I'm quite stumped on this issue, I must say.
I'm on Qt 4.6.3 on VC2008.
Re: QFileDialog Signal / Class Slot issue
You create that dialog on the stack, so it gets deleted right after it was shown. So either create it on the heap or use exec() which blocks the program till the dialog is closed. It's a basic C++ error.
Re: QFileDialog Signal / Class Slot issue
That's what's odd, though. The second dialog doesn't appear at all. It's my understanding that the program sleeps while a dialog is opened. The first one opens and is then closed but the second one doesn't open. Sure, it might get deleted at the end of the SLOT function, but the function shouldn't end until the second dialog is closed by the user, right?
Re: QFileDialog Signal / Class Slot issue
Execution continues after the show()
Execution is halted and a new event loop is created by exec()
Therefore the first will open and then execute the event loop.
The second will just set the visibility of the dialog and then immediately delete it without waiting for user action.
Re: QFileDialog Signal / Class Slot issue
Ah, thanks. I've used another GUI library once that automatically stopped processing while a File Dialog was open and thought Qt was the same.
Re: QFileDialog Signal / Class Slot issue
So like I said above. use exec() instead of show() and it do what you expect.