Access a dialog's UI from main class.
-
Hello guys,
Let's say that i have a Dialog and a MainWindow classes. MainWindow has (mainwindow.cpp, mainwindow.h, mainwindow.ui, main.cpp). Dialog has (dialog.cpp, dialog.h, dialog.ui). I want to access all the items that dialog.ui has (pushbutton, textbrowser, etc) from mainwindow class.
I hope that you understand my topic. -
@leonidas_lolo the best way would be to use Signals & Slots mechanism.
-
Can anyone post a link or an example (the simplest one).
-
Just create public getter methods in your Dialog class...
QString Dialog::name() const { return ui->nameLineEdit->text(); }
-
A simple example would be :
MainWindow.cpp
Dialog *dialog = new Dialog(this); dialog->show();
Dialog.cpp
Consider it contains aQPushButton
namedpushButton
, thenconnect(pushButton,SIGNAL(clicked()),parentWidget(),SLOT(onDialogButtonClicked()));
where onDialogButtonClicked() is declared as a
SLOT
inMainWindow.cpp
-
Where is the best point to declare a connection ( at main class constructor ? )
-
@leonidas_lolo but then you will need to know that object viz. in other class in main class. Considering the example above, you will need to know
pushButton
inMainWindow.cpp
for connections.
Ofcourse you can also connect to a custom signal in Dialog.cpp which can be emitted onpushButton
's click. -
Yes I think that in my application custom signals and slots are more useful. But is better to make the connection of the signal and slot only when what I want happens or should I declare the connection in the main class constructor ?
-
But is better to make the connection of the signal and slot only when what I want happens.
Yes if you want it then connect each-other objects.
should I declare the connection in the main class constructor ?
You can, if the objects are accessible in constructor.