floppydisk
floppydisk

Reputation: 248

Qt: get text from button in QTableWidget

I have QTableWidget in my Qt application, and I add buttons to it like that:

QPushButton *startButton = new QPushButton("start");
ui->tableWidget_tvConnection->setCellWidget(row, col, startButton);
connect(startButton, SIGNAL(clicked()), this, SLOT(startButtonPressed()));

And when it is pressed I need to get text from it, so I tried this:

void MainWindow::startButtonPressed()
{
    ...
    QPushButton *button = ui->tableWidget_tvConnection->cellWidget(row, col);
    qDebug() << button->text();
}

But compiler doesn't allow me to convert from QWidget* to QPushButton*:

error: invalid conversion from 'QWidget*' to 'QPushButton*' [-fpermissive]

So Is it even possible to get text from button inside QTableWidget? If it isn't I have another way how to handle this in my application, but this would be really nice.

Upvotes: 0

Views: 979

Answers (1)

Jablonski
Jablonski

Reputation: 18504

You get QWidget, so you should cast it to QPushButton. After that, you will be able to use this as a normal pushbutton. Try this:

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

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

Upvotes: 2

Related Questions