QPushButton inside a tab of QTabWidget

  • 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

  • Qt Champions 2017

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

  • 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()));

  • Qt Champions 2017

    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.

  • @J.Hilk
    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.

  • @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()));

  • Qt Champions 2017

    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
    to make the program short, i removed code for device3 tab. it is written in same way as for other two tabs.


    class DemoApp : public QWidget

    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;

    DemoApp::DemoApp(QWidget *parent) :
    QWidget(parent) // QMainWindow(parent)
    // QWidget *
    // window = new QWidget(this);


    QVBoxLayout *mainLayout = new QVBoxLayout;
    window->setWindowTitle("Device Monitor");
    window->setLayout(mainLayout); //vertical
    connect(this->sGetDataButton, SIGNAL(pressed()), this, SLOT(updateSettings()));

    void DemoApp::DashBoardGui()

    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;
    QPushButton *GetDataButton1 = new QPushButton(tr("&GetData"));
    QPushButton *moreButton1 = new QPushButton(tr("&More"));
    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(mainLayout3); /////
    Device1Tab->setLayout(mainLayout); // HLayout2


    void DemoApp::updateDevicetab1()
    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;
    QVBoxLayout *smainLayout = new QVBoxLayout;
    QHBoxLayout *smainLayout3 = new QHBoxLayout;
    QVBoxLayout *smainLayout1 = new QVBoxLayout;
    QVBoxLayout *smainLayout2 = new QVBoxLayout;
    sGetDataButton = new QPushButton(tr("&GetData"));
    smoreButton = new QPushButton(tr("&More"));
    QDialogButtonBox *sbuttonBox = new QDialogButtonBox(Qt::Horizontal);
    sbuttonBox->addButton(sGetDataButton, QDialogButtonBox::ActionRole);
    sbuttonBox->addButton(smoreButton, QDialogButtonBox::ActionRole);

    // mainLayout->addLayout(mainLayout3); /////


    void DemoApp::updateDevicetab2()

    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.

  • @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()));

  • @o6a6r9v1p


    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.

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

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

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

Log in to reply

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