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. [Solved]findChild<>() issue

[Solved]findChild<>() issue

Scheduled Pinned Locked Moved General and Desktop
7 Posts 3 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.
  • Y Offline
    Y Offline
    yhu420
    wrote on last edited by
    #1

    Hi everyone,
    It seems I have a problem with the findchild function, here are more infos:
    The compiling part is ok.
    The program launches.
    Aaand when I enter the slot, (with findchild), it crashes.
    So why do I affirm its because of findchild? Because i debbugged, its as simple as that.
    To be precise, I can get the pointer, but not use it :( .
    Here's some code
    @void Mainwin::connectClicked()
    {
    QTextEdit er = this->findChild<QTextEdit>("mainte");
    er->append(tr("<em>Tentative de connexion en cours...</em>"));
    this->findChild<QPushButton*>("connectbut")->setEnabled(false);

    socket->abort();
    socket->connectToHost(this->findChild<QLineEdit*>("servedit")->text(), this->findChild<QSpinBox*>("serportspin")->value());
    

    }@

    when I debug, the problem comes up on the 4th line (er->append...)
    Any reply is much appreciated

    1 Reply Last reply
    0
    • JeroentjehomeJ Offline
      JeroentjehomeJ Offline
      Jeroentjehome
      wrote on last edited by
      #2

      hi,
      Did you consider that 'er' could be zero when then QTextEdit doesn't have children??
      That will cause a crash.
      Greetz

      Greetz, Jeroen

      1 Reply Last reply
      0
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #3

        Also, you are searching for your child widgets with names. Did you set those names on the specific widgets ?

        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
        0
        • JeroentjehomeJ Offline
          JeroentjehomeJ Offline
          Jeroentjehome
          wrote on last edited by
          #4

          In my honest opinion it is not good to search children by name, this is not a object orientated way of thinking. The way you do it causes classes to have multiple dependencies and updates on a class effect all the other classes, but of course sometimes you must ;-)
          Greetz

          Greetz, Jeroen

          1 Reply Last reply
          0
          • Y Offline
            Y Offline
            yhu420
            wrote on last edited by
            #5

            Sorry for answering late this time I am bringing new pieces of information. If I qDebug() er, er is "QObject(0x0)" so yeah its nothing.
            and then i'm trying to get a pointer, I don't know if names work with these. Here's some more code:

            @Mainwin::Mainwin(QWidget *parent) : QWidget(parent)
            {
            //Things...
            QTextEdit *mainte = new QTextEdit;
            //things
            mainlayout->addWidget(mainte);
            setLayout(mainlayout);
            }@

            Itt's also maybe because of the layout...

            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              mainte is the name of the variable holding your QTextEdit, no the name of the QTextEdit.

              If you really want to find your QTextEdit with a name you have to call "setObjectName":http://qt-project.org/doc/qt-5.0/qtcore/qobject.html#objectName-prop

              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
              0
              • Y Offline
                Y Offline
                yhu420
                wrote on last edited by
                #7

                Ah ok thank you so much I just facepalmed myself reading this, thanks once again SGalst and all the others

                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