Results 1 to 5 of 5

Thread: Can't access private member of Dialog from Main Window.

  1. #1
    Join Date
    Feb 2010
    Posts
    2
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Can't access private member of Dialog from Main Window.

    Hello all,

    I'm trying to get a test app working where a Main Window Menu is used to call a Dialog. Text is entered into the Dialog and then transferred to the Main Window. Sounds easy






    My problem is I don't know how to access the Dialog's lineEdit. I've tried the following, but the compiler gives the error that lineEdit is private.

    Qt Code:
    1. // from mainwindow.cpp
    2. void MainWindow::on_actionDialog_1_triggered() // if the menu item triggered.
    3. {
    4. Dialog *mydialog = new Dialog(this);
    5.  
    6. if (mydialog->exec())
    7. {
    8. QString textIn = mydialog->ui->lineEdit->text();
    9.  
    10. ui->textEdit->append(textIn);
    11. }
    12.  
    13. }
    To copy to clipboard, switch view to plain text mode 

    Here are the files generated by Creator from my Dialog.ui:

    ui_dialog.h
    dialog.h
    dialog.cpp

    Thanks.

  2. #2
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Can't access private member of Dialog from Main Window.

    Create a public method in mydialog that returns the value of ui->lineEdit->text as a QString. Call that method instead of trying to access the private ui elements directly from a different class.

  3. The following user says thank you to squidge for this useful post:

    jgoody (9th February 2010)

  4. #3
    Join Date
    Feb 2010
    Posts
    2
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Can't access private member of Dialog from Main Window.

    Thanks.

    I got confused because I was following a tutorial which used public inheritance to create the dialog class, so it is accessed easily with "QString str = dialog.lineEdit->text()".

    If my dialog had a lot of widgets in it, it would be easier to do it that way than create an accessor function for each widget.

  5. #4
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Can't access private member of Dialog from Main Window.

    If your dialog had a lot of widgets in it, you would create a data source for the contents of those widgets and then pass that datasource into your DoDataExchange() method in the dialog. That method would then take that datasource and populate it's own widgets. Once your dialog is complete (eg. user clicks the "Ok" button), the dialog would update the contents of the data source with the contents of the dialog and you would receive it in your other class.

    eg.
    Qt Code:
    1. void myDialog::DoDataExchange(myDataSource *dataSource)
    2. {
    3. if (dataSource->m_bSaveAndValidate)
    4. {
    5. // save the contents of the dialog to the data source
    6. dataSource->m_sDescription = ui->lineEdit1->text;
    7. dataSource->m_sPartNo = ui->lineEdit2->text;
    8. // etc //
    9. }
    10. else
    11. {
    12. // populate dialog with datasource
    13. ui->lineEdit1->text = dataSource->m_sDescription;
    14. ui->lineEdit2->text = dataSource->m_sPartNo;
    15. }
    16. }
    To copy to clipboard, switch view to plain text mode 

    That way your view (the dialog) can change however many times you want - you could change a lineEdit to a ComboBox for example. The data source stays the same, and thus everywhere you use the dialog also stays the same, as you never access the dialog ui elements directly.

    It also makes it a lot easier to use the same dialog in other projects, as it's more encapsulated - there's no need to find and copy the bits of code that manipulate the dialog, as it's all in one place.

  6. The following user says thank you to squidge for this useful post:

    jgoody (11th February 2010)

  7. #5
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,521
    Thanked 278 Times in 273 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Can't access private member of Dialog from Main Window.

    You have a problem with C++ base not with Qt. Look at this two definitions of class MyClass
    Qt Code:
    1. class MyClass
    2. {
    3. ......
    4. private:
    5. QLineEdit line_edit;
    6. }
    To copy to clipboard, switch view to plain text mode 
    and
    Qt Code:
    1. class MyClass
    2. {
    3. ......
    4. public:
    5. QLineEdit line_edit;
    6. }
    To copy to clipboard, switch view to plain text mode 

    In first example with code
    Qt Code:
    1. MyClas my_class;
    2. QString text = my_class.line_edit.text();
    To copy to clipboard, switch view to plain text mode 
    compiler generate error "lineEdit is private". In second definition of MyClass all is OK.

    Just read some C++ tutorials about public, private and protected members.

Similar Threads

  1. Qt 4.6.0: Opening a dialog from a main window
    By dmginc in forum Qt Programming
    Replies: 3
    Last Post: 14th January 2010, 13:16
  2. access main window from function
    By eric in forum Qt Programming
    Replies: 6
    Last Post: 19th January 2008, 22:29
  3. Crash during sending list from dialog to main window
    By Djony in forum Qt Programming
    Replies: 5
    Last Post: 23rd November 2006, 20:43
  4. Std iterator as private member variable
    By Michiel in forum General Programming
    Replies: 5
    Last Post: 21st April 2006, 16:27
  5. QTime private member mds means what?
    By jamadagni in forum Qt Programming
    Replies: 2
    Last Post: 24th March 2006, 19:08

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.