3

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?

Tami
  • 153
  • 2
  • 2
  • 11
  • Why don't you use [`QObject::findChildren`](http://doc.qt.io/qt-4.8/qobject.html#findChildren) to retrieve `QTableWidget` – Danh Dec 26 '15 at 06:48

1 Answers1

1

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 *>();
Danh
  • 5,916
  • 7
  • 30
  • 45
  • Thank you! I'm new with Qt and didn't see `findChildren()` in documetation of QTabWidget, so became quite confused :) – Tami Dec 26 '15 at 07:02
  • @Tami `qobject_cast` actually could be thought as `instanceof` operator in Java language. It's very handy in Qt! – frogatto Dec 26 '15 at 07:31
  • @Tami `findChildren` is a method of `QObject` – Danh Dec 26 '15 at 09:00
  • @Danh I understand, only meant that I didn't see it in the list of methods, which could be used by `QTabWidget` – Tami Dec 26 '15 at 15:16