Search code examples
c++qtqwidget

How to get QTabWidget title text of QWidget in Qt?


I know I can use the widget function of QTabWidget in order to get the the QPlanTextEdit from the specified tab. But how can I get the tab title text of the current tab widget?

QPlainTextEdit* pTextEdit = NULL;
QWidget* pWidget= ui->tabWidget->widget(1);
if (pWidget->metaObject()->className() == "QPlainTextEdit")
    pTextEdit = (QPlainTextEdit*)pWidget;
else
{
    QList<QPlainTextEdit *> allTextEdits = pWidget->findChildren<QPlainTextEdit *>();
    if (allTextEdits.count() != 1)
    { 
        qError() << "Error";
        return;
    }  
    pTextEdit = allTextEdits[0];
}
ptextEdit->setPlainText("Updated Plain Text Edit);
// HERE I NEED THE CURRENT TAB'S TEXT!!

Solution

  • int index = ui->tabWidget->currentIndex();
    QString currentTabText = ui->tabWidget->tabText(index);