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);
}
}