Results 1 to 10 of 10

Thread: Widget-Dialog communication

  1. #1
    Join Date
    Jul 2007
    Posts
    104
    Thanks
    22
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Widget-Dialog communication

    I created a widget.It needs some informations from user.So I created a dialog too.And this dialog has treeWidget and items...(I thought to use ready dialogs.But they are specific )

    So how can communicate widget with dialog?

  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: Widget-Dialog communication

    You'll have to be more specific than that.
    What exactly has the widget to communicate to the dialog( or the other way around)?
    You could always use signals and slots, but it really depends on what you have to do.

    Regards

  3. #3
    Join Date
    Jul 2007
    Posts
    104
    Thanks
    22
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Widget-Dialog communication

    Only that:
    Widget waits the name of selected tableWidgetItem which is listed in tableWidget on Dialog.
    so I created both widget and dialog.dialog is a private member of widget class.
    I couldnt understand that:
    how can I call dialog and get the name?
    dialog.exec( ) works?
    Or?

  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: Widget-Dialog communication

    Well, the easiest would be to emit a signal when you press the OK button in the dialog.
    You can pass the the name of the item as parameter in the signal.
    All that remains to do is to connect this signal to a slot in the widget. In this slot you will get the item name and you can use it.


    Regards

  5. #5
    Join Date
    Jul 2007
    Posts
    104
    Thanks
    22
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Widget-Dialog communication

    Ok.But the clicked() signal doesnt take any parameter.So how can I write a slot?
    And I m calling dialog using dialog.exec();
    When the widget called the dialog, does it wait the answer or the end of running of the dialog or doesnt?(I have no debug so I asked)

  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: Widget-Dialog communication

    No, I meant creating a custom signal that you emit when you press the dialog.

    But, if I get it correctly, you start the dialog from the widget.
    Then why don't you wait until the user closes the dialog and just query it for the selected item? You could make a getter in the dialog that just returns a QString.

    The data in the dialog won't be destroyed until the dialog itself is destroyed. Even if you hide it.

    Regards

  7. #7
    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: Widget-Dialog communication

    This is roughly how various QDialog subclasses like QFileDialog, QColorDialog and QFontDialog do it. The idea is to add a convenient static function that creates and executes the dialog with initial values set etc. If user cancels, a "null" value is returned, otherwise the actual value is returned:
    Qt Code:
    1. class Dialog : public QDialog
    2. {
    3. public:
    4. Dialog(QWidget* parent = 0);
    5.  
    6. static QString getValue(QWidget* parent = 0,
    7. const QString& caption = QString(),
    8. const QString& value = QString());
    9. private:
    10. QLineEdit* lineEdit;
    11. };
    12.  
    13. Dialog::Dialog(QWidget* parent) : QDialog(parent)
    14. {
    15. lineEdit = new QLineEdit(this);
    16. ...
    17. }
    18.  
    19. QString Dialog::getValue(QWidget* parent, const QString& caption, const QString& value)
    20. {
    21. Dialog dialog(parent);
    22. dialog.setWindowTitle(caption);
    23. dialog.lineEdit->setText(value);
    24. if (dialog.exec() == QDialog::Accepted)
    25. return dialog.lineEdit->text(); // accepted, return actual value
    26. return QString(); // cancelled, return null string
    27. }
    28.  
    29. // usage:
    30. QString value = Dialog::getValue(parent, caption, initial);
    31. if (!value.isNull())
    32. {
    33. // accepted, do something with value
    34. }
    To copy to clipboard, switch view to plain text mode 
    Just notice that for a single textual/numerical input there is also QInputDialog readily available.
    J-P Nurmi

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

    hgedek (13th August 2007)

  9. #8
    Join Date
    Jun 2008
    Location
    Glenwood, NJ USA
    Posts
    32
    Thanks
    1
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11

    Default Re: Widget-Dialog communication

    I have searched for best practices on returning values from dialogs for application use. This thread shows the basic concept. I would like to implement in a similiar fashion but have several values that need to be returned. Would returning a QList<QVariant> of the values be a good approach? Or are thier better ways that would take advantage of signals/slots etc. ?

  10. #9
    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: Widget-Dialog communication

    Quote Originally Posted by awhite1159 View Post
    I would like to implement in a similiar fashion but have several values that need to be returned. Would returning a QList<QVariant> of the values be a good approach? Or are thier better ways that would take advantage of signals/slots etc. ?
    I would rather invent "a custom data type" which contains all necessary values and an isNull() method (to express cancelling). Qt makes it easy to get custom data types work seamlessly with QVariant, signals and slots, QDebug, QDataStream and QSettings...
    J-P Nurmi

  11. #10
    Join Date
    Jun 2008
    Location
    Glenwood, NJ USA
    Posts
    32
    Thanks
    1
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11

    Default Re: Widget-Dialog communication

    Excellent. Thanks. I will try to implement it this way.

Similar Threads

  1. Replies: 4
    Last Post: 25th August 2014, 18:05
  2. closing dialog in hidden main Widget
    By Lele in forum Qt Programming
    Replies: 3
    Last Post: 6th December 2006, 10:35
  3. Docking a widget to fill a dialog
    By mridey in forum Qt Tools
    Replies: 1
    Last Post: 16th November 2006, 11:45
  4. Previewing a stack widget dialog
    By mikeh in forum Qt Tools
    Replies: 4
    Last Post: 2nd October 2006, 10:19
  5. dialog box
    By Bahar in forum Qt Programming
    Replies: 3
    Last Post: 31st January 2006, 14:52

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.