Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. how to clear a QPlainTextBox within a QTabWidget?
Qt 6.12 LTS is out! See what's new in the release blog

how to clear a QPlainTextBox within a QTabWidget?

Scheduled Pinned Locked Moved Solved General and Desktop
20 Posts 5 Posters 4.3k Views 4 Watching
  • 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.
  • mzimmersM Offline
    mzimmersM Offline
    mzimmers
    wrote on last edited by
    #8

    I'm afraid I don't understand. How do I know which one to call the clear on? In other words, what do I do with the index that denotes which tab is displayed?

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #9

      You don't need it, as I suggested, you can get the widget directly.

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

      mzimmersM 1 Reply Last reply
      0
      • SGaistS SGaist

        You don't need it, as I suggested, you can get the widget directly.

        mzimmersM Offline
        mzimmersM Offline
        mzimmers
        wrote on last edited by
        #10

        @SGaist right, I can do this:

            QWidget *w ;
        ...
            w = ui->tabWidget->currentWidget();
        

        But what member/method of w gives me the child QPlainTextBox? That's the step I'm missing.

        1 Reply Last reply
        0
        • M Offline
          M Offline
          mpergand
          wrote on last edited by
          #11

          The QPlainText should be the first child.

          But, why not put the QPlainText directly as the tab widget ?

          mzimmersM 1 Reply Last reply
          0
          • M mpergand

            The QPlainText should be the first child.

            But, why not put the QPlainText directly as the tab widget ?

            mzimmersM Offline
            mzimmersM Offline
            mzimmers
            wrote on last edited by
            #12

            @mpergand I don't know how to do that, at least not with Designer.

            But this seems to work, though I'm not sure it's what SGaist had in mind:

            void Widget::on_pushButtonClear_clicked()
            {
                QWidget *w ;
                QPlainTextEdit *qpte;
            
                w = ui->tabWidget->currentWidget();
                qpte = qobject_cast<QPlainTextEdit *>(w->children().at(0));
                qpte->clear();
            }
            

            Comments?

            mrjjM J.HilkJ 2 Replies Last reply
            0
            • M Offline
              M Offline
              mpergand
              wrote on last edited by
              #13

              In Designer, it seems impossible to change the default widget added automaticaly.
              I'm missing something ?

              mrjjM 1 Reply Last reply
              0
              • M mpergand

                In Designer, it seems impossible to change the default widget added automaticaly.
                I'm missing something ?

                mrjjM Offline
                mrjjM Offline
                mrjj
                Lifetime Qt Champion
                wrote on last edited by
                #14

                @mpergand
                I think not. Only way i found was via promotion.

                M 1 Reply Last reply
                0
                • mzimmersM mzimmers

                  @mpergand I don't know how to do that, at least not with Designer.

                  But this seems to work, though I'm not sure it's what SGaist had in mind:

                  void Widget::on_pushButtonClear_clicked()
                  {
                      QWidget *w ;
                      QPlainTextEdit *qpte;
                  
                      w = ui->tabWidget->currentWidget();
                      qpte = qobject_cast<QPlainTextEdit *>(w->children().at(0));
                      qpte->clear();
                  }
                  

                  Comments?

                  mrjjM Offline
                  mrjjM Offline
                  mrjj
                  Lifetime Qt Champion
                  wrote on last edited by
                  #15

                  @mzimmers

                  Just in case u call on_pushButtonClear_clicked twice..

                  if ( w->children().size() ) {
                  qpte = qobject_cast<QPlainTextEdit *>(w->children().at(0));
                  ...
                  }

                  1 Reply Last reply
                  2
                  • mrjjM mrjj

                    @mpergand
                    I think not. Only way i found was via promotion.

                    M Offline
                    M Offline
                    mpergand
                    wrote on last edited by
                    #16

                    @mrjj
                    Infortunately, you cannot promote to an already existing widget, QPlainTextEdit here.
                    A pity ...

                    1 Reply Last reply
                    0
                    • mzimmersM Offline
                      mzimmersM Offline
                      mzimmers
                      wrote on last edited by
                      #17

                      Got it. Thanks for the assistance; perhaps the best lesson here is that there are still things one can't do in Designer.

                      mrjjM 1 Reply Last reply
                      0
                      • mzimmersM mzimmers

                        Got it. Thanks for the assistance; perhaps the best lesson here is that there are still things one can't do in Designer.

                        mrjjM Offline
                        mrjjM Offline
                        mrjj
                        Lifetime Qt Champion
                        wrote on last edited by
                        #18

                        @mzimmers
                        Well its pretty easy to setup in code if really needed.
                        I often mix both UI design and code.
                        Thats what i like the best with Qt. Its not either WYSIWYG and no code
                        or reverse. Its actually only code. ;)

                        1 Reply Last reply
                        0
                        • mzimmersM mzimmers

                          @mpergand I don't know how to do that, at least not with Designer.

                          But this seems to work, though I'm not sure it's what SGaist had in mind:

                          void Widget::on_pushButtonClear_clicked()
                          {
                              QWidget *w ;
                              QPlainTextEdit *qpte;
                          
                              w = ui->tabWidget->currentWidget();
                              qpte = qobject_cast<QPlainTextEdit *>(w->children().at(0));
                              qpte->clear();
                          }
                          

                          Comments?

                          J.HilkJ Offline
                          J.HilkJ Offline
                          J.Hilk
                          Moderators
                          wrote on last edited by
                          #19

                          @mzimmers said in how to clear a QPlainTextBox within a QTabWidget?:

                          void Widget::on_pushButtonClear_clicked()
                          {
                              QWidget *w ;
                              QPlainTextEdit *qpte;
                          
                              w = ui->tabWidget->currentWidget();
                              qpte = qobject_cast<QPlainTextEdit *>(w->children().at(0));
                              qpte->clear();
                          }
                          

                          because this is bound to fail as soon as the first child is not the targeted widget, or if it does not have children at all, I would suggest the following change:

                          void Widget::on_pushButtonClear_clicked()
                          {
                              QWidget *w ;
                              QPlainTextEdit *qpte;
                          
                              w = ui->tabWidget->currentWidget();
                              for(QObject *obj : w->children()){
                                   qpte = qobject_cast<QPlainTextEdit *>(obj);
                                   if(qpte)
                                        qpte->clear();
                              }   
                          
                          

                          Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                          Q: What's that?
                          A: It's blue light.
                          Q: What does it do?
                          A: It turns blue.

                          1 Reply Last reply
                          0
                          • SGaistS Offline
                            SGaistS Offline
                            SGaist
                            Lifetime Qt Champion
                            wrote on last edited by SGaist
                            #20

                            No need to make it so complicated: findChild is what you are looking for.

                            QWidget *w = ui->tabWidget->currentWidget();
                            QPlainTextEdit *qpte = w->findChild<QPlainTextEdit *>();
                            if (qpte) {
                                qpte->clear();
                            }
                            

                            Since the content of the widget is known, there's no real need for the if, it's to show good practice if you are doing "widget inspection".

                            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
                            4

                            • Login

                            • Login or register to search.
                            • First post
                              Last post
                            0
                            • Categories
                            • Recent
                            • Tags
                            • Popular
                            • Users
                            • Groups
                            • Search
                            • Get Qt
                            • Unsolved