Results 1 to 4 of 4

Thread: Qt4/C++ - Accessing data on a QDialog form a QMainWindow

  1. #1
    Join Date
    Oct 2006
    Posts
    105
    Thanks
    13
    Thanked 4 Times in 4 Posts
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Qt4/C++ - Accessing data on a QDialog form a QMainWindow

    Hello,

    I'm constructing multiple QDialogs from a button on a QMainWindow.
    Each dialog gets filled with different data when constructed and I (may) edit this data.
    I can find which dialogs have been edited and get the dialogs objectName when I click
    the qmainwindow 'save' button.
    Is it possible to access, say a QLabel->text() on one of the dialogs only knowing the dialogs objectName?
    If 'yes, how?
    Perhaps I should adopt a different approach.

    Regards
    Qt Code:
    1. class propForm : public QDialog
    2. {
    3. Q_OBJECT
    4.  
    5. public:
    6. propForm(QWidget *parent = 0);
    7. ...
    8.  
    9. propForm::propForm(QWidget *parent) : QDialog(parent)
    10. {
    11. ...
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. void myMaiWindow::showDialogButton() // slot
    2. {
    3. cm::formCount += 1;
    4. prop = new propForm(this);
    5. prop->setObjectName("prop" + QString::number(cm::formCount));
    6. qDebug() << "name - " << prop->objectName();
    7. }
    8.  
    9. void myMainWindow::saveButton() // slot
    10. {
    11. qDebug() << "Save - " << cm::filePath;
    12. qDebug() << "last Lost - " << cm::formLostFocus << "\n";
    13.  
    14. QList<QDialog *> widgets = findChildren<QDialog *>();
    15. qDebug() << widgets;
    16.  
    17. for (int i; i < cm::formCount; i++) {
    18. // if edited get data and save the file
    19. // getting the data is the don't know bit.
    20. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Dec 2009
    Location
    New Orleans, Louisiana
    Posts
    791
    Thanks
    13
    Thanked 153 Times in 150 Posts
    Qt products
    Qt5
    Platforms
    MacOS X

    Default Re: Qt4/C++ - Accessing data on a QDialog form a QMainWindow

    Have you tried QDialog::findChild()?

  3. #3
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Qt4/C++ - Accessing data on a QDialog form a QMainWindow

    Perhaps I should adopt a different approach.
    You should adopt a different approach. The whole idea of object-oriented programming is to encapsulate information and package it in ways that you do not need to know anything about the internal implementation of something in order to use it in an application.

    In Qt terms, it means that the user of a dialog should never need to know anything about the sub-widgets contained within the dialog. Instead, the dialog should inform its user that something it might be interested in has been edited by the user. So you have a bunch of ways to do this:

    - design a data structure (a class or struct) that holds the information that is to be edited by the dialog, and give the dialog get / set methods so that the application can set the initial values for editing, and get the edited values when the user closes the dialog with the OK button.

    - use the same data structure and a "set" method, but add a signal to the dialog that is emitted with an edited copy of the struct when the user changes something

    - expose signals and slots for each sub-widget in the dialog that the application can connect to to learn of editing changes.

    These are ranked from highest degree of encapsulation to lowest. The first method can be used without any exposed dialog signals or slots, meaning you can use anything that accepts the data structure. The last method is the worst because it essentially exposes everything. You can't change anything about the dialog without changing the application that uses it.

  4. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Qt4/C++ - Accessing data on a QDialog form a QMainWindow

    Regarding findChildren: it you already know that you need access to an object after its creation, why throw away the knowledge of its pointer?

    Better store it in some form of container, e.g. an associative container like a map or hash.

    Cheers,
    _

Similar Threads

  1. Replies: 2
    Last Post: 30th November 2013, 00:19
  2. How to pass a member data from QMainWindow to QDialog
    By pollausen85 in forum Qt Programming
    Replies: 3
    Last Post: 11th October 2013, 10:40
  3. Replies: 1
    Last Post: 26th March 2013, 02:04
  4. Replies: 0
    Last Post: 22nd September 2011, 10:31
  5. QDialog and QMainWindow Data Transfer
    By Ahmad in forum Qt Programming
    Replies: 12
    Last Post: 6th May 2007, 07:02

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.