PDA

View Full Version : QFileDialog Signal / Class Slot issue



Wasabi
6th August 2010, 17:21
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.



class System : public QObject
{
Q_OBJECT
public:
System();
~System();
public slots:
void OpenFile(QStringList str);
};

System::System()
{
}
void System::OpenFile(QStringList str)
{
QFileDialog a;
a.show();
}
System::~System()
{
}

int main(int argc, char *argv[])
{
QApplication app(argc, argv);
System c;
QFileDialog fd;
QObject::connect(&fd,SIGNAL(filesSelected(QStringList)),&c,SLOT(OpenFile(QStringList)));
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.

Lykurg
6th August 2010, 17:24
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.

Wasabi
7th August 2010, 19:49
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?

squidge
7th August 2010, 20:45
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.

Wasabi
7th August 2010, 20:49
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.

squidge
7th August 2010, 23:48
So like I said above. use exec() instead of show() and it do what you expect.