3

QTabWidget has a property called currentTabName.

enter image description here

How can I access the currentTabName by code?

I need to check what tab is selected, but I can't use the tab text (tabText) because it is translatable and may change and I don't want to use the tab index (currentIndex) because the index may change in the future.

I'm using Qt 5.3

KelvinS
  • 2,870
  • 8
  • 34
  • 67

1 Answers1

3

As Chris Kawa answered here this is the object name of the current widget.

From code I can get it like this:

QString currentTabName = tabWidget->currentWidget()->objectName();

Note: As the doc suggest make sure to check for nullptr when using tabWidget->currentWidget().

KelvinS
  • 2,870
  • 8
  • 34
  • 67
  • 1
    Yeah, I just found this out when I found out they were using designer. The relevant source code is here. https://github.com/qt/qttools/blob/13946e382e7ab9418b4fe69a0b87a65d74c31bf4/src/designer/src/lib/shared/qdesigner_tabwidget.cpp#L443 – Alex Huszagh Jun 30 '17 at 21:27
  • 1
    Thanks Alexander. I have found the solution on the Qt forum. I didn't know that the `currentTabName` was the `objectName` from the `currentWidget`. – KelvinS Jun 30 '17 at 21:28