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.0k 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.
  • M Offline
    M Offline
    mzimmers
    wrote on 19 Jul 2018, 22:08 last edited by
    #1

    Hi all -

    I have a QTabWidget with four tabs, each containing a QPlainTextBox. How do I go about erasing the contents of the active tab? I've created a slot:

    void Widget::on_pushButtonClear_clicked()
    {
        int i;
        i = ui->tabWidget->currentIndex();
    }
    

    ...but I can't figure out how to:

    1. use this index to reference the active tab,
    2. clear a QPlainTextEdit (thought this would be easy, but didn't see anything in the page).

    Thanks for any guidance...

    1 Reply Last reply
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 19 Jul 2018, 22:11 last edited by
      #2

      Hi,

      If the QPlainTextEdit is the widget set on the tab, you can use QTabWidget::currentWidget and qobject_cast to cast the pointer to the correct class and then call clear.

      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
      2
      • M Offline
        M Offline
        mrjj
        Lifetime Qt Champion
        wrote on 19 Jul 2018, 22:13 last edited by
        #3

        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();

        M 1 Reply Last reply 19 Jul 2018, 22:14
        0
        • M mrjj
          19 Jul 2018, 22:13

          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();

          M Offline
          M Offline
          mzimmers
          wrote on 19 Jul 2018, 22:14 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.

          M 1 Reply Last reply 19 Jul 2018, 22:16
          0
          • M mzimmers
            19 Jul 2018, 22:14

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

            M Offline
            M Offline
            mrjj
            Lifetime Qt Champion
            wrote on 19 Jul 2018, 22:16 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
            • M Offline
              M Offline
              mzimmers
              wrote on 19 Jul 2018, 22:19 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

              M 1 Reply Last reply 19 Jul 2018, 22:21
              0
              • M mzimmers
                19 Jul 2018, 22:19

                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

                M Offline
                M Offline
                mrjj
                Lifetime Qt Champion
                wrote on 19 Jul 2018, 22:21 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
                • M Offline
                  M Offline
                  mzimmers
                  wrote on 19 Jul 2018, 22:26 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
                  • S Offline
                    S Offline
                    SGaist
                    Lifetime Qt Champion
                    wrote on 19 Jul 2018, 22:28 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

                    M 1 Reply Last reply 19 Jul 2018, 22:32
                    0
                    • S SGaist
                      19 Jul 2018, 22:28

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

                      M Offline
                      M Offline
                      mzimmers
                      wrote on 19 Jul 2018, 22:32 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 19 Jul 2018, 22:34 last edited by
                        #11

                        The QPlainText should be the first child.

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

                        M 1 Reply Last reply 19 Jul 2018, 22:42
                        0
                        • M mpergand
                          19 Jul 2018, 22:34

                          The QPlainText should be the first child.

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

                          M Offline
                          M Offline
                          mzimmers
                          wrote on 19 Jul 2018, 22:42 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?

                          M J.HilkJ 2 Replies Last reply 19 Jul 2018, 22:55
                          0
                          • M Offline
                            M Offline
                            mpergand
                            wrote on 19 Jul 2018, 22:48 last edited by
                            #13

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

                            M 1 Reply Last reply 19 Jul 2018, 22:53
                            0
                            • M mpergand
                              19 Jul 2018, 22:48

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

                              M Offline
                              M Offline
                              mrjj
                              Lifetime Qt Champion
                              wrote on 19 Jul 2018, 22:53 last edited by
                              #14

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

                              M 1 Reply Last reply 19 Jul 2018, 22:58
                              0
                              • M mzimmers
                                19 Jul 2018, 22:42

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

                                M Offline
                                M Offline
                                mrjj
                                Lifetime Qt Champion
                                wrote on 19 Jul 2018, 22:55 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
                                • M mrjj
                                  19 Jul 2018, 22:53

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

                                  M Offline
                                  M Offline
                                  mpergand
                                  wrote on 19 Jul 2018, 22:58 last edited by
                                  #16

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

                                  1 Reply Last reply
                                  0
                                  • M Offline
                                    M Offline
                                    mzimmers
                                    wrote on 19 Jul 2018, 22:58 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.

                                    M 1 Reply Last reply 19 Jul 2018, 23:01
                                    0
                                    • M mzimmers
                                      19 Jul 2018, 22:58

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

                                      M Offline
                                      M Offline
                                      mrjj
                                      Lifetime Qt Champion
                                      wrote on 19 Jul 2018, 23:01 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
                                      • M mzimmers
                                        19 Jul 2018, 22:42

                                        @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 20 Jul 2018, 05:52 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
                                        • S Offline
                                          S Offline
                                          SGaist
                                          Lifetime Qt Champion
                                          wrote on 20 Jul 2018, 06:11 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

                                          1/20

                                          19 Jul 2018, 22:08

                                          • Login

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