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. QPushButton inside a tab of QTabWidget

QPushButton inside a tab of QTabWidget

Scheduled Pinned Locked Moved Solved General and Desktop
13 Posts 3 Posters 3.5k Views
  • 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.
  • O Offline
    O Offline
    o6a6r9v1p
    wrote on last edited by A Former User
    #1

    we are using QTabWidget with 3 tabs

    tabs are used for showing the values for different devices

    tabs are called Device1Tab, Device2Tab, Device3Tab

    inside the tab , reading & showing values for respective device.
    in each tab, we have few labels, to show the values & a pushButton.

    values shall be shown when the pushbutton in that tab is pressed.
    we have used following code line

    connect(this, SIGNAL(Device2Tab->GetDataButton_pressed()), this, SLOT(updateData()));
    

    is this correct?

    while accessing the label in a tab, do we have to prefix the tab name?

    Edit: Added code tags -- @Wieland

    1 Reply Last reply
    0
    • dheerendraD Offline
      dheerendraD Offline
      dheerendra
      Qt Champions 2022
      wrote on last edited by
      #2

      Not required. You need to give the pointer to QObject as argument for connect. You can look at documentation.

      Dheerendra
      @Community Service
      Certified Qt Specialist
      http://www.pthinks.com

      1 Reply Last reply
      5
      • J.HilkJ Offline
        J.HilkJ Offline
        J.Hilk
        Moderators
        wrote on last edited by J.Hilk
        #3

        No, close, but not quite correct,

        connect(object1, signalX, object2, slotY);
        

        this is not the object in this case, but the button is.

        in your case:

        //connect(object1, signalX, object2, slotY);
        connect(Device2Tab->GetDataButton, SIGNAL(pressed()), this, SLOT(updateData()));
        

        Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


        Q: What's that?
        A: It's blue light.
        Q: What does it do?
        A: It turns blue.

        O 1 Reply Last reply
        1
        • dheerendraD Offline
          dheerendraD Offline
          dheerendra
          Qt Champions 2022
          wrote on last edited by dheerendra
          #4

          In addition to what said @J-Hilk, Idea is that if you have reference to QObject directly, you can the reference the object directly. Once you specify the QObject pointer as first argument, while specifying the signal you don't have to specify the QObject reference again.

          Dheerendra
          @Community Service
          Certified Qt Specialist
          http://www.pthinks.com

          1 Reply Last reply
          7
          • J.HilkJ J.Hilk

            No, close, but not quite correct,

            connect(object1, signalX, object2, slotY);
            

            this is not the object in this case, but the button is.

            in your case:

            //connect(object1, signalX, object2, slotY);
            connect(Device2Tab->GetDataButton, SIGNAL(pressed()), this, SLOT(updateData()));
            
            O Offline
            O Offline
            o6a6r9v1p
            wrote on last edited by
            #5

            @J.Hilk
            when
            connect(Device2Tab->GetDataButton, SIGNAL(pressed()), this, SLOT(updateData()));
            is used, it gives the error 'class QWidget' has no member named 'GetDataButton'

            when it is used as
            connect(this->GetDataButton, SIGNAL(pressed()), this, SLOT(updateData()));

            compiling, shows tabs, but when GetDataButton is pressed, it is crashing.

            J.HilkJ 1 Reply Last reply
            0
            • O o6a6r9v1p

              @J.Hilk
              when
              connect(Device2Tab->GetDataButton, SIGNAL(pressed()), this, SLOT(updateData()));
              is used, it gives the error 'class QWidget' has no member named 'GetDataButton'

              when it is used as
              connect(this->GetDataButton, SIGNAL(pressed()), this, SLOT(updateData()));

              compiling, shows tabs, but when GetDataButton is pressed, it is crashing.

              J.HilkJ Offline
              J.HilkJ Offline
              J.Hilk
              Moderators
              wrote on last edited by
              #6

              @o6a6r9v1p

              thats actually to be expected behaviour.

              The old SIGNAL(), SLOT() syntax checks during runtime and not compile time.

              Anyway, I don't know how your Device2Tab and GetDataButton are created.

              For example:
              If you use the designer, than the correct pointer to the button would be

              connect(ui->GetDataButton, SIGNAL(pressed()), this, SLOT(updateData()));
              

              If you create it from code:

              QPushButton *GetDataButton = new  QPushButton(Device2Tab);
              

              than you'll have to write:

              void MyClass::SomeFunction() {
                 QPushButton *GetDataButton = new  QPushButton(Device2Tab);
                  ....
                  //Button Specifications
                  ...
                  connect(GetDataButton, SIGNAL(pressed()), this, SLOT(updateData()));
              }
              

              Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


              Q: What's that?
              A: It's blue light.
              Q: What does it do?
              A: It turns blue.

              1 Reply Last reply
              1
              • dheerendraD Offline
                dheerendraD Offline
                dheerendra
                Qt Champions 2022
                wrote on last edited by
                #7

                What is Device2Tab ? Is it QTableWidget class ? If yes, it will not have GetDataButton variable.

                Looks like GetDataButton is QPushButton type variable in side your widget class and you have not allocated memory for the same.

                Can you paste the complete code sample on this ?

                Dheerendra
                @Community Service
                Certified Qt Specialist
                http://www.pthinks.com

                O 1 Reply Last reply
                5
                • dheerendraD dheerendra

                  What is Device2Tab ? Is it QTableWidget class ? If yes, it will not have GetDataButton variable.

                  Looks like GetDataButton is QPushButton type variable in side your widget class and you have not allocated memory for the same.

                  Can you paste the complete code sample on this ?

                  O Offline
                  O Offline
                  o6a6r9v1p
                  wrote on last edited by
                  #8

                  @dheerendra
                  to make the program short, i removed code for device3 tab. it is written in same way as for other two tabs.

                  @
                  //demo.h

                  class DemoApp : public QWidget
                  {
                  Q_OBJECT

                  public:
                  ----
                  QPushButton *sGetDataButton;
                  QPushButton *smoreButton;

                  QDialogButtonBox *sbuttonBox;

                  QTabWidget *DashBoard = new QTabWidget();
                  QWidget *Device1Tab= new QWidget();
                  QWidget *Device3Tab= new QWidget();
                  QWidget *Device2Tab= new QWidget();

                  ----
                  -----
                  

                  float valueTemp;
                  float valueBV, valueBCI;
                  float valueLV;
                  }
                  @

                  @
                  //demo.cpp
                  DemoApp::DemoApp(QWidget *parent) :
                  QWidget(parent) // QMainWindow(parent)
                  {
                  // QWidget *
                  // window = new QWidget(this);

                  DashBoardGui();
                  

                  QVBoxLayout *mainLayout = new QVBoxLayout;
                  mainLayout->addWidget(DashBoard);
                  window->setWindowTitle("Device Monitor");
                  window->setLayout(mainLayout); //vertical
                  window->resize(320,240);
                  window->show();
                  connect(this->sGetDataButton, SIGNAL(pressed()), this, SLOT(updateSettings()));
                  }

                  void DemoApp::DashBoardGui()
                  {
                  device1Tab();
                  device2Tab();

                  DashBoard->addTab( Device1Tab, tr("Device1"));
                  DashBoard->addTab( Device2Tab, tr("Device2"));
                  

                  }

                  void DemoApp::device1Tab()
                  {

                  QLabel *LabelBV = new QLabel(tr("Battery Voltage(V):"));
                  
                  LabelBV_Value = new QLabel(tr("        "));
                  LabelBV_Value ->setFixedWidth(80);
                  
                  LabelBV_Value->setFrameStyle(QFrame::Panel | QFrame::Sunken);
                  
                  QLabel *LabelBCI = new QLabel(tr("Battery Current(A):"));
                  LabelBCI_Value = new QLabel(tr("        "));
                  LabelBCI_Value->setFrameStyle(QFrame::Panel | QFrame::Sunken);
                  
                  
                  QVBoxLayout *VLayout1 = new QVBoxLayout;
                   QVBoxLayout *VLayout2 = new QVBoxLayout;
                    QVBoxLayout *VLayout3 = new QVBoxLayout;
                     QHBoxLayout *HLayout1 = new QHBoxLayout;
                     QHBoxLayout *HLayout2 = new QHBoxLayout;
                  
                     VLayout1->addWidget(LabelBV);
                     VLayout2->addWidget(LabelBV_Value);
                  
                     VLayout1->addWidget(LabelBCI);
                     VLayout2->addWidget(LabelBCI_Value);
                  
                     HLayout1->addLayout(VLayout1);
                     HLayout1->addLayout(VLayout2);
                  
                     HLayout2->addLayout(HLayout1);
                     HLayout2->addLayout(VLayout3);
                  
                  QPushButton *GetDataButton1 = new QPushButton(tr("&GetData"));
                  GetDataButton1->setDefault(true);
                  
                  QPushButton *moreButton1 = new QPushButton(tr("&More"));
                  moreButton1->setCheckable(true);
                  moreButton1->setAutoDefault(false);
                  
                  QDialogButtonBox *buttonBox1 = new QDialogButtonBox(Qt::Horizontal);
                  buttonBox1->addButton(GetDataButton1, QDialogButtonBox::ActionRole);
                  buttonBox1->addButton(moreButton1, QDialogButtonBox::ActionRole);
                  
                  
                  QVBoxLayout *mainLayout = new QVBoxLayout;
                  QHBoxLayout *mainLayout3 = new QHBoxLayout;
                  QVBoxLayout *mainLayout1 = new QVBoxLayout;
                  QVBoxLayout *mainLayout2 = new QVBoxLayout;
                  
                  mainLayout->addLayout(HLayout2);
                  

                  // mainLayout->addLayout(mainLayout3); /////
                  mainLayout->addWidget(buttonBox);
                  //mainLayout->addStretch(1);
                  Device1Tab->setLayout(mainLayout); // HLayout2

                  }

                  void DemoApp::updateDevicetab1()
                  {
                  valueBV=12.34;
                  valueBCI=2.19;
                  LabelBV_Value->setText( QString::number(valueBV, 'f', 3));
                  LabelBCI_Value->setText( QString::number(valueBCI, 'f', 3));
                  }

                  void DemoApp::device2Tab()
                  {

                  QLabel *sLabelTemp = new QLabel(tr("Temperature(C):"));
                  QLabel *sLabelTemp_Value = new QLabel(tr("12345      "));
                  sLabelTemp_Value->setFrameStyle(QFrame::Panel | QFrame::Sunken);
                  QHBoxLayout *sHLayout1 = new QHBoxLayout;
                  
                  sHLayout1->addWidget(sLabelTemp);
                  sHLayout1->addWidget(sLabelTemp_Value);
                  QVBoxLayout *smainLayout = new QVBoxLayout;
                  QHBoxLayout *smainLayout3 = new QHBoxLayout;
                  QVBoxLayout *smainLayout1 = new QVBoxLayout;
                  QVBoxLayout *smainLayout2 = new QVBoxLayout;
                  
                  smainLayout1->addLayout(sHLayout1);
                  sGetDataButton = new QPushButton(tr("&GetData"));
                  sGetDataButton->setDefault(true);
                  
                  
                  smoreButton = new QPushButton(tr("&More"));
                  smoreButton->setAutoDefault(false);
                  
                  
                  QDialogButtonBox *sbuttonBox = new QDialogButtonBox(Qt::Horizontal);
                  sbuttonBox->addButton(sGetDataButton, QDialogButtonBox::ActionRole);
                  sbuttonBox->addButton(smoreButton, QDialogButtonBox::ActionRole);
                  
                  
                  smainLayout3->addLayout(smainLayout1);
                  smainLayout3->addLayout(smainLayout2);
                  
                  smainLayout->addLayout(smainLayout3);
                  

                  // mainLayout->addLayout(mainLayout3); /////
                  smainLayout->addWidget(sGetDataButton);
                  smainLayout->addStretch(1);
                  Device2Tab->setLayout(smainLayout);

                  }

                  void DemoApp::updateDevicetab2()
                  {
                  valueTemp=62.37;

                  sLabelTemp_Value->setText( QString::number(valueTemp, 'f', 3));

                  }

                  when ''GetData'' pushbutton is pressed, respective ''updateDevicetabx()'' is to invoked.

                  can you tell me where i am wrong.

                  O 1 Reply Last reply
                  0
                  • O o6a6r9v1p

                    @dheerendra
                    to make the program short, i removed code for device3 tab. it is written in same way as for other two tabs.

                    @
                    //demo.h

                    class DemoApp : public QWidget
                    {
                    Q_OBJECT

                    public:
                    ----
                    QPushButton *sGetDataButton;
                    QPushButton *smoreButton;

                    QDialogButtonBox *sbuttonBox;

                    QTabWidget *DashBoard = new QTabWidget();
                    QWidget *Device1Tab= new QWidget();
                    QWidget *Device3Tab= new QWidget();
                    QWidget *Device2Tab= new QWidget();

                    ----
                    -----
                    

                    float valueTemp;
                    float valueBV, valueBCI;
                    float valueLV;
                    }
                    @

                    @
                    //demo.cpp
                    DemoApp::DemoApp(QWidget *parent) :
                    QWidget(parent) // QMainWindow(parent)
                    {
                    // QWidget *
                    // window = new QWidget(this);

                    DashBoardGui();
                    

                    QVBoxLayout *mainLayout = new QVBoxLayout;
                    mainLayout->addWidget(DashBoard);
                    window->setWindowTitle("Device Monitor");
                    window->setLayout(mainLayout); //vertical
                    window->resize(320,240);
                    window->show();
                    connect(this->sGetDataButton, SIGNAL(pressed()), this, SLOT(updateSettings()));
                    }

                    void DemoApp::DashBoardGui()
                    {
                    device1Tab();
                    device2Tab();

                    DashBoard->addTab( Device1Tab, tr("Device1"));
                    DashBoard->addTab( Device2Tab, tr("Device2"));
                    

                    }

                    void DemoApp::device1Tab()
                    {

                    QLabel *LabelBV = new QLabel(tr("Battery Voltage(V):"));
                    
                    LabelBV_Value = new QLabel(tr("        "));
                    LabelBV_Value ->setFixedWidth(80);
                    
                    LabelBV_Value->setFrameStyle(QFrame::Panel | QFrame::Sunken);
                    
                    QLabel *LabelBCI = new QLabel(tr("Battery Current(A):"));
                    LabelBCI_Value = new QLabel(tr("        "));
                    LabelBCI_Value->setFrameStyle(QFrame::Panel | QFrame::Sunken);
                    
                    
                    QVBoxLayout *VLayout1 = new QVBoxLayout;
                     QVBoxLayout *VLayout2 = new QVBoxLayout;
                      QVBoxLayout *VLayout3 = new QVBoxLayout;
                       QHBoxLayout *HLayout1 = new QHBoxLayout;
                       QHBoxLayout *HLayout2 = new QHBoxLayout;
                    
                       VLayout1->addWidget(LabelBV);
                       VLayout2->addWidget(LabelBV_Value);
                    
                       VLayout1->addWidget(LabelBCI);
                       VLayout2->addWidget(LabelBCI_Value);
                    
                       HLayout1->addLayout(VLayout1);
                       HLayout1->addLayout(VLayout2);
                    
                       HLayout2->addLayout(HLayout1);
                       HLayout2->addLayout(VLayout3);
                    
                    QPushButton *GetDataButton1 = new QPushButton(tr("&GetData"));
                    GetDataButton1->setDefault(true);
                    
                    QPushButton *moreButton1 = new QPushButton(tr("&More"));
                    moreButton1->setCheckable(true);
                    moreButton1->setAutoDefault(false);
                    
                    QDialogButtonBox *buttonBox1 = new QDialogButtonBox(Qt::Horizontal);
                    buttonBox1->addButton(GetDataButton1, QDialogButtonBox::ActionRole);
                    buttonBox1->addButton(moreButton1, QDialogButtonBox::ActionRole);
                    
                    
                    QVBoxLayout *mainLayout = new QVBoxLayout;
                    QHBoxLayout *mainLayout3 = new QHBoxLayout;
                    QVBoxLayout *mainLayout1 = new QVBoxLayout;
                    QVBoxLayout *mainLayout2 = new QVBoxLayout;
                    
                    mainLayout->addLayout(HLayout2);
                    

                    // mainLayout->addLayout(mainLayout3); /////
                    mainLayout->addWidget(buttonBox);
                    //mainLayout->addStretch(1);
                    Device1Tab->setLayout(mainLayout); // HLayout2

                    }

                    void DemoApp::updateDevicetab1()
                    {
                    valueBV=12.34;
                    valueBCI=2.19;
                    LabelBV_Value->setText( QString::number(valueBV, 'f', 3));
                    LabelBCI_Value->setText( QString::number(valueBCI, 'f', 3));
                    }

                    void DemoApp::device2Tab()
                    {

                    QLabel *sLabelTemp = new QLabel(tr("Temperature(C):"));
                    QLabel *sLabelTemp_Value = new QLabel(tr("12345      "));
                    sLabelTemp_Value->setFrameStyle(QFrame::Panel | QFrame::Sunken);
                    QHBoxLayout *sHLayout1 = new QHBoxLayout;
                    
                    sHLayout1->addWidget(sLabelTemp);
                    sHLayout1->addWidget(sLabelTemp_Value);
                    QVBoxLayout *smainLayout = new QVBoxLayout;
                    QHBoxLayout *smainLayout3 = new QHBoxLayout;
                    QVBoxLayout *smainLayout1 = new QVBoxLayout;
                    QVBoxLayout *smainLayout2 = new QVBoxLayout;
                    
                    smainLayout1->addLayout(sHLayout1);
                    sGetDataButton = new QPushButton(tr("&GetData"));
                    sGetDataButton->setDefault(true);
                    
                    
                    smoreButton = new QPushButton(tr("&More"));
                    smoreButton->setAutoDefault(false);
                    
                    
                    QDialogButtonBox *sbuttonBox = new QDialogButtonBox(Qt::Horizontal);
                    sbuttonBox->addButton(sGetDataButton, QDialogButtonBox::ActionRole);
                    sbuttonBox->addButton(smoreButton, QDialogButtonBox::ActionRole);
                    
                    
                    smainLayout3->addLayout(smainLayout1);
                    smainLayout3->addLayout(smainLayout2);
                    
                    smainLayout->addLayout(smainLayout3);
                    

                    // mainLayout->addLayout(mainLayout3); /////
                    smainLayout->addWidget(sGetDataButton);
                    smainLayout->addStretch(1);
                    Device2Tab->setLayout(smainLayout);

                    }

                    void DemoApp::updateDevicetab2()
                    {
                    valueTemp=62.37;

                    sLabelTemp_Value->setText( QString::number(valueTemp, 'f', 3));

                    }

                    when ''GetData'' pushbutton is pressed, respective ''updateDevicetabx()'' is to invoked.

                    can you tell me where i am wrong.

                    O Offline
                    O Offline
                    o6a6r9v1p
                    wrote on last edited by
                    #9

                    @o6a6r9v1p said in pushButton inside a tab of tabWidget:

                    connect(this->sGetDataButton, SIGNAL(pressed()), this, SLOT(updateSettings()));

                    this is now:
                    connect(this->sGetDataButton, SIGNAL(pressed()), this, SLOT(updateDevicetab2()));

                    J.HilkJ 1 Reply Last reply
                    0
                    • O o6a6r9v1p

                      @o6a6r9v1p said in pushButton inside a tab of tabWidget:

                      connect(this->sGetDataButton, SIGNAL(pressed()), this, SLOT(updateSettings()));

                      this is now:
                      connect(this->sGetDataButton, SIGNAL(pressed()), this, SLOT(updateDevicetab2()));

                      J.HilkJ Offline
                      J.HilkJ Offline
                      J.Hilk
                      Moderators
                      wrote on last edited by
                      #10

                      @o6a6r9v1p

                      move

                      connect(this->sGetDataButton, SIGNAL(pressed()), this, SLOT(updateSettings()));
                      

                      from the constructor to

                      void DemoApp::device2Tab()
                      

                      and shorten it to

                      connect(sGetDataButton, SIGNAL(pressed()), this, SLOT(updateSettings()));
                      

                      and it should work as intendet.


                      Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                      Q: What's that?
                      A: It's blue light.
                      Q: What does it do?
                      A: It turns blue.

                      O 2 Replies Last reply
                      1
                      • J.HilkJ J.Hilk

                        @o6a6r9v1p

                        move

                        connect(this->sGetDataButton, SIGNAL(pressed()), this, SLOT(updateSettings()));
                        

                        from the constructor to

                        void DemoApp::device2Tab()
                        

                        and shorten it to

                        connect(sGetDataButton, SIGNAL(pressed()), this, SLOT(updateSettings()));
                        

                        and it should work as intendet.

                        O Offline
                        O Offline
                        o6a6r9v1p
                        wrote on last edited by
                        #11

                        @J.Hilk
                        thanks for the quick help.
                        but the program crashes when getdata button is pressed.
                        I will go through program & check it.

                        1 Reply Last reply
                        0
                        • J.HilkJ J.Hilk

                          @o6a6r9v1p

                          move

                          connect(this->sGetDataButton, SIGNAL(pressed()), this, SLOT(updateSettings()));
                          

                          from the constructor to

                          void DemoApp::device2Tab()
                          

                          and shorten it to

                          connect(sGetDataButton, SIGNAL(pressed()), this, SLOT(updateSettings()));
                          

                          and it should work as intendet.

                          O Offline
                          O Offline
                          o6a6r9v1p
                          wrote on last edited by
                          #12

                          @J.Hilk
                          this worked fine with device1Tab.
                          When same is extended to deviceTab2, compiling without eerors. When pushbutton on deviceTab2 is presses, it is crashing. In wahtever manner, it is done in deviceTab1, in the same way code in deviceTab2 is done. Where shall I look for possible mistakes.

                          O 1 Reply Last reply
                          0
                          • O o6a6r9v1p

                            @J.Hilk
                            this worked fine with device1Tab.
                            When same is extended to deviceTab2, compiling without eerors. When pushbutton on deviceTab2 is presses, it is crashing. In wahtever manner, it is done in deviceTab1, in the same way code in deviceTab2 is done. Where shall I look for possible mistakes.

                            O Offline
                            O Offline
                            o6a6r9v1p
                            wrote on last edited by
                            #13

                            @o6a6r9v1p
                            Got the problem, the label is to declared public. it is working fine.
                            Thanks to all

                            1 Reply Last reply
                            1

                            • Login

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