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. find child control
Forum Updated to NodeBB v4.3 + New Features

find child control

Scheduled Pinned Locked Moved Unsolved General and Desktop
16 Posts 4 Posters 4.8k Views 1 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.
  • S Offline
    S Offline
    sashapont
    wrote on last edited by sashapont
    #1

    I create QTextEdit dynamically and what to get it next, but have app crash& Please told me where is error?

     QTextEdit *O1Date= new QTextEdit("10.06.2016");
     O1Date->setObjectName("O1Date");
     QGraphicsScene *scene = new QGraphicsScene;
                  QGraphicsProxyWidget *proxy = new QGraphicsProxyWidget();
                  proxy->setWidget(O1Date);
     scene->addItem(proxy);
     ui->graphicsView->setScene(scene);
    
    

    And then
    QTextEdit* O1Date1 = ui->graphicsView->findChild<QTextEdit*>("O1Date");

    Joel BodenmannJ 1 Reply Last reply
    0
    • S sashapont

      I create QTextEdit dynamically and what to get it next, but have app crash& Please told me where is error?

       QTextEdit *O1Date= new QTextEdit("10.06.2016");
       O1Date->setObjectName("O1Date");
       QGraphicsScene *scene = new QGraphicsScene;
                    QGraphicsProxyWidget *proxy = new QGraphicsProxyWidget();
                    proxy->setWidget(O1Date);
       scene->addItem(proxy);
       ui->graphicsView->setScene(scene);
      
      

      And then
      QTextEdit* O1Date1 = ui->graphicsView->findChild<QTextEdit*>("O1Date");

      Joel BodenmannJ Offline
      Joel BodenmannJ Offline
      Joel Bodenmann
      wrote on last edited by
      #2

      Where exactly crashes your application? Did you check the pointer returned by findChild() before you access/use it?

      Industrial process automation software: https://simulton.com
      Embedded Graphics & GUI library: https://ugfx.io

      1 Reply Last reply
      0
      • S Offline
        S Offline
        sashapont
        wrote on last edited by
        #3

        Yes^ it is not accessible....

        Joel BodenmannJ 1 Reply Last reply
        0
        • S sashapont

          Yes^ it is not accessible....

          Joel BodenmannJ Offline
          Joel BodenmannJ Offline
          Joel Bodenmann
          wrote on last edited by
          #4

          I just checked the documentation. I'm not an expert so please don't take what I'm saying for granted...
          First of all, QGraphicsView::setScene() doesn't make the scene become a child of the QGraphicsView. Also, QGraphicsProxyWidget::setWidget() doesn't make the widget become a child of QGraphicsProxyWidget. Hence you will never find your widget by calling findChild() on your view.

          So either you store a pointer to your QTextEdit widget yourself or you use QGraphicsProxyWidget::widget() to access it.

          Again: I might be wrong and there might be a better/proper solution to this.

          Industrial process automation software: https://simulton.com
          Embedded Graphics & GUI library: https://ugfx.io

          1 Reply Last reply
          0
          • VRoninV Offline
            VRoninV Offline
            VRonin
            wrote on last edited by
            #5

            The problem here is you do not give a parent to O1Date
            QTextEdit *O1Date= new QTextEdit("10.06.2016");
            should become
            QTextEdit *O1Date= new QTextEdit("10.06.2016",ui->graphicsView);

            "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
            ~Napoleon Bonaparte

            On a crusade to banish setIndexWidget() from the holy land of Qt

            S 1 Reply Last reply
            1
            • Joel BodenmannJ Offline
              Joel BodenmannJ Offline
              Joel Bodenmann
              wrote on last edited by
              #6

              @VRonin said:

              The problem here is you do not give a parent to O1Date

              Well, look at that. I missed the obvious solution...

              Industrial process automation software: https://simulton.com
              Embedded Graphics & GUI library: https://ugfx.io

              1 Reply Last reply
              0
              • VRoninV VRonin

                The problem here is you do not give a parent to O1Date
                QTextEdit *O1Date= new QTextEdit("10.06.2016");
                should become
                QTextEdit *O1Date= new QTextEdit("10.06.2016",ui->graphicsView);

                S Offline
                S Offline
                sashapont
                wrote on last edited by
                #7

                @VRonin said:

                should become
                QTextEdit *O1Date= new QTextEdit("10.06.2016",ui->graphicsView);

                In this case i cannot rotate it in graphic view :(

                kshegunovK 1 Reply Last reply
                0
                • S sashapont

                  @VRonin said:

                  should become
                  QTextEdit *O1Date= new QTextEdit("10.06.2016",ui->graphicsView);

                  In this case i cannot rotate it in graphic view :(

                  kshegunovK Offline
                  kshegunovK Offline
                  kshegunov
                  Moderators
                  wrote on last edited by kshegunov
                  #8

                  @sashapont
                  Tracing the inheritance tree, I'd look for the text edit in the scene, not in the view.

                  QTextEdit *O1Date= new QTextEdit("10.06.2016");
                  O1Date->setObjectName("O1Date");
                  QGraphicsScene *scene = new QGraphicsScene;
                  QGraphicsProxyWidget *proxy = new QGraphicsProxyWidget();
                  proxy->setWidget(O1Date);
                  scene->addItem(proxy);
                   ui->graphicsView->setScene(scene);
                  

                  Then:

                  ui->graphicsView->scene()->findChild<QTextEdit *>("O1Date");
                  

                  In any case I'd think the most simplest solution is to just keep a pointer to your item. E.g.

                  QPointer<QTextEdit> O1Date; //< This goes into the header file
                  
                  O1Date = new QTextEdit("10.06.2016"); //< This goes into the source file
                  

                  EDIT:
                  Although the scene uses it's own child tree as can be seen here.

                  Read and abide by the Qt Code of Conduct

                  S 1 Reply Last reply
                  1
                  • kshegunovK kshegunov

                    @sashapont
                    Tracing the inheritance tree, I'd look for the text edit in the scene, not in the view.

                    QTextEdit *O1Date= new QTextEdit("10.06.2016");
                    O1Date->setObjectName("O1Date");
                    QGraphicsScene *scene = new QGraphicsScene;
                    QGraphicsProxyWidget *proxy = new QGraphicsProxyWidget();
                    proxy->setWidget(O1Date);
                    scene->addItem(proxy);
                     ui->graphicsView->setScene(scene);
                    

                    Then:

                    ui->graphicsView->scene()->findChild<QTextEdit *>("O1Date");
                    

                    In any case I'd think the most simplest solution is to just keep a pointer to your item. E.g.

                    QPointer<QTextEdit> O1Date; //< This goes into the header file
                    
                    O1Date = new QTextEdit("10.06.2016"); //< This goes into the source file
                    

                    EDIT:
                    Although the scene uses it's own child tree as can be seen here.

                    S Offline
                    S Offline
                    sashapont
                    wrote on last edited by
                    #9

                    @kshegunov said:

                    ui->graphicsView->scene()->findChild<QTextEdit *>("O1Date");

                    Not working :( app crashes

                    kshegunovK 1 Reply Last reply
                    0
                    • S sashapont

                      @kshegunov said:

                      ui->graphicsView->scene()->findChild<QTextEdit *>("O1Date");

                      Not working :( app crashes

                      kshegunovK Offline
                      kshegunovK Offline
                      kshegunov
                      Moderators
                      wrote on last edited by
                      #10

                      @sashapont

                      ui->graphicsView->scene()
                      

                      Did you make sure this is not NULL?

                      Read and abide by the Qt Code of Conduct

                      1 Reply Last reply
                      0
                      • S Offline
                        S Offline
                        sashapont
                        wrote on last edited by
                        #11

                        I see textedit, and that rotated^ that scene is not NULL

                        kshegunovK 1 Reply Last reply
                        0
                        • S sashapont

                          I see textedit, and that rotated^ that scene is not NULL

                          kshegunovK Offline
                          kshegunovK Offline
                          kshegunov
                          Moderators
                          wrote on last edited by
                          #12

                          @sashapont
                          I'm sorry I don't follow. Do you mind uploading a screen shot and a stack trace of the crash. Also the exact type of the crash would be helpful.

                          Read and abide by the Qt Code of Conduct

                          1 Reply Last reply
                          0
                          • S Offline
                            S Offline
                            sashapont
                            wrote on last edited by
                            #13

                            Thats all project
                            untitled

                            kshegunovK 1 Reply Last reply
                            0
                            • S sashapont

                              Thats all project
                              untitled

                              kshegunovK Offline
                              kshegunovK Offline
                              kshegunov
                              Moderators
                              wrote on last edited by
                              #14

                              @sashapont
                              Your app crashes because you don't handle the case when the line edit is not found.

                              QTextEdit * O1Date1 = ui->graphicsView->scene()->findChild<QTextEdit *>("O1Date");
                              

                              is NULL and you're directly calling:

                              O1Date1->setText("");
                              

                              Which brings me to my original point - use a pointer to your widget instead of recursively searching through the object tree.

                              Read and abide by the Qt Code of Conduct

                              1 Reply Last reply
                              0
                              • S Offline
                                S Offline
                                sashapont
                                wrote on last edited by sashapont
                                #15

                                @kshegunov said:

                                QPointer<QTextEdit> O1Date;

                                When I try to write that in header file i have error

                                implicit instantiation of undefined template 'QPointer<QTextEdit>'
                                QPointer<QTextEdit> O1Date;
                                ^

                                I try to use static variables in header and it works thank you!

                                But i have new issue
                                Now i can set value to the text edit^ And in qDebug I see that it set, but in control i see old value... How i can redraw that?

                                kshegunovK 1 Reply Last reply
                                0
                                • S sashapont

                                  @kshegunov said:

                                  QPointer<QTextEdit> O1Date;

                                  When I try to write that in header file i have error

                                  implicit instantiation of undefined template 'QPointer<QTextEdit>'
                                  QPointer<QTextEdit> O1Date;
                                  ^

                                  I try to use static variables in header and it works thank you!

                                  But i have new issue
                                  Now i can set value to the text edit^ And in qDebug I see that it set, but in control i see old value... How i can redraw that?

                                  kshegunovK Offline
                                  kshegunovK Offline
                                  kshegunov
                                  Moderators
                                  wrote on last edited by kshegunov
                                  #16

                                  @sashapont said:

                                  implicit instantiation of undefined template 'QPointer<QTextEdit>'

                                  Have you forgotten to include the appropriate header? That is:

                                  #include <QPointer>
                                  

                                  Now i can set value to the text edit^ And in qDebug I see that it set, but in control i see old value... How i can redraw that?

                                  Issue an update request to the graphics view:

                                   ui->graphicsView->update();
                                  

                                  Although, I believe it the change of data should be reflected automatically.

                                  Read and abide by the Qt Code of Conduct

                                  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