挺晚了,直接上代码了
.h部分
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include <QPushButton>
namespace Ui {
class Widget;
}
class Widget : public QWidget
{
Q_OBJECT
public:
explicit Widget(QWidget *parent = 0);
~Widget();
private slots:
void BtnTabSelect();
private:
Ui::Widget *ui;
QPushButton* pBtnClicked;
};
#endif // WIDGET_H
.cpp部分
#include "widget.h"
#include "ui_widget.h"
#include <QTabWidget>
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
ui->pushButton->setProperty("st",1);//设置索引和索引值
ui->pushButton_2->setProperty("st",0);
ui->pushButton_3->setProperty("st",0);
ui->pushButton_4->setProperty("st",0);
pBtnClicked = ui->pushButton;//初始选中为pushButton
connect(ui->pushButton,SIGNAL(clicked()),this,SLOT(BtnTabSelect()));
connect(ui->pushButton_2,SIGNAL(clicked()),this,SLOT(BtnTabSelect()));
connect(ui->pushButton_3,SIGNAL(clicked()),this,SLOT(BtnTabSelect()));
connect(ui->pushButton_4,SIGNAL(clicked()),this,SLOT(BtnTabSelect()));
}
void Widget::BtnTabSelect()
{
QPushButton* pBtn = qobject_cast<QPushButton*>(this->sender());
if(pBtn)
{
if(pBtn == ui->pushButton)
{
ui->tabWidget->setCurrentWidget(ui->tab);//设置为主窗口
if(pBtnClicked)
{
pBtnClicked->setProperty("st",1);
}
else {}
pBtnClicked = pBtn;
pBtn->setProperty("st",1);
}
else if(pBtn == ui->pushButton_2)
{
ui->tabWidget->setCurrentWidget(ui->tab2);//设置为主窗口
if(pBtnClicked)
{
pBtnClicked->setProperty("st",1);
}
else {}
pBtnClicked = pBtn;
pBtn->setProperty("st",1);
}
else if(pBtn == ui->pushButton_3)
{
ui->tabWidget->setCurrentWidget(ui->tab3);//设置为主窗口
if(pBtnClicked)
{
pBtnClicked->setProperty("st",1);
}
else {}
pBtnClicked = pBtn;
pBtn->setProperty("st",1);
}
else if(pBtn == ui->pushButton_4)
{
ui->tabWidget->setCurrentWidget(ui->tab4);//设置为主窗口
if(pBtnClicked)
{
pBtnClicked->setProperty("st",1);
}
else {}
pBtnClicked = pBtn;
pBtn->setProperty("st",1);
}
}
}
Widget::~Widget()
{
delete ui;
}
ui部分
做个记录,方便以后查看