tableWidget->s" />
Qt中如何在QTableWidget中一个单元格插入多个按钮,如何正确获取插入的按钮的行列数 |
您所在的位置:网站首页 › 怎么添加单元格里的选项按钮 › Qt中如何在QTableWidget中一个单元格插入多个按钮,如何正确获取插入的按钮的行列数 |
一、在QTableWidget单元格中插入单个按钮,调用setCellWidget直接插入: QPushButton *btn = new QPushButton(); btn->setText(tr("查看")); ui->tableWidget->setCellWidget(0,4,btn);
方法2:使用frameGeometry来获取位置 QPushButton *btn = (QPushButton *)sender(); int x = btn->frameGeometry().x(); int y = btn->frameGeometry().y(); QModelIndex index = ui->tableWidget->indexAt(QPoint(x,y)); int row = index.row(); int col = index.column();2)有两个按钮时: 方法1:使用mapToParent来获取位置 QPushButton *btn = (QPushButton*)sender(); QWidget *w_parent = (QWidget*)btn->parent(); int x = w_parent->mapToParent(QPoint(0,0)).x(); int y = w_parent->mapToParent(QPoint(0,0)).y(); QModelIndex index = ui->tableWidget->indexAt(QPoint(x,y)); int row = index.row(); int col = index.column();方法2:使用frameGeometry来获取位置 QPushButton *btn = (QPushButton*)sender(); QWidget *w_parent = (QWidget*)btn->parent(); int x = w_parent->frameGeometry().x(); int y = w_parent->frameGeometry().y(); QModelIndex index = ui->tableWidget->indexAt(QPoint(x,y)); int row = index.row(); int col = index.column();工程源码下载地址:https://download.csdn.net/download/juzone/11959062 |
今日新闻 |
推荐新闻 |
CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3 |