QT之QTableWidget实现单元格添加按钮和进度条,多次利用信号槽

接上一个关于QStackedWidget的博文,将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 
#include 
#include 
#include 
#include 
#include 
#include 
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 
#include 
#include 

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
{

    pwind = new QWidget;
    setCentralWidget(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->setStyleSheet(
                "QProgressBar {border: 2px solid grey;   border-radius: 5px;"
                "background-color: #FFFFFF;"
                "text-align: center;}"
                "QProgressBar::chunk {background-color: rgb(0,250,0) ;}"
                );
    m_Probar1->setValue(pTick1);//设置进度条的初始值

    m_Probar2->setStyleSheet(
                "QProgressBar {border: 2px solid grey;   border-radius: 5px;"
                "background-color: #FFFFFF;"
                "text-align: center;}"
                "QProgressBar::chunk {background-color: rgb(0,250,0) ;}"
                );
    m_Probar2->setValue(pTick2);

    m_Probar3->setStyleSheet(
                "QProgressBar {border: 2px solid grey;   border-radius: 5px;"
                "background-color: #FFFFFF;"
                "text-align: center;}"
                "QProgressBar::chunk {background-color: rgb(0,250,0) ;}"
                );
    m_Probar3->setValue(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->setRowCount(1);
    m_pTableWidget1->setColumnCount(3);
//设置QTableWidget的标题栏数值;
    m_pTableWidget1->setHorizontalHeaderLabels(QStringList()<horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
//将QTableWidget的行标记不显示;
    m_pTableWidget1->verticalHeader()->hide();
   


    m_pTableWidget2->setRowCount(1);
    m_pTableWidget2->setColumnCount(3);
    m_pTableWidget2->setHorizontalHeaderLabels(QStringList()<horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
    m_pTableWidget2->verticalHeader()->hide();

    m_pTableWidget3->setRowCount(1);
    m_pTableWidget3->setColumnCount(3);
    m_pTableWidget3->setHorizontalHeaderLabels(QStringList()<horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
    m_pTableWidget3->verticalHeader()->hide();


//将造好的按钮、进度条扔进QTableWidget里面,一定要先设置QTableWidget的行列数;
//QTableWidget添加按钮和进度条就是这么一个简单的命令

    m_pTableWidget1->setCellWidget(0,0,btnT4);
    m_pTableWidget1->setCellWidget(0,1,pText1);
    m_pTableWidget1->setCellWidget(0,2,m_Probar1);

    m_pTableWidget2->setCellWidget(0,0,btnT5);
    m_pTableWidget2->setCellWidget(0,1,pText2);
    m_pTableWidget2->setCellWidget(0,2,m_Probar2);

    m_pTableWidget3->setCellWidget(0,0,btnT6);
    m_pTableWidget3->setCellWidget(0,1,pText3);
    m_pTableWidget3->setCellWidget(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->setLayout(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->setCurrentIndex(nIndex);
}
//接受到按钮的信号以后,这里开始控制进度条的加减。
void MainWindow::Progress1()
{
    pTick1++;
    m_Probar1->setValue(pTick1);
}
void MainWindow::Progress11()
{
    pTick1--;
    m_Probar1->setValue(pTick1);
}
void MainWindow::Progress2()
{
    pTick2++;
    m_Probar2->setValue(pTick2);
}
void MainWindow::Progress22()
{
    pTick2--;
    m_Probar2->setValue(pTick2);
}
void MainWindow::Progress3()
{
    pTick3++;
    m_Probar3->setValue(pTick3);
}

void MainWindow::Progress33()
{
    pTick3--;
    m_Probar3->setValue(pTick3);
}

博文内容不多,细节全在代码里面,如果有大佬觉得我这代码写得不好的,还请留言提意见,本小白先行谢过了。

最后是千篇一律的main.cpp

#include "mainwindow.h"
#include 

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;
    w.show();

    return a.exec();
}

 

你可能感兴趣的:(QT之QTableWidget实现单元格添加按钮和进度条,多次利用信号槽)