Qt World Summit: Register Today!

[Solved]findChild<>() issue

  • 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>"));

    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

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

  • Lifetime Qt Champion

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

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

  • 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)
    QTextEdit *mainte = new QTextEdit;

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

  • Lifetime Qt Champion

    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

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

Log in to reply