QTableWidget是QT程序中常用的显示数据表格的空间,里面不仅仅可以添加文字,也可以添加控件,图片等等,此处以添加QCombobox和QPushButton举例,点击选择控件以后,可以获得哪一行哪一列的信息。
主要是以下几行代码:
//创建一个QComboBox控件
QComboBox *comBox_ = new QComboBox();
//设置QComboBox的item
comBox_->addItem("A");
comBox_->addItem("B");
comBox_->addItem("C");
//建立信号槽
connect(comBox_, SIGNAL(currentIndexChanged(QString)), this,SLOT(clickCombobox(QString)));
//将QComboBox控件设置到QTableWidget中
ui->tableWidget->setCellWidget(0, 0 ,comBox_);
//槽函数
void MyTableWidget::clickCombobox(QString text)
{
QComboBox *comBox_ = dynamic_cast<QComboBox*>(this->sender());
if(NULL == comBox_)
{
return;
}
int x = comBox_->frameGeometry().x();
int y = comBox_->frameGeometry().y();
QModelIndex index = ui->tableWidget->indexAt(QPoint(x, y));
int row = index.row();
int column = index.column();
qDebug() << "选择第几行:" << row << "选择第几列:" << column;
qDebug() << "选择内容:" << text;
//接下来自定义要处理的
//。。。。。。
}
上面是在QTableWidget中添加QCombobox的几行示例,完整MyTableWidget.cpp代码如下,包含添加QPushButton按键:
#include "MyTableWidget.h"
#include "ui_MyTableWidget.h"
#include <QComboBox>
#include <QPushButton>
#include <QDebug>
MyTableWidget::MyTableWidget(QWidget *parent) :
QWidget(parent),
ui(new Ui::MyTableWidget)
{
ui->setupUi(this);
//设置行数(此处为5)
ui->tableWidget->setRowCount(5);
//设置列数(此处为2)
ui->tableWidget->setColumnCount(2);
//设置充满表格宽度
ui->tableWidget->horizontalHeader()->setStretchLastSection(true);
//设置列表内容
setTableWidgetData();
}
MyTableWidget::~MyTableWidget()
{
delete ui;
}
void MyTableWidget::setTableWidgetData()
{
for(int i = 0 ; i < 5 ; i ++)
{
//创建一个QComboBox控件
QComboBox *comBox_ = new QComboBox();
//设置QComboBox的item
comBox_->addItem("A");
comBox_->addItem("B");
comBox_->addItem("C");
//建立信号槽
connect(comBox_, SIGNAL(currentIndexChanged(QString)), this, SLOT(clickCombobox(QString)));
//将QComboBox控件设置到QTableWidget中
ui->tableWidget->setCellWidget(i, 0 ,comBox_);
//创建一个QPushButton控件
QPushButton *pushButton_ = new QPushButton();
//设置按键显示文字
pushButton_->setText("点击");
//建立信号槽
connect(pushButton_, SIGNAL(clicked(bool)), this, SLOT(clickButton()));
//将QPushButton控件设置到QTableWidget中
ui->tableWidget->setCellWidget(i, 1 ,pushButton_);
}
}
void MyTableWidget::clickCombobox(QString text)
{
QComboBox *comBox_ = dynamic_cast<QComboBox*>(this->sender());
if(NULL == comBox_)
{
return;
}
int x = comBox_->frameGeometry().x();
int y = comBox_->frameGeometry().y();
QModelIndex index = ui->tableWidget->indexAt(QPoint(x, y));
int row = index.row();
int column = index.column();
qDebug() << "选择第几行:" << row << "选择第几列:" << column;
qDebug() << "选择内容:" << text;
//接下来自定义要处理的
//。。。。。。
}
void MyTableWidget::clickButton()
{
QPushButton *pushButton_ = dynamic_cast<QPushButton*>(this->sender());
if(NULL == pushButton_)
{
return;
}
int x = pushButton_->frameGeometry().x();
int y = pushButton_->frameGeometry().y();
QModelIndex index = ui->tableWidget->indexAt(QPoint(x, y));
int row = index.row();
int column = index.column();
qDebug() << "选择第几行:" << row << "选择第几列:" << column;
//接下来自定义要处理的
//。。。。。。
}
注:本Demo中采用的是带UI的,即包含MyTableWidget.ui,该ui中只拖入一个QTableWidget控件,然后进行栅格布局即可。
效果:
结尾:
只为记录,只为分享! 愿所写能对你有所帮助。不忘记点个赞,谢谢~