QTable添加复选框
作者:互联网
效果
mainWindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QWidget>
#include <QListWidget>
#include <QStringList>
#include <QString>
#include <QDebug>
#include <QListWidgetItem>
#include <QGridLayout>
#include <QToolButton>
#include <QLineEdit>
#include <QRadioButton>
#include <QMainWindow>
QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
void initTableWidget();
void settingTableChanged(int row, int col);
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
mainWindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
connect(this->ui->tableWidget, SIGNAL(cellChanged(int row,int col)), this, SLOT(settingTableChanged(int row, int col)));
initTableWidget();
}
void MainWindow::initTableWidget()
{
ui->tableWidget->setRowCount(10);
ui->tableWidget->setColumnCount(10);
for(int i = 0;i < ui->tableWidget->rowCount ();i++)
{
for(int j=1;j<ui->tableWidget->columnCount ();j++){
ui->tableWidget->setEditTriggers ( QAbstractItemView::NoEditTriggers );//表格不可编辑
ui->tableWidget->setColumnWidth(j,ui->tableWidget->size ().width ()/1.8);
QTableWidgetItem *check=new QTableWidgetItem;
check->setCheckState (Qt::Checked);
ui->tableWidget->setItem(i,j,check); //插入复选框
}
QTableWidgetItem *check = new QTableWidgetItem("20%");
ui->tableWidget->setItem(i,16,check); //插入复选框
}
}
void MainWindow::settingTableChanged (int row, int col){
if(ui->tableWidget->item (row,col)->checkState ()==Qt::Checked){
// ui->setting_tableWidget->item(row,col)->setBackgroundColor (Qt::green);
ui->tableWidget->item(row,col)->setTextColor (Qt::green);
for(int j=1;j<16;j++)
{
if(j!=col){
ui->tableWidget->item(row,j)->setCheckState (Qt::Unchecked); //插入复选框
}
}
}
}
MainWindow::~MainWindow()
{
delete ui;
}
原文:https://blog.csdn.net/Fuel_Ming/article/details/121792322
标签:QTable,int,tableWidget,复选框,添加,ui,include,MainWindow,row 来源: https://www.cnblogs.com/xiaohai123/p/16317645.html