QTableWidget中checkBox的高级应用实例

mainwindow.cpp

#include "mainwindow.h"

#include "ui_mainwindow.h"

#include "QDebug"

#include "QCheckBox"

MainWindow::MainWindow(QWidget *parent) :

QMainWindow(parent),

ui(new Ui::MainWindow)

{

ui->setupUi(this);

connect(ui->tableWidget,SIGNAL(cellClicked(int,int)),this,SLOT(changeTest(int, int)));

}

MainWindow::~MainWindow()

{

delete ui;

}

void MainWindow::changeEvent(QEvent *e)

{

QMainWindow::changeEvent(e);

switch (e->type()) {

case QEvent::LanguageChange:

ui->retranslateUi(this);

break;

default:

break;

}

}

void MainWindow::changeTest(int row, int col)

{

//QMessageBox::about(0,QString::number(row),QString::number(col));

qDebug()<<row<<col;

}//

void MainWindow::on_pushButton_clicked()

{

QString m=ui->lineEdit->text();

int z=m.toInt(0,10);

ui->tableWidget->setRowCount(z);

QString str;

QString numStr;

pBoxGrp = new QButtonGroup(this);

for(int m=0;m<z;m++)

{

// ui->tableWidget->setCellWidget(m,1,e);

numStr.clear();

numStr.clear();

numStr.setNum(m);

str = "chkbox" +numStr ;

QCheckBox *pBox =new QCheckBox(str);

pBox->setObjectName(str);

pBoxGrp->addButton(pBox,m);

pBoxGrp->setExclusive(false);

connect(pBoxGrp,SIGNAL(buttonClicked(int)),this,SLOT(slotBoxStateChanged(int)));

ui->tableWidget->setCellWidget(m,1,pBox);

}

}//当选中checke是第一列分别显示出相应的行号 没选中什么也不显示

void MainWindow::slotBoxStateChanged(int id)

{

QAbstractButton *pButton =pBoxGrp->button(id);

QCheckBox *pBox =(QCheckBox*)pButton;

if(pBox->checkState()==Qt::Checked)

{

QString str;

int nNum =id +1;

str.setNum(nNum);

ui->tableWidget->setItem(id,0,new QTableWidgetItem(str));

}

else

{

ui->tableWidget->takeItem(id,0);

}

}

对应.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include "QCheckBox"
#include "QButtonGroup"
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow {
Q_OBJECT
public:
MainWindow(QWidget *parent = 0);
~MainWindow();
protected:
void changeEvent(QEvent *e);
private:
Ui::MainWindow *ui;
QButtonGroup *pBoxGrp;
private slots:
void on_pushButton_clicked();
void changeTest(int,int);
void slotBoxStateChanged(int);
};
class chekcbox :public QCheckBox{
public :
int m;
};
#endif // MAINWINDOW_H
QTableWidget中checkBox的高级应用实例

你可能感兴趣的:(QTableWidget)