Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [SOLVED]Get mainwindows Widgets
Forum Updated to NodeBB v4.3 + New Features

[SOLVED]Get mainwindows Widgets

Scheduled Pinned Locked Moved General and Desktop
9 Posts 3 Posters 7.4k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    toho71
    wrote on last edited by
    #1

    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.

    1 Reply Last reply
    0
    • F Offline
      F Offline
      fluca1978
      wrote on last edited by
      #2

      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]

      1 Reply Last reply
      0
      • T Offline
        T Offline
        toho71
        wrote on last edited by
        #3

        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

        1 Reply Last reply
        0
        • G Offline
          G Offline
          goetz
          wrote on last edited by
          #4

          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.

          http://www.catb.org/~esr/faqs/smart-questions.html

          1 Reply Last reply
          0
          • T Offline
            T Offline
            toho71
            wrote on last edited by
            #5

            So this code is Compleatly wrong

            Just a little pseudo

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

            --

            1 Reply Last reply
            0
            • F Offline
              F Offline
              fluca1978
              wrote on last edited by
              #6

              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");
                }@
              1 Reply Last reply
              0
              • T Offline
                T Offline
                toho71
                wrote on last edited by
                #7

                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

                1 Reply Last reply
                0
                • G Offline
                  G Offline
                  goetz
                  wrote on last edited by
                  #8

                  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
                  class MyWidget : public QWidget
                  {
                  Q_OBJECT

                  public:
                  // constructors etc.

                  signals:
                  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()));
                  }

                  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");
                  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.

                  http://www.catb.org/~esr/faqs/smart-questions.html

                  1 Reply Last reply
                  0
                  • T Offline
                    T Offline
                    toho71
                    wrote on last edited by
                    #9

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

                    1 Reply Last reply
                    0

                    • Login

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • Users
                    • Groups
                    • Search
                    • Get Qt Extensions
                    • Unsolved