[SOLVED]Get mainwindows Widgets
I’m building a desktop application and from a widget that i placed in another widget, reach a label on the mainwindow to change text or color or something.
I’ve tried to send the ref from thet labelwidget thru the constructor but i cant do anything with it.
Is it the parentwidget etc etc or am I completly wrong.
8 replies
Not clear what you want to achieve, but QWidget has a method to get its parent. However I don’t see the problem on keeping a pointer/reference to the widget somewhere, e.g., in your main window class.
Also QObject::children() [doc.qt.nokia.com] could be helpful for you.
[EDIT: merged two comments, please edit comments, do not double comment, Volker]
I’m very bad to explain my problem I think.
Bad english
My case
I got a mainwindow with some widgets .
Qlabel Qtab etc etc
On every tab i have diffrent widgets(Qtforms and Classes)
i Instance the Class when i open that tab
and show that widget.
for exampel
On one widget there are ex . 6 buttons with diffrent conditions and when i click a button i want the text on the Label on the mainwindow to append or change the text.
Is the post above the rigth way to handel this case?
I’ll give it a try.
//Thomas
Hi Thomas,
in that case you would connect the buttons’ clicked() signals to on or more internal slot(s) of your widget. In that you emit a newly defined signal, e.g. changeLabelText(QString). You then connect that signal to the main window’s label.
eg.
- // --- in the header file for your widgets in the tab
- {
- Q_OBJECT
- public:
- // constructors etc.
- signals:
- protected slots:
- void onButton1Clicked();
- };
- // --- in the cpp file for your widgets in the tab
- {
- // setup your usual stuff here
- connect(ui->button1, SIGNAL(clicked()), this, SLOT(onButton1Clicked()));
- }
- MyWidget::onButton1Clicked()
- {
- emit changeLabelText("I have clicked on button 1!");
- }
- // --- in your main widget, where you setup or insert the widgets:
- MainWidget::insertTab(MyWidget *widget)
- {
- tabWidet->addTab(widget, "my widget");
- }
You can ease your life, if you create a base class MyTabContentWidget which does less more than define the signal changeLabelText and subclass your actual widgets from that. This way you have a nice interface to use in your main widget.
You must log in to post a reply. Not a member yet? Register here!


