Cant get Qtextedit from new tabs.



  • I am having problems getting child objects when I add a new tab. When getting QTextEdit from the ones made in designer it works correctly. What I am trying to do is create a new tab and access the QTextEdit . The problem I see is when creating the QTextEdit in code then adding it in a new tab, it isn't being created. Instead I see QWidget with a scroll area being added. In the program, the widgets look correct but there isn't a QTextEdit widget that can be accessed.

    void App::on_actionNew_triggered()
    {
        QTextEdit* pTextEdit = new QTextEdit;
        QString tabTitle = "Untitled" + QString::number(ui->tabWidget->count() + 1);
        ui->tabWidget->addTab(pTextEdit, tabTitle);
        ui->tabWidget->setCurrentIndex(ui->tabWidget->count() - 1);
        pTextEdit->setObjectName("textEdit_" + QString::number(ui->tabWidget->count() + 1));
        pTextEdit->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
        pTextEdit->setFontPointSize(11);
        pTextEdit->setFocus();
    
        QList<QWidget*> te = ui->tabWidget->widget(ui->tabWidget->currentIndex())->findChildren<QWidget*>();
        qDebug() << "New : " << ui->tabWidget->widget(ui->tabWidget->currentIndex())->children();
        qDebug() << "From Designer: " << ui->tabWidget->widget(0)->children();
    
    }
    

    the debug

    New :  (QWidget(0x155607a8, name = "qt_scrollarea_viewport"), QWidget(0x18cd8d30, name = "qt_scrollarea_hcontainer"), QWidgetTextControl(0x1552cb40), QWidget(0x18cd8ee0, name = "qt_scrollarea_vcontainer"))
    From Designer:  (QVBoxLayout(0x154dd8f0, name = "verticalLayout_3"), QTextEdit(0x154dc8b0, name = "textEdit_Document"))
    (QWidget(0x155607a8, name="qt_scrollarea_viewport"), QWidget(0x18cd8d30, name="qt_scrollarea_hcontainer"), QScrollBar(0x155606e8), QWidget(0x18cd8ee0, name="qt_scrollarea_vcontainer"), QScrollBar(0x155608e8))
    

  • Lifetime Qt Champion

    Hi and welcome to devnet,

    If you want to get the only QTextEdit then search for them directly rather than for QWidget.


  • Qt Champions 2016

    hi and welcome
    I made a default project and used your code with a button
    and it adds a new tab with a working text edit ?

    if i list the textedits, i see the new ones

     QList<QTextEdit*> te = ui->tabWidget->findChildren<QTextEdit*>();
      foreach (auto t, te) {
        qDebug() << t->objectName();
      }
    


  • @SGaist Sorry, didn't see that i left that Qwidget.

    EDIT: I think I may found the problem. When creating a new QTextEdit, it isn't parenting to the new tab but the tabwidget.



  • So, when creating a new tab with tabwidget.addTab(new QTextEdit, "Name") it parents it to a QstackedWidget. In designeer they are parented to a QWidget Tab.
    So what I need to figure out is how to access the QTextEdits that is in a tab. Each tab has a single QTextEdit widget in it. (Something like how tabs are in word.) I have combo boxes and actions that will need to change the selected text. Below is example code:

    void App::on_actionNew_triggered()
    {
        QTextEdit* pTextEdit = new QTextEdit;
        QString tabTitle = "Untitled" + QString::number(ui->tabWidget->count() + 1);
        ui->tabWidget->addTab(pTextEdit, tabTitle);
        ui->tabWidget->setCurrentIndex(ui->tabWidget->count() - 1);
        pTextEdit->setObjectName("textEdit_" + QString::number(ui->tabWidget->count() + 1));
    }
    
    void App::on_comboBox_FontFamily_currentTextChanged(const QString &arg1)
    {
        if (fontDataBase.hasFamily(ui->comboBox_FontFamily->currentText()))
        {
            // Get current tab and find textEdit child.
            QTextEdit* curTextEdit = NULL;
            QWidget* pWidget = ui->tabWidget->widget(ui->tabWidget->currentIndex());
    
            QList<QTextEdit*> allTextEdits = pWidget->findChildren<QTextEdit*>();
            if (allTextEdits.count() < 1)
            {
                return;
            }
    
            curTextEdit = allTextEdits[0];
    
            // Set selected text to Fontfamily.
            QTextCursor cursor = curTextEdit->textCursor();
            QTextCharFormat format = cursor.charFormat();
            format.setFontFamily(ui->comboBox_FontFamily->currentText());
            cursor.setCharFormat(format);
        }
    }
    

    This will work for tabs created in designer but not for tabs created by code.


  • Lifetime Qt Champion

    Since your QTextEdit is the widget that you put in the tab why not just use:

    QTextEdit *textEdit = qobject_cast<QTextEdit *>(ui->tabWidget->currentWidget());
    

    ?



  • Thinks for helping. The problem with this, is that it gets the new tabs but not the one created in designer. I guess a work around would just create all tabs through code only. What I manage to do is store the widgets in a struct Qlist for each appropriate tab. Your solution would be more optimal than what I came up with.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.