Results 1 to 12 of 12

Thread: How do I make my own QDialog working like for example QFileDialog

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    8
    Thanked 541 Times in 521 Posts

    Default Re: How do I make my own QDialog working like for example QFileDialog

    What do you mean when you say the code in main window continues? That is not possible...
    Have you tested this in debug?
    Do you have other threads running?

    Anyway, exec() returns a code that specifies if the user pressed OK or cancel. So you should test for that ( provided that you have OK and cancel buttons ).

    regards

  2. #2
    Join Date
    Aug 2006
    Posts
    20
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How do I make my own QDialog working like for example QFileDialog

    Quote Originally Posted by marcel View Post
    What do you mean when you say the code in main window continues? That is not possible...
    Have you tested this in debug?
    Do you have other threads running?
    I know I thougt it sholdn't, I have no other threads.
    But if do like this using the code above.
    Qt Code:
    1. int r = esw->exec();
    2. qDebug() << r;
    3. qDebug() << esw->getSettings();
    To copy to clipboard, switch view to plain text mode 
    printout is:
    1
    ()
    Accept.

    Quote Originally Posted by marcel View Post
    Anyway, exec() returns a code that specifies if the user pressed OK or cancel. So you should test for that ( provided that you have OK and cancel buttons ).
    I have only impelemented two buttons and connected them to accept and reject slots. Is that enough?

    But....Ah...hmmm..."show() returns control to the caller immediately". Maybe I shouldn't use show() but how do I tell it to be visible?

  3. #3
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    8
    Thanked 541 Times in 521 Posts

    Default Re: How do I make my own QDialog working like for example QFileDialog

    Oh, that's because in your exec() implementation you call show() . I missed that.
    You must call QDialog::exec().

    Regards

  4. #4
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts

    Default Re: How do I make my own QDialog working like for example QFileDialog

    MyDialog::exec() is incorrect. First of all, QDialog::exec() is not virtual so it might be a better idea to rename the method to avoid confusion if you even need a method like that at all. I'd suggest removing it and using QDialog::exec() which should be sufficient for you. QDialog::exec() starts an event loop which blocks while the dialog is open.

    The static convenience methods of various QDialog subclasses work more or less like this:
    Qt Code:
    1. class MyDialog : public QDialog
    2. {
    3. public:
    4. static QString doSomething(QWidget* parent = 0);
    5.  
    6. private:
    7. QString info;
    8. };
    9.  
    10. QString MyDialog::doSomething(QWidget* parent)
    11. {
    12. MyDialog dialog(parent);
    13. if (dialog.exec() == QDialog::Accepted)
    14. return dialog.info;
    15. return QString();
    16. }
    17.  
    18. // usage:
    19. QString blaa = MyDialog::doSomething();
    20. if (!blaa.isNull())
    21. {
    22. // dialog was accepted, blaa contains the info
    23. }
    To copy to clipboard, switch view to plain text mode 
    J-P Nurmi

  5. #5
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    8
    Thanked 541 Times in 521 Posts

    Default Re: How do I make my own QDialog working like for example QFileDialog

    First of all, QDialog::exec() is not virtual
    exec() is a slot in QDialog, therefore it is virtual.

    But yes, you are right, it is better to use QDialog::exec directly and won't make any difference in this case.

    Regards

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,373
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Thanks
    3
    Thanked 5,019 Times in 4,795 Posts
    Wiki edits
    10

    Default Re: How do I make my own QDialog working like for example QFileDialog

    Quote Originally Posted by marcel View Post
    exec() is a slot in QDialog, therefore it is virtual.
    Only when called as a slot.

  7. #7
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    8
    Thanked 541 Times in 521 Posts

    Default Re: How do I make my own QDialog working like for example QFileDialog

    Quote Originally Posted by wysota View Post
    Only when called as a slot.
    Yes. Damn it!

  8. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,373
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Thanks
    3
    Thanked 5,019 Times in 4,795 Posts
    Wiki edits
    10

    Default Re: How do I make my own QDialog working like for example QFileDialog

    Quote Originally Posted by marcel View Post
    Yes. Damn it!
    It doesn't have to be virtual to reimplement and use it of course... Virtuality is only needed when something else calls the method.

  9. #9
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts

    Default Re: How do I make my own QDialog working like for example QFileDialog

    Quote Originally Posted by wysota View Post
    It doesn't have to be virtual to reimplement and use it of course... Virtuality is only needed when something else calls the method.
    True, but it's still a bad practice to shadow non-virtual methods.
    J-P Nurmi

  10. #10
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,373
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Thanks
    3
    Thanked 5,019 Times in 4,795 Posts
    Wiki edits
    10

    Default Re: How do I make my own QDialog working like for example QFileDialog

    Quote Originally Posted by jpn View Post
    True, but it's still a bad practice to shadow non-virtual methods.
    "It depends" I don't see anything improper in reimplementing QDialog::exec() I know it's possible that someone will want to call it after casting to QDialog, but hey! let's try our luck Of course it's better to avoid such risks...

  11. #11
    Join Date
    Aug 2006
    Posts
    20
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How do I make my own QDialog working like for example QFileDialog

    Thanks a lot for the help and tips!!
    Got it working now.
    Removed exec(), accept(), reject() and done().

    Now I can just
    Qt Code:
    1. int r = myDialog->exec()
    2. if(r==1)
    3. QStringList settings = myDialog->readSettings()
    4. delete myDialog
    To copy to clipboard, switch view to plain text mode 

    Thanks again!!!

Similar Threads

  1. Compiling with Qmake/Make
    By VireX in forum Newbie
    Replies: 25
    Last Post: 22nd February 2007, 05:57

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
  •  
Qt is a trademark of The Qt Company.