从相应行的按钮中检索tableWidget行
Retrieve tableWidget row from button in corresponding row
我有一个tableWidget,我使用为其动态创建行
ui.tableWidget->insertRow(0);
QTableWidgetItem *newItemText = new QTableWidgetItem("bla", 0);
QPushButton *goButton = new QPushButton("Go", ui.tableWidget);
connect(goButton, SIGNAL(clicked()), this, SLOT(on_pushButtonGo_clicked_custom() ));
ui.tableWidget->setItem(0, 0, newItemText);
ui.tableWidget->setCellWidget(0, 1, goButton);
在每一行中,我都有一个单元格"Go"和一个包含按钮的单元格。这些按钮都连接到插槽on_pushButtonGo_clicked_custom。
现在,在on_pushButtonGo_clicked_custom中,我需要检索相应的行,在该行中单击了按钮。我该怎么做?
非常感谢你的帮助!
QSignalMapper允许将字符串或整数与原始信号对象相关联。这意味着您可以将每个goButton
与相应的行相关联。文档中的示例看起来与您的问题有关。
你需要修改你的代码像这样:
...
#include <QSignalMapper>
...
QSignalMapper * signalMapper = new QSignalMapper (this);
ui.tableWidget->insertRow(0);
QTableWidgetItem *newItemText = new QTableWidgetItem("bla", 0);
QPushButton *goButton = new QPushButton("Go", ui.tableWidget);
signalMapper->setMapping (goButton, 0); // where 0 is row number.
connect(goButton, SIGNAL(clicked()), signalMapper, SLOT(map()));
// You need rewrite slot for passing number of row.
connect(signalMapper, SIGNAL(mapped(const int)), this, SLOT(on_pushButtonGo_clicked_custom(const int)));
ui.tableWidget->setItem(0, 0, newItemText);
ui.tableWidget->setCellWidget(0, 1, goButton);
行数将通过on_pushButtonGo_clicked_custom(const int)
插槽的参数可用:
...
void MainWidget::on_pushButtonGo_clicked_custom(const int rowNumber)
{
qDebug () << "Row number is: " << rowNumber;
}
相关文章:
- 使用VerQueryValue检索应用程序的文件描述
- 是否可以从格式字符串中检索"width"
- 使用 pqxx 将 std::vector 存储在 postgresql 中,并从数据库中检索它
- 如何在QByteArray中放置和检索位字段而不会感到痛苦?
- 如何在Qt中从数据库中检索二进制数据?
- 如何从C++代码中检索 QML 的文本字段中的文本?
- 从 opencv c++ 中的矢量中检索固定的帧数
- 如何在不等待检索的情况下获取C++中的内存位置?
- 如何在 QTreeWidget 中检索特定项目的 mimeData?
- C++从字符串中检索几行
- 在数据库中插入和检索矩阵
- 两个垫子的 OpenCV 数据是相同的,但使用 Mat::at 检索时的值已损坏
- 使用 const char* 键映射 C++ 检索空值
- 打印从SQLite3数据库检索的数据C++与命令行中相同的方式
- 如何检索指向在单独线程上运行的函数的移动指针?
- 如何检索 Steamwork SDK 的当前版本
- 通过 host() 从 af::array 检索数据会导致错误的数据
- 无法从类中检索值
- 初始化类中的指针数组,并在另一个类中检索它
- 从相应行的按钮中检索tableWidget行