Access a tab widget in QTabWidget

I have a QTabWidget where each tab has a QPlainTextEdit as its widget. So, how do I access each tab widget? How to change this widget?

+4
source share
1 answer

You can use the widget function QTabWidget to get the widget with the specified tab index.

If QPlainTextEdit is the only widget on each tab page, then the returned widget will be like this. Otherwise, you need to get the children widget and find QPlainTextEdit in it.

 QPlainTextEdit* pTextEdit = NULL; QWidget* pWidget= ui->tabWidget->widget(1); // for the second tab // You can use metaobject to get widget type or qobject_cast 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]; } // Do whatever you want with it... ptextEdit->setPlainText("Updated Plain Text Edit); 
+10
source

Source: https://habr.com/ru/post/1388584/


All Articles