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. how to clear a QPlainTextBox within a QTabWidget?
Forum Updated to NodeBB v4.3 + New Features

how to clear a QPlainTextBox within a QTabWidget?

Scheduled Pinned Locked Moved Solved General and Desktop
20 Posts 5 Posters 2.1k 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.
  • mrjjM mrjj

    Hi
    Why not simply remove whole tab instead of "cleaning it"
    http://doc.qt.io/qt-5/qtabwidget.html#removeTab

    ui->tabWidget->removeTab ( ui->tabWidget->currentIndex() );

    and
    ui->plainTextEdit->clear();

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

    @mrjj I want to keep the tab because more stuff will be written to it. Clearing the contents is just a convenience for the user.

    mrjjM 1 Reply Last reply
    0
    • mzimmersM mzimmers

      @mrjj I want to keep the tab because more stuff will be written to it. Clearing the contents is just a convenience for the user.

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

      @mzimmers
      Ok, but is the page the actual plainTextEdit or does it have a parent QWidget?
      (as @SGaist asks)

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

        This is how I did it (but I'd be happy to change it if it would make this easier):
        0_1532038762624_tabs.PNG
        0_1532038786023_tabs2.PNG

        mrjjM 1 Reply Last reply
        0
        • mzimmersM mzimmers

          This is how I did it (but I'd be happy to change it if it would make this easier):
          0_1532038762624_tabs.PNG
          0_1532038786023_tabs2.PNG

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

          @mzimmers
          Ok so its not the page itself.
          Well, calling clear() on PlainTextWidget should make you happy.

          1 Reply Last reply
          0
          • 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 Extensions
                                    • Unsolved