Results 1 to 7 of 7

Thread: Example of Static QDialog

  1. #1
    Join Date
    Jun 2012
    Posts
    13
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Example of Static QDialog

    I'm looking for simple examples of a dialog box form implementation where a user fills out the form and then after the form dialog box exits the calling routine can still call static methods on the dialog box to access the filled out data.

    In other words unless somebody has a better idea I think I'm looking for an example of a static dialog box implementation with static methods and members. My current approach based upon other GUI implementations is not working.

  2. #2
    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: Example of Static QDialog

    Do you use the word "static" in the meaning C++ defines that term? If not, please define what you mean.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Jun 2012
    Posts
    13
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Example of Static QDialog

    Yes. I mean static as in it gets allocated when the program starts and remains for the life of the program so that irregardless of whether the dialog box is open or closed the contained static variables retain their last set state.

    Actually I meant upon the declaration of the dialog box, not the beginning of the program.

  4. #4
    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: Example of Static QDialog

    Quote Originally Posted by sa5webber View Post
    Yes. I mean static as in it gets allocated when the program starts and remains for the life of the program so that irregardless of whether the dialog box is open or closed the contained static variables retain their last set state.
    This has nothing to do with "static methods" then. You mean a static object and you can't have that since you can't allocate any widgets before QApplication object is created. I don't even know why you'd want such thing.

    You can call static methods on objects related to Qt exactly the same way you can call static methods on objects unrelated to Qt (apart the limitation I mentioned).
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Example of Static QDialog

    Yes. I mean static as in it gets allocated when the program starts and remains for the life of the program so that irregardless of whether the dialog box is open or closed the contained static variables retain their last set state.

    Actually I meant upon the declaration of the dialog box, not the beginning of the program.
    No, I don't think you mean "static" in any C++ sense at all. The C++ object and its possible visual presence are not related to each other. The member variables in the object do not go away when the visual item is hidden: they only go away when you destroy the object. Here's an example of a dialog that is shown, hidden, and shown again. The value of its text box persists between showings and is available when the dialog is hidden:

    Qt Code:
    1. #include <QtGui>
    2.  
    3. class MyDialog: public QDialog
    4. {
    5. Q_OBJECT
    6. public:
    7. explicit MyDialog(QWidget *p = 0): QDialog(p) {
    8. QVBoxLayout *layout = new QVBoxLayout(this);
    9. lineEdit = new QLineEdit(this);
    10. QPushButton *button = new QPushButton("Ok", this);
    11. layout->addWidget(lineEdit);
    12. layout->addWidget(button);
    13. setLayout(layout);
    14.  
    15. connect(button, SIGNAL(clicked()), SLOT(accept()));
    16. }
    17.  
    18. QString getText() const {
    19. return lineEdit->text();
    20. }
    21. private:
    22. QLineEdit *lineEdit;
    23. };
    24.  
    25.  
    26. int main(int argc, char **argv)
    27. {
    28. QApplication app(argc, argv);
    29.  
    30. MyDialog dialog; // dialog object exists
    31. (void) dialog.exec(); // dialog is visible
    32. qDebug() << dialog.getText(); // dialog is not visible but the object still exists
    33. (void) dialog.exec(); // dialog is visible
    34. qDebug() << dialog.getText(); // dialog is not visible but the object still exists
    35.  
    36. return 0;
    37. }
    38. #include "main.moc"
    To copy to clipboard, switch view to plain text mode 

  6. #6
    Join Date
    Jun 2012
    Posts
    13
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Example of Static QDialog

    Good explanation. Thanks for the clarification. What you showed is exactly what I want. I need to be able to potentially open and close the form multiple times. It wasn't clear to me what exec() and closing the form actually did to the object in memory and so I was initially deleting the object myself on closure and re-creating it to open it again to avoid any potential memory leak which is why I was asking about the static approach.

    So with that said. I found this link yesterday.

    http://www.qtcentre.org/threads/7315...le-QFileDialog

    It looks like it uses a static method to declare MyDialog and show the dialog. It appears the difference between what you're showing and what it does is that it uses MyDialog directly to open the dialog without having to declare it externally. Although I have a question as to the MyDialog declaration in the static method. Is this declaration also assumed to be static since its in a static method so that the declaration only occurs the first time the method is called?

    In your opinion is there any advantage/disadvantage to either way?

    By the way thanks again for all your expertise and help.


    Added after 20 minutes:


    I should clarify that the code in the link I 'm referring to is in reply #5.
    Last edited by sa5webber; 6th June 2012 at 18:42.

  7. #7
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Example of Static QDialog

    The static function creates a local instance of the MyDialog class which is automatically destroyed at the end of the function. The MyDialog instance is not static just because it is created inside a static function.

    A static convenience function in a class like this dialog is usually used as a way to create a quick, short-lived instance with common defaults that returns a simple value of some sort, e.g. QFileDialog or QInputDialog.

    With QWidgets like QDialog just make sure you give the object a parent at creation on the heap and don't worry about the memory deallocation: it will be freed when the parent is.

  8. The following user says thank you to ChrisW67 for this useful post:

    sa5webber (8th June 2012)

Similar Threads

  1. Replies: 3
    Last Post: 1st June 2011, 15:32
  2. Replies: 9
    Last Post: 25th March 2011, 21:22
  3. QDialog.exec() exiting without calling QDialog::accept()
    By doggrant in forum Qt Programming
    Replies: 3
    Last Post: 2nd February 2011, 11:35
  4. closing a Qdialog called from a Qdialog
    By OverTheOCean in forum Qt Programming
    Replies: 3
    Last Post: 28th September 2009, 08:02
  5. Replies: 4
    Last Post: 14th February 2006, 21:35

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.