Reputation: 2442
I am using the QtabWidget in Qt creator and created three tabs(tab-1,tab-2,tab-3).Each tab has around 30 fields.Now when I run the application initially the user would be on tab-1.At present all the data which is displayed in all the three tabs is pulled at once. Is there any way that when the user clicks on Tab-2 only then the data corresponding to tab-2 is loaded and the same for tab-3. I am looking for some function like "onTabClick". I checked the slots associated with the tab widget and could not find any.
Code for my form in Qt:
Mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <ctime>
#include <iostream>
#include <QPixmap>
#include <stdio.h>
#include <QProcess>
#include <QString>
#include <QPainter>
#include <QPen>
#include <QBrush>
#include <QLabel>
#include <QTimer>
#include <ctime>
using namespace std;
void MainWindow::onTabChanged(int tabIndex) {
cout<<"the tab index is:"<<tabIndex<<endl;
if (tabIndex == 0) {
// Create the first tab elements
cout<<"tab 0"<<endl;
} else if (tabIndex == 1) {
// ...
cout<<"tab 1"<<endl;
}
}
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
time_t now = time(0);
char* dt = ctime(&now);
ui->setupUi(this);
this->setWindowTitle("First Qt Project");
connect(ui->tabWidget, SIGNAL(currentChanged(int)), this, SLOT(onTabChanged(int)));
}
MainWindow::~MainWindow()
{
delete ui;
}
MainWindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private slots:
void onTabChanged(int tabIndex);
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
Upvotes: 1
Views: 2657
Reputation: 2442
I found the solution for this issue:
by default any slots which are created are under the section of "private slots" in the MainWindow.h file as shown below:
private slots:
//default slots created from the ui(when the user right clicks on a button and then connects to a slot)
So in my case since the function onTabChanges is a user defined function and I manually created a SLOT for this function this should be in public slots section. So including it this way resolved the issue.
public slots:
void onTabChanged(int tabIndex);
Upvotes: 0
Reputation: 21220
You can use QTabWidget::currentChanged(int)
signal to handle the tab changing event. I.e:
Connect signal with corresponding slot:
connect(tabWidget, SIGNAL(currentChanged(int)), this, SLOT(onTabChanged(int)));
and in the slot:
void MyClass::onTabChanged(int tabIndex) {
if (tabIndex == 0) {
// Create the first tab elements
} else if (tabIndex == 1) {
// ...
}
}
Upvotes: 1