3 Attachment(s)
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 :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.
Code:
// 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:
Attachment 4254
Attachment 4255
Attachment 4253
Thanks.
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.
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.
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.
Code:
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.
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
Code:
class MyClass
{
......
private:
}
and
Code:
class MyClass
{
......
public:
}
In first example with code
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.