Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Cant get Qtextedit from new tabs.
QtWS25 Last Chance

Cant get Qtextedit from new tabs.

Scheduled Pinned Locked Moved Unsolved General and Desktop
7 Posts 3 Posters 2.1k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    MavenC
    wrote on 1 Apr 2016, 22:01 last edited by
    #1

    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))
    
    1 Reply Last reply
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 1 Apr 2016, 22:52 last edited by
      #2

      Hi and welcome to devnet,

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

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      M 1 Reply Last reply 1 Apr 2016, 23:06
      0
      • M Offline
        M Offline
        mrjj
        Lifetime Qt Champion
        wrote on 1 Apr 2016, 22:55 last edited by mrjj 4 Jan 2016, 23:12
        #3

        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();
          }
        
        1 Reply Last reply
        0
        • S SGaist
          1 Apr 2016, 22:52

          Hi and welcome to devnet,

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

          M Offline
          M Offline
          MavenC
          wrote on 1 Apr 2016, 23:06 last edited by MavenC 4 Feb 2016, 01:59
          #4

          @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.

          1 Reply Last reply
          0
          • M Offline
            M Offline
            MavenC
            wrote on 2 Apr 2016, 02:40 last edited by MavenC 4 Feb 2016, 02:45
            #5

            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.

            1 Reply Last reply
            0
            • S Offline
              S Offline
              SGaist
              Lifetime Qt Champion
              wrote on 2 Apr 2016, 21:19 last edited by
              #6

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

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

              ?

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              1
              • M Offline
                M Offline
                MavenC
                wrote on 2 Apr 2016, 22:19 last edited by
                #7

                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.

                1 Reply Last reply
                0

                4/7

                1 Apr 2016, 23:06

                • Login

                • Login or register to search.
                4 out of 7
                • First post
                  4/7
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved