Results 1 to 6 of 6

Thread: QFileDialog Signal / Class Slot issue

  1. #1
    Join Date
    Aug 2010
    Posts
    65
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Windows

    Default 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.

    Qt Code:
    1. class System : public QObject
    2. {
    3. Q_OBJECT
    4. public:
    5. System();
    6. ~System();
    7. public slots:
    8. void OpenFile(QStringList str);
    9. };
    10.  
    11. System::System()
    12. {
    13. }
    14. void System::OpenFile(QStringList str)
    15. {
    16. a.show();
    17. }
    18. System::~System()
    19. {
    20. }
    21.  
    22. int main(int argc, char *argv[])
    23. {
    24. QApplication app(argc, argv);
    25. System c;
    26. QObject::connect(&fd,SIGNAL(filesSelected(QStringList)),&c,SLOT(OpenFile(QStringList)));
    27. fd.show();
    28. return app.exec();
    29. }
    To copy to clipboard, switch view to plain text mode 

    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.

  2. #2
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default 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.

  3. #3
    Join Date
    Aug 2010
    Posts
    65
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Windows

    Default 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?

  4. #4
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default 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.

  5. #5
    Join Date
    Aug 2010
    Posts
    65
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Windows

    Default 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.

  6. #6
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QFileDialog Signal / Class Slot issue

    So like I said above. use exec() instead of show() and it do what you expect.

Similar Threads

  1. qt signal/slot auto-connect issue
    By parnedo in forum Qt Programming
    Replies: 9
    Last Post: 16th July 2009, 12:55
  2. Signal and Slot Issue
    By dlrlsqa1 in forum Newbie
    Replies: 5
    Last Post: 23rd March 2009, 12:44
  3. Replies: 12
    Last Post: 18th September 2008, 15:04
  4. Signal in base class Slot in Subclass
    By csvivek in forum Newbie
    Replies: 7
    Last Post: 30th March 2008, 16:59
  5. Signal/slot looking in base class, not derived class
    By georgie in forum Qt Programming
    Replies: 2
    Last Post: 12th May 2006, 07:36

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.