Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access the widget of a tab in a QTabWidget

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

like image 527
Kazuma Avatar asked Dec 30 '11 10:12

Kazuma


People also ask

How do I add a tab in PYQT?

To add a tab to a QTabWidget , call the method . addTab() . label1 = QLabel("Widget in Tab 1.") label2 = QLabel("Widget in Tab 2.")

How do I change my Qt tab name?

If so, click on the tab you want to rename, then in the Property Editor (if you can't find it make sure it's visible by using the View->Property Editor menu item) scroll down to the bottom and look for the currentTabText property. You can change the tab's name right there.


1 Answers

You can use the widget function of QTabWidget in order to get the widget at the specified tab index.

If the QPlainTextEdit is the only widget of every tab page then the returned widget will be that. Otherwise you need to get the children of the widget and find the QPlainTextEdit in them.

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);
like image 145
pnezis Avatar answered Sep 24 '22 07:09

pnezis