QTableWidget中添加QComboBox/QPushButton控件并响应控件点击

本文介绍了如何在QT的QTableWidget中添加QComboBox和QPushButton控件,并展示了如何响应控件点击,获取选中行和列的信息。示例代码包括在QTableWidget中插入QComboBox,以及实现一个完整的带有QPushButton的MyTableWidget实例。这是一个基于带UI的QT程序,UI文件仅包含一个QTableWidget。示例效果展示了添加的控件在表格中的实际展示。
摘要由CSDN通过智能技术生成

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控件,然后进行栅格布局即可。

效果:

结尾:

只为记录,只为分享! 愿所写能对你有所帮助。不忘记点个赞,谢谢~

评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值