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 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
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on 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
      • mrjjM Offline
        mrjjM Offline
        mrjj
        Lifetime Qt Champion
        wrote on 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
        3
        • mrjjM mrjj

          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 last edited by
          #4

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

          mrjjM 1 Reply Last reply
          0
          • H HenkCoder

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

            mrjjM Offline
            mrjjM Offline
            mrjj
            Lifetime Qt Champion
            wrote on 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
            2
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on 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
              • mrjjM mrjj

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

                mrjjM 1 Reply Last reply
                0
                • H HenkCoder

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

                  mrjjM Offline
                  mrjjM Offline
                  mrjj
                  Lifetime Qt Champion
                  wrote on 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
                  1
                  • mrjjM mrjj

                    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 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!
                    SGaistS 1 Reply Last reply
                    0
                    • H HenkCoder

                      @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!
                      SGaistS Offline
                      SGaistS Offline
                      SGaist
                      Lifetime Qt Champion
                      wrote on 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
                      1
                      • SGaistS SGaist
                        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 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!

                        mrjjM 1 Reply Last reply
                        0
                        • H HenkCoder

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

                          mrjjM Offline
                          mrjjM Offline
                          mrjj
                          Lifetime Qt Champion
                          wrote on 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
                          0
                          • mrjjM mrjj

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

                            mrjjM 1 Reply Last reply
                            0
                            • H HenkCoder

                              @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

                              mrjjM Offline
                              mrjjM Offline
                              mrjj
                              Lifetime Qt Champion
                              wrote on 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
                              0
                              • mrjjM mrjj

                                @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 last edited by
                                #15

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

                                1 Reply Last reply
                                0

                                • Login

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