Home > OS >  QTabwidget click tab event C
QTabwidget click tab event C

Time:08-22

I am new in QT 4 C .I have QT 4 form with QTabwidget like in th picture below.

enter image description here

I want to disply on console the string "aa" by selecting tab on clicking it.

Here is my newForm.h

#ifndef _NEWFORM_H
#define _NEWFORM_H

#include "qwidget.h"
#include "ui_newForm.h"
       
class newForm : public QDialog {
    Q_OBJECT
public:
    newForm();
    virtual ~newForm();
private:
    Ui::newForm widget;
};

#endif /* _NEWFORM_H */
_________________________________________________________________________________________

Here is my main.cpp

#include <QApplication>
#include "newForm.h"

int main(int argc, char *argv[]) {
    // initialize resources, if needed
    // Q_INIT_RESOURCE(resfile);

    QApplication app(argc, argv);

    newForm *a = new newForm();
    a->show();
    // create and show your widgets here

    return app.exec();
}

Here is my newForm.cpp

#include "newForm.h"
#include <iostream>
#include <QDebug>

newForm::newForm() {
    connect(widget.tabWidget, SIGNAL(stateChanged()), this, SLOT(onTabChanged(int)));
    widget.setupUi(this);    
}

newForm::~newForm() {
}
void newForm::onTabChanged(int ){       
    qDebug()<<"aa"<<"\n";       
}

On selecting new tab it is not displaying "aa"?How to qDebug() "aa" on console?

CodePudding user response:

First of all, why are you using Qt 4 in 2022?
Next thing use currentIndexChanged(int) istead of stateChanged().
Did you also noticed that stateChanged() doesnt pass an interger which is needed for onTabChanged(int). You also connected widget.tabWidget which isn't initilized yet.

This code might work:

newForm.h

#ifndef _NEWFORM_H
#define _NEWFORM_H

#include "qwidget.h"

namespace Ui { class newForm; };

       
class newForm : public QDialog {
    Q_OBJECT
public:
    newForm();
    ~newForm();

private:
    Ui::newForm *widget;
};

#endif /* _NEWFORM_H */

newForm.cpp

#include "newForm.h"
#include "ui_newForm.h"

#include <QDebug>


newForm::newForm() 
    : widget(new Ui::newForm)
{
    widget->setupUi(this);

    connect(widget->tabWidget, SIGNAL(currentChanged(int)), this, SLOT(onTabChanged(int)));
}

void newForm::onTabChanged(int ){       
    qDebug() << "aa";       
}

newForm::~newForm() {
    delete widget;
}

main.cpp

#include <QApplication>
#include "newForm.h"


int main(int argc, char *argv[]) {
    // initialize resources, if needed
    // Q_INIT_RESOURCE(resfile);

    QApplication app(argc, argv);

    newForm a;
    a.show();
    // create and show your widgets here

    return app.exec();
}
  • Related