Results 1 to 12 of 12

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

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

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

    Hi!!

    I have an application where i can import raw videofiles and make some transformations on it.
    When I am done I would want to open up a QDialog so that the user can specify some settings when exporting the sequence and when the user press the export button the sequence will be processed and exported. But I would want to keep it simple, i.e all this should be made in the same method with no connections like for example a QFileDialog.

    I have made a new class that inherits from QDialog but I there must be some thing wrong.
    When i call myDialog->exec() the dialog pops up but the code in the mainwindow contiues to execute.

    I want the program to wait so that the user can make his choises in the dialog and when the ok button is pressed i can read out what settings was made and start export.
    Kind of like this:
    Qt Code:
    1. MyDialog myDialog = new MyDialog(this);
    2. QStringList settings = myDialog->exec();
    To copy to clipboard, switch view to plain text mode 
    or
    Qt Code:
    1. MyDialog myDialog = new MyDialog(this);
    2. myDialog->exec();
    3. QStringList settings = myDialog->getSettings();
    To copy to clipboard, switch view to plain text mode 

    When I use QFileDialog it possible to do like:
    Qt Code:
    1. QString file = QFileDialog::getSaveFileName(.........
    To copy to clipboard, switch view to plain text mode 

    This is how I do it now:
    Qt Code:
    1. void MainWindow::exportSequence()
    2. {
    3. // open a dialog for choosing export settings
    4. MyDialog *md = new MyDialog(this);
    5. md->exec();
    6. qDebug() << esw->getSettings();
    7. //start exporting video
    8. .....
    To copy to clipboard, switch view to plain text mode 
    MyDialog:
    Qt Code:
    1. class MyDialog: public QDialog
    2. {
    3. Q_OBJECT
    4.  
    5. public:
    6. MyDialog(QWidget *parent=0);
    7.  
    8. QStringList getSettings() {return settings;}
    9. public slots:
    10. int exec();
    11. void done(int val);
    12. void accept();
    13. void reject();
    14. private:
    15. QStringList settings;
    16. QPushButton *exportButton;
    17. QPushButton *cancelButton;
    18. .....
    19. Lots of buttons and other stuff
    20. .....
    21. };
    22.  
    23. MyDialog::MyDialog(QWidget *parent) : QDialog(parent)
    24. {
    25.  
    26.  
    27. setModal(true);
    28. setFocusPolicy(Qt::StrongFocus);
    29. setFocus();
    30. ......
    31. exportButton = new QPushButton(tr("Export"));
    32. connect(exportButton, SIGNAL(clicked()), this, SLOT(accept()));
    33. cancelButton = new QPushButton(tr("Cancel"));
    34. connect(cancelButton, SIGNAL(clicked()), this, SLOT(reject()));
    35. .....
    36. }
    37. int MyDialog::exec()
    38. {
    39. activateWindow();
    40. this->show();
    41. return 1;
    42. }
    43. void MyDialog::done(int val)
    44. {
    45. printf("done()\n");
    46. }
    47.  
    48. void MyDialog::accept()
    49. {
    50. settings << "setting1" << "setting2" << "ect";
    51. printf("Accept.\n");
    52. this->hide();
    53. }
    54. void ExportSequenceWidget::reject()
    55. {
    56. settings.clear();
    57. printf("Reject.\n");
    58. this->hide();
    59. }
    To copy to clipboard, switch view to plain text mode 

    I have been looking at the examples but I can't really figure out how to do this.
    Please help me solve this.
    Many thanks.
    //Nils

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

    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

  3. #3
    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?

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

    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

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

    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

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

    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

  7. #7
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    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.

  8. #8
    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!!!

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

    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!

  10. #10
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    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.

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

    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

  12. #12
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    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...

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.