QT之QTableWidget实现单元格添加按钮和进度条,多次利⽤
信号槽
接上⼀个关于的博⽂,将QStackedWidget中的三个按钮替换成QTableWidget,并实现单元格⾥⾯添加按钮和进度条。
实现功能为:单元格⾥的按钮控制进度条的加减;
这⾥多次运⽤Qt经典的信号槽功能,希望可以帮助⼤家学习QStackedWidget控件的同时,更好的理解信号槽的⽤法;
先来看看效果
设计思路:
1、基于上⼀篇博⽂,我的QStackedWidget中,每⼀页有⼀个按钮,所以在这⾥将按钮放⼊QTableWidget中,在另外新建⼀个按钮,因此,每⼀页QStackedWidget包含两个按钮;
2、实现两个按钮控制进度条的加减操作,在这⾥将“加”和“减”分别交给上⼀步的两个按钮来实现;
3、将按钮和进度条添加进QTableWidget中,设置QTableWidget的表头(涉及样式表操作);
4、将做好的QTableWidget放⼊QStackedWidget;
5、搞定;
代码:
mainwindow.h⽂件
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QGridLayout>
#include <QWidget>
#include <QPushButton>
#include <QStackedWidget>
#include <QTableWidget>
#include <QProgressBar>
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = 0);
~
显示器排行MainWindow();
QWidget *pwind;
QPushButton *btnT1;//标题按钮,⽤于切换页⾯
QPushButton *btnT2;
QPushButton *btnT3;
煎肉怎么腌制QPushButton *btnT4;//QTableWidget控制进度条“加”
QPushButton *btnT5;
QPushButton *btnT6;
QProgressBar *m_Probar1;//进度条
QProgressBar *m_Probar2;
QProgressBar *m_Probar3;
QTableWidget *m_pTableWidget1;//每个页⾯建⽴⼀个QTableWidget QTableWidget *m_pTableWidget2;
QTableWidget *m_pTableWidget3;
QStackedWidget *m_pStackedWidget;
int pTick1;
int pTick2;
int pTick3;
public slots:
void switchPage(int nIndex);
void sss1();
void sss2();
void sss3();
void Progress1();//进度条“加”
void Progress2();
void Progress3();
void Progress11();//进度条“减”
void Progress22();
void Progress33();
};
#endif // MAINWINDOW_H
特别建⽴的信号槽来实现进度条的加减控制
mainwindow.cpp
#include "mainwindow.h"
#include <QLabel>
立体画教程#include <QTextEdit>
#include <QHeaderView>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
pwind = new QWidget;
tCentralWidget(pwind);
QPushButton *btnT1= new QPushButton("BTN1");
QPushButton *btnT2= new QPushButton("BTN2");
QPushButton *btnT3= new QPushButton("BTN3");
QLabel *pttt1= new QLabel("→",this);
QLabel *pttt2= new QLabel("→",this);
QPushButton *pText1= new QPushButton("OutBTN1");//将上⼀博⽂中建⽴的QTextEdit控件改为QPushButton QPushButton *pText2= new QPushButton("OutBTN2");
QPushButton *pText3= new QPushButton("OutBTN3");
m_pStackedWidget = new QStackedWidget();
btnT4 = new QPushButton("InBTN1");
btnT5 = new QPushButton("InBTN2");
btnT6 = new QPushButton("InBTN3");
pTick1 = 0;//设置进度条的初始值
pTick2 = 0;
pTick3 = 0;
m_Probar1 = new QProgressBar();//进度条初始化
m_Probar2 = new QProgressBar();
m_Probar3 = new QProgressBar();
//设置进度条样式
m_Probar1->tStyleSheet(
"QProgressBar {border: 2px solid grey; border-radius: 5px;"
"background-color: #FFFFFF;"
"text-align: center;}"
"QProgressBar::chunk {background-color: rgb(0,250,0) ;}"
);
m_Probar1->tValue(pTick1);//设置进度条的初始值
m_Probar2->tStyleSheet(
"QProgressBar {border: 2px solid grey; border-radius: 5px;"
"background-color: #FFFFFF;"
"text-align: center;}"
"QProgressBar::chunk {background-color: rgb(0,250,0) ;}"
);
m_Probar2->tValue(pTick2);
m_Probar3->tStyleSheet(
"QProgressBar {border: 2px solid grey; border-radius: 5px;"
"background-color: #FFFFFF;"
"text-align: center;}"
英国内政大臣
"QProgressBar::chunk {background-color: rgb(0,250,0) ;}"
);
助学金申请理由m_Probar3->tValue(pTick3);
///建⽴按钮与进度条的关系,信号槽,这⾥⽤到的是Qt5的格式;
connect(btnT4,&QPushButton::clicked,this,&MainWindow::Progress1);
connect(pText1,&QPushButton::clicked,this,&MainWindow::Progress11);
connect(btnT5,&QPushButton::clicked,this,&MainWindow::Progress2);
connect(pText2,&QPushButton::clicked,this,&MainWindow::Progress22);
应收账款质押登记
connect(btnT6,&QPushButton::clicked,this,&MainWindow::Progress3);
connect(pText3,&QPushButton::clicked,this,&MainWindow::Progress33);
m_pTableWidget1 = new QTableWidget();//QTableWidget的初始化
m_pTableWidget2 = new QTableWidget();
m_pTableWidget3 = new QTableWidget();
//设置QTableWidget为1⾏3列;
海天堂构
m_pTableWidget1->tRowCount(1);
m_pTableWidget1->tColumnCount(3);
//设置QTableWidget的标题栏数值;
m_pTableWidget1->tHorizontalHeaderLabels(QStringList()<<tr("信号加")<<tr("信号减")<<tr("实时状态"));
//设置QTableWidget的属性为⾃动填满空间,可以去掉这⾏代码看看效果;
m_pTableWidget1->horizontalHeader()->tSectionResizeMode(QHeaderView::Stretch);
//将QTableWidget的⾏标记不显⽰;
m_pTableWidget1->verticalHeader()->hide();
m_pTableWidget2->tRowCount(1);
m_pTableWidget2->tColumnCount(3);
m_pTableWidget2->tHorizontalHeaderLabels(QStringList()<<tr("Input")<<tr("Output")<<tr("实时状态")); m_pTableWidget2->horizontalHeader()->tSectionResizeMode(QHeaderView::Stretch);
m_pTableWidget2->verticalHeader()->hide();
m_pTableWidget3->tRowCount(1);
m_pTableWidget3->tColumnCount(3);
m_pTableWidget3->tHorizontalHeaderLabels(QStringList()<<tr("Input")<<tr("Output")<<tr("实时状态")); m_pTableWidget3->horizontalHeader()->tSectionResizeMode(QHeaderView::Stretch);
m_pTableWidget3->verticalHeader()->hide();
//将造好的按钮、进度条扔进QTableWidget⾥⾯,⼀定要先设置QTableWidget的⾏列数;
//QTableWidget添加按钮和进度条就是这么⼀个简单的命令
m_pTableWidget1->tCellWidget(0,0,btnT4);
m_pTableWidget1->tCellWidget(0,1,pText1);
m_pTableWidget1->tCellWidget(0,2,m_Probar1);
m_pTableWidget2->tCellWidget(0,0,btnT5);
m_pTableWidget2->tCellWidget(0,1,pText2);
m_pTableWidget2->tCellWidget(0,2,m_Probar2);
m_pTableWidget3->tCellWidget(0,0,btnT6);
m_pTableWidget3->tCellWidget(0,1,pText3);贫民窟的百万富翁
m_pTableWidget3->tCellWidget(0,2,m_Probar3);
//后⾯的代码就是⼀个框架了,详细见上⼀篇博⽂
m_pStackedWidget->addWidget(m_pTableWidget1);
m_pStackedWidget->addWidget(m_pTableWidget2);
m_pStackedWidget->addWidget(m_pTableWidget3);
QGridLayout *pLayout = new QGridLayout();
pLayout->addWidget(btnT1,0,0);
pLayout->addWidget(pttt1,0,1);
pLayout->addWidget(btnT2,0,2);
pLayout->addWidget(pttt2,0,3);
pLayout->addWidget(btnT3,0,4);
pLayout->addWidget(m_pStackedWidget,1,0,2,5);
pwind->tLayout(pLayout);
connect(btnT1, &QPushButton::clicked, this, &MainWindow::sss1);
connect(btnT2, &QPushButton::clicked, this, &MainWindow::sss2);
connect(btnT3, &QPushButton::clicked, this, &MainWindow::sss3);
}
MainWindow::~MainWindow()
{
}
void MainWindow::sss1()//这个地⽅我建⽴了int i,然后在调⽤i,这样做⿇烦吗?不,这样做显得我⽐较能装
{
int i = 0;
switchPage(i);
}
void MainWindow::sss2()
{
int i = 1;
switchPage(i);
}
void MainWindow::sss3()
{
int i = 2;
switchPage(i);
}
void MainWindow::switchPage(int nIndex)
{
m_pStackedWidget->tCurrentIndex(nIndex);
}
//接受到按钮的信号以后,这⾥开始控制进度条的加减。
void MainWindow::Progress1()
{
pTick1++;
m_Probar1->tValue(pTick1);
}
void MainWindow::Progress11()
{
pTick1--;
m_Probar1->tValue(pTick1);
}
void MainWindow::Progress2()
{
pTick2++;
m_Probar2->tValue(pTick2);
}
void MainWindow::Progress22()
{
pTick2--;
m_Probar2->tValue(pTick2);
}
void MainWindow::Progress3()
{
pTick3++;
m_Probar3->tValue(pTick3);
}
void MainWindow::Progress33()
{
pTick3--;
m_Probar3->tValue(pTick3);
}
博⽂内容不多,细节全在代码⾥⾯,如果有⼤佬觉得我这代码写得不好的,还请留⾔提意见,本⼩⽩先⾏谢过了。最后是千篇⼀律的main.cpp