3
votes

MainWindow of my Qt application has QTabWidget, where each tab is a QTableWidget. I need to get access to the selected cell of a current table (with currentRow() and currentColumn()). But when I'm taking pointer to the table with ui->tabWidget->currentWidget() result is QWidget* so for it methods like currentRow() don't exist.

Is there any way to determine that all pages of QTabWidget are members of QTableWidget class?

1
Why don't you use QObject::findChildren to retrieve QTableWidget - Danh

1 Answers

1
votes

You can use qobject_cast to check if an object of type QObject is an object of type T inherits from QObject

QWidget *widget = ui->tabWidget->currentWidget();
QTableWidget *tableWidget = qobject_cast<QTableWidget*>(widget);
if (tableWidget != 0)
{
    /// Do work
}

By the way, you can get all QTableWidget in you tab by

QList<QTableWidget *> allTables = ui->tabWidget->findChildren<QTableWidget *>();