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. Access To QWidget declared in addTab() function.
Forum Update on Monday, May 27th 2025

Access To QWidget declared in addTab() function.

Scheduled Pinned Locked Moved Solved General and Desktop
15 Posts 3 Posters 652 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.
  • H Offline
    H Offline
    HenkCoder
    wrote on 14 Jul 2021, 18:53 last edited by HenkCoder
    #1

    Hello guys,
    I'm trying to create a simple notepad with tabs but I have a problem.
    I made a button to create a new Tab and put this code:

    ui->tabWidget->addTab(new QTextEdit(), QString("Tab " + QString::number(ui->tabWidget->count() + 1)));
    

    My question is, there is a way yo access the QTextEdit I declared in the addTab() function?

    1 Reply Last reply
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 14 Jul 2021, 18:54 last edited by
      #2

      Hi,

      QTabWidget::widget is one way.

      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
        mrjj
        Lifetime Qt Champion
        wrote on 14 Jul 2021, 19:07 last edited by
        #3

        Hi
        Do note that QTabWidget::widget returns the QTextEdit as a QWidget pointer so you need to use
        qobject_cast to make it a QTextEdit pointer and hence be able to use QTextEdit methods.

        H 1 Reply Last reply 14 Jul 2021, 20:13
        3
        • M mrjj
          14 Jul 2021, 19:07

          Hi
          Do note that QTabWidget::widget returns the QTextEdit as a QWidget pointer so you need to use
          qobject_cast to make it a QTextEdit pointer and hence be able to use QTextEdit methods.

          H Offline
          H Offline
          HenkCoder
          wrote on 14 Jul 2021, 20:13 last edited by
          #4

          @mrjj
          Hi, thank you for answering.
          How do I apply this? Sorry I'm new to this framework

          M 1 Reply Last reply 14 Jul 2021, 20:16
          0
          • H HenkCoder
            14 Jul 2021, 20:13

            @mrjj
            Hi, thank you for answering.
            How do I apply this? Sorry I'm new to this framework

            M Offline
            M Offline
            mrjj
            Lifetime Qt Champion
            wrote on 14 Jul 2021, 20:16 last edited by mrjj
            #5

            @HenkCoder
            Hi np. its like

            QTextEdit * edit = qobject_cast <QTextEdit*> ( ui->tabWidget->widget(0)) ;
            
            if (edit) { // check is important as if its not a QTextEdit then edit is null and app crash :)
            edit->xxxxx();
            }
            
            H 1 Reply Last reply 14 Jul 2021, 20:23
            2
            • S Offline
              S Offline
              SGaist
              Lifetime Qt Champion
              wrote on 14 Jul 2021, 20:17 last edited by
              #6

              More information in the function documentation.

              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 mrjj
                14 Jul 2021, 20:16

                @HenkCoder
                Hi np. its like

                QTextEdit * edit = qobject_cast <QTextEdit*> ( ui->tabWidget->widget(0)) ;
                
                if (edit) { // check is important as if its not a QTextEdit then edit is null and app crash :)
                edit->xxxxx();
                }
                
                H Offline
                H Offline
                HenkCoder
                wrote on 14 Jul 2021, 20:23 last edited by HenkCoder
                #7

                @mrjj
                Hey, what does that It statement do? And what do I have to write instead of xxxxx() or is it what I actually have to write?

                M 1 Reply Last reply 14 Jul 2021, 20:31
                0
                • H HenkCoder
                  14 Jul 2021, 20:23

                  @mrjj
                  Hey, what does that It statement do? And what do I have to write instead of xxxxx() or is it what I actually have to write?

                  M Offline
                  M Offline
                  mrjj
                  Lifetime Qt Champion
                  wrote on 14 Jul 2021, 20:31 last edited by mrjj
                  #8

                  Hi

                  Hey, what does that It statement do? And what do I have to write instead of xxxxx()?

                  Hi. this statement takes a base pointer (QWidget *) and cast it to the concrete child type.
                  In this case a QTextEdit.
                  This only works it IS a QTextEdit there.
                  So basically we do like

                  SomeClass * ptr = ( Otherclass *) ui->tabWidget->widget(0)
                  but in a saer manner that will fial its its not a QTextWidget.
                  A pure cast /old type cast will always work but crash the app if
                  it really not is a TextEdit

                  edit->xxxxx(); is just to show to call functions in it. xxxx is just like any function it has.

                  like
                  edit->toPlainText():

                  to get all text into QString or what ever you want to do with your textedit

                  H 1 Reply Last reply 14 Jul 2021, 20:39
                  1
                  • M mrjj
                    14 Jul 2021, 20:31

                    Hi

                    Hey, what does that It statement do? And what do I have to write instead of xxxxx()?

                    Hi. this statement takes a base pointer (QWidget *) and cast it to the concrete child type.
                    In this case a QTextEdit.
                    This only works it IS a QTextEdit there.
                    So basically we do like

                    SomeClass * ptr = ( Otherclass *) ui->tabWidget->widget(0)
                    but in a saer manner that will fial its its not a QTextWidget.
                    A pure cast /old type cast will always work but crash the app if
                    it really not is a TextEdit

                    edit->xxxxx(); is just to show to call functions in it. xxxx is just like any function it has.

                    like
                    edit->toPlainText():

                    to get all text into QString or what ever you want to do with your textedit

                    H Offline
                    H Offline
                    HenkCoder
                    wrote on 14 Jul 2021, 20:39 last edited by
                    #9

                    @mrjj
                    Ok thanks, but I have two more question.

                    1. Can declared that QTextEdit as a global pointer?
                    2. what does the '0' mean in ui->tabWidget->widget(0)?
                      That's it, no more question, thank you for your patience and sorry because I'm new!
                    S 1 Reply Last reply 14 Jul 2021, 20:41
                    0
                    • H HenkCoder
                      14 Jul 2021, 20:39

                      @mrjj
                      Ok thanks, but I have two more question.

                      1. Can declared that QTextEdit as a global pointer?
                      2. what does the '0' mean in ui->tabWidget->widget(0)?
                        That's it, no more question, thank you for your patience and sorry because I'm new!
                      S Offline
                      S Offline
                      SGaist
                      Lifetime Qt Champion
                      wrote on 14 Jul 2021, 20:41 last edited by
                      #10
                      1. no there's no need for a global variable, you can use a member variable but do not start using globals for that.

                      2. it's explained in the method documentation. It's the index of the widget you want to get.

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

                      H 1 Reply Last reply 14 Jul 2021, 20:50
                      1
                      • S SGaist
                        14 Jul 2021, 20:41
                        1. no there's no need for a global variable, you can use a member variable but do not start using globals for that.

                        2. it's explained in the method documentation. It's the index of the widget you want to get.

                        H Offline
                        H Offline
                        HenkCoder
                        wrote on 14 Jul 2021, 20:50 last edited by
                        #11

                        @SGaist
                        Oh ok, but I need it as a global, can I declare it like that?
                        Thank you for explaining me the widget(0) thing!

                        M 1 Reply Last reply 14 Jul 2021, 20:54
                        0
                        • H HenkCoder
                          14 Jul 2021, 20:50

                          @SGaist
                          Oh ok, but I need it as a global, can I declare it like that?
                          Thank you for explaining me the widget(0) thing!

                          M Offline
                          M Offline
                          mrjj
                          Lifetime Qt Champion
                          wrote on 14 Jul 2021, 20:54 last edited by mrjj
                          #12

                          @HenkCoder
                          Hi
                          Why would you need it as a global ?

                          If you plan something like NotePad++ with tabs then each tab will be its own QTextEdit
                          and you need to take the right one, depending on which tab user clicks.

                          So you should hook up a slot to the signal
                          https://doc.qt.io/qt-5/qtabwidget.html#currentChanged
                          and there you got the index (the parameter) to use to ask for the widget, then
                          convert it and you can alter text or what you want.

                          H 1 Reply Last reply 14 Jul 2021, 21:24
                          0
                          • M mrjj
                            14 Jul 2021, 20:54

                            @HenkCoder
                            Hi
                            Why would you need it as a global ?

                            If you plan something like NotePad++ with tabs then each tab will be its own QTextEdit
                            and you need to take the right one, depending on which tab user clicks.

                            So you should hook up a slot to the signal
                            https://doc.qt.io/qt-5/qtabwidget.html#currentChanged
                            and there you got the index (the parameter) to use to ask for the widget, then
                            convert it and you can alter text or what you want.

                            H Offline
                            H Offline
                            HenkCoder
                            wrote on 14 Jul 2021, 21:24 last edited by
                            #13

                            @mrjj
                            Hey, I need it as a global because I want to put other buttons like bold, italics, underline etc, but I cannot access the QTextEdit if it's not a global

                            M 1 Reply Last reply 15 Jul 2021, 06:42
                            0
                            • H HenkCoder
                              14 Jul 2021, 21:24

                              @mrjj
                              Hey, I need it as a global because I want to put other buttons like bold, italics, underline etc, but I cannot access the QTextEdit if it's not a global

                              M Offline
                              M Offline
                              mrjj
                              Lifetime Qt Champion
                              wrote on 15 Jul 2021, 06:42 last edited by
                              #14

                              @HenkCoder

                              Hi
                              You still dont need a global variable for that :)

                              Any button that must access the TextEdit can just get it.

                              like you cna make a helper function to make this easy

                              TextEdit * MainWindow::GetEditor() {
                                return q_objectcast<TextEdit *> ( ui->tabWidget->currentWidget() );
                              }
                              
                              then in BOLd button clicked etc , you can do
                              void BoldClicked() {
                              TextEdit * edit = GetEditor();  to have access to the current tab as a TextEdit
                              if (edit) {
                              make text bold ...
                              }
                              
                              }
                              
                              
                              
                              H 1 Reply Last reply 15 Jul 2021, 08:24
                              0
                              • M mrjj
                                15 Jul 2021, 06:42

                                @HenkCoder

                                Hi
                                You still dont need a global variable for that :)

                                Any button that must access the TextEdit can just get it.

                                like you cna make a helper function to make this easy

                                TextEdit * MainWindow::GetEditor() {
                                  return q_objectcast<TextEdit *> ( ui->tabWidget->currentWidget() );
                                }
                                
                                then in BOLd button clicked etc , you can do
                                void BoldClicked() {
                                TextEdit * edit = GetEditor();  to have access to the current tab as a TextEdit
                                if (edit) {
                                make text bold ...
                                }
                                
                                }
                                
                                
                                
                                H Offline
                                H Offline
                                HenkCoder
                                wrote on 15 Jul 2021, 08:24 last edited by
                                #15

                                @mrjj
                                Oooh, I understand.
                                Thank you all for the help!

                                1 Reply Last reply
                                0

                                1/15

                                14 Jul 2021, 18:53

                                • Login

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