[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.

  • 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()":http://doc.qt.nokia.com/stable/qobject.html#children could be helpful for you.

    [EDIT: merged two comments, please edit comments, do not double comment, Volker]

  • AFter i pressed a button on my shown widget i want to change the state of a label in the Mainclass.

    And i tried to do this just like I do in Java but I got alot of errors in that case

  • You should NOT change the label from the button class. Use signal/slots mechanism to notify the main widget class of the requested change. The buttons provide a clicked() signal that you can connect to.

  • So this code is Compleatly wrong

    Just a little pseudo

    On the widgets Button click()
    mainwindow->Label->setText("Text I want to setSet")


  • Steps are the following:

    • connect a signal from your button to a slot of yours
      @connect( myButton, SIGNAL(clicked()), this, SLOT(updateLabelSlot()));@
    • in the slot perform the update of the text
      @void MainWindow::updateLabelSlot(){
      Label->setText("whatever you want");

  • 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.

  • 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.


    // --- in the header file for your widgets in the tab
    class MyWidget : public QWidget

    // constructors etc.

    void changeLabelText(const QString &text)

    protected slots:
    void onButton1Clicked();


    // --- in the cpp file for your widgets in the tab

    MyWidget::MyWidget(QWidget *parent)
    : QWidget(parent)
    // setup your usual stuff here
    connect(ui->button1, SIGNAL(clicked()), this, SLOT(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");
    connect(widget, SIGNAL(changeLabelText(QString), label, SLOT(setText(QString)));

    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.

  • Nice.
    I'll try this.
    It works perfectly
    Thanks to all

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.