PDA

View Full Version : Can't access private member of Dialog from Main Window.



jgoody
7th February 2010, 13:20
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 :confused:

http://i48.tinypic.com/jzdti0.jpg

http://i48.tinypic.com/2cht2w.jpg


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.


// from mainwindow.cpp
void MainWindow::on_actionDialog_1_triggered() // if the menu item triggered.
{
Dialog *mydialog = new Dialog(this);

if (mydialog->exec())
{
QString textIn = mydialog->ui->lineEdit->text();

ui->textEdit->append(textIn);
}

}

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

4254
4255
4253

Thanks.

squidge
7th February 2010, 15:15
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.

jgoody
9th February 2010, 13:55
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.

squidge
9th February 2010, 22:35
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.


void myDialog::DoDataExchange(myDataSource *dataSource)
{
if (dataSource->m_bSaveAndValidate)
{
// save the contents of the dialog to the data source
dataSource->m_sDescription = ui->lineEdit1->text;
dataSource->m_sPartNo = ui->lineEdit2->text;
// etc //
}
else
{
// populate dialog with datasource
ui->lineEdit1->text = dataSource->m_sDescription;
ui->lineEdit2->text = dataSource->m_sPartNo;
}
}


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.

Lesiok
10th February 2010, 06:56
You have a problem with C++ base not with Qt. Look at this two definitions of class MyClass

class MyClass
{
......
private:
QLineEdit line_edit;
}
and

class MyClass
{
......
public:
QLineEdit line_edit;
}

In first example with code

MyClas my_class;
QString text = my_class.line_edit.text();
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.