Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/137.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/qt/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ Qt:从QTableWidget中的按钮获取文本_C++_Qt_Qtablewidget_Qpushbutton - Fatal编程技术网

C++ Qt:从QTableWidget中的按钮获取文本

C++ Qt:从QTableWidget中的按钮获取文本,c++,qt,qtablewidget,qpushbutton,C++,Qt,Qtablewidget,Qpushbutton,我的Qt应用程序中有QTableWidget,我向其中添加了如下按钮: QPushButton *startButton = new QPushButton("start"); ui->tableWidget_tvConnection->setCellWidget(row, col, startButton); connect(startButton, SIGNAL(clicked()), this, SLOT(startButtonPressed())); 当它被按下时,我需要从

我的Qt应用程序中有QTableWidget,我向其中添加了如下按钮:

QPushButton *startButton = new QPushButton("start");
ui->tableWidget_tvConnection->setCellWidget(row, col, startButton);
connect(startButton, SIGNAL(clicked()), this, SLOT(startButtonPressed()));
当它被按下时,我需要从中获取文本,所以我尝试了以下方法:

void MainWindow::startButtonPressed()
{
    ...
    QPushButton *button = ui->tableWidget_tvConnection->cellWidget(row, col);
    qDebug() << button->text();
}
那么,甚至可以从QTableWidget中的按钮获取文本吗?
如果不是,我有另一种方法在我的应用程序中处理这个问题,但这将非常好。

您将获得
QWidget
,因此您应该将它转换为
QPushButton
。之后,您可以将此按钮用作正常的
按钮
。试试这个:

QPushButton *button = qobject_cast<QPushButton *>(ui->tableWidget_tvConnection->cellWidget(row, col));

if(button) {
    //success
} else {
    //bad
}
QPushButton*button=qobject\u cast(ui->tableWidget\u tvConnection->cellWidget(行、列));
如果(按钮){
//成功
}否则{
//坏的
}

СПааааа,чааа:)
QPushButton *button = qobject_cast<QPushButton *>(ui->tableWidget_tvConnection->cellWidget(row, col));

if(button) {
    //success
} else {
    //bad
}