undefined reference .... to custom slot



  • Hi again

    I have a problem with a custom slot. When I try compiling I get this..

    /home/laurie/NetBeansProjects/QT_Learning2/build-QTDesigner-Desktop_Qt_5_2_1_GCC_64bit-Debug/moc_mainheader.cpp:67: error: undefined reference to `learningbox::updateMe()'

    This is the header file
    @#include <QObject>

    class learningbox : public QObject
    {
    Q_OBJECT
    public:
    learningbox();
    public slots:
    void updateMe();
    };@

    and this is the class (I've left out some lines that aren't relevant here)
    @
    void learningbox()
    {
    QWidget mybox;
    QTextEdit words;
    QPushButton mybutton("Push me", &mybox);

    words.setParent(&mybox);
    words.setHtml("Hello this is some text2");      
           
    QHBoxLayout *mylayout = new QHBoxLayout;
        
    mybox.setWindowTitle("My textbox");
    
    QObject::connect (&mybutton, SIGNAL(clicked()), qApp, SLOT(updateMe()));
    
    mybox.setLayout(mylayout);
    mybox.layout()->setSizeConstraint(QLayout::SetDefaultConstraint);
    
    mybox.show();
    qApp->exec&#40;&#41;;
    

    return;
    }
    void updateMe()
    {
    std::cout<<"Button clicked";

    }
    @

    I'm sure this is something simple but I've been scratching my head for days.

    I've done a clean build, rerun QMAKE and everything else.

    I also tried changing
    @void updateMe()@

    to
    @void learningbox::updateMe()@

    but that failed to compile with

    /home/laurie/NetBeansProjects/QT_Learning2/QTDesigner/classes.cpp:41: error: 'learningbox' is not a class or namespace
    void learningbox::updateMe()
    ^
    What have I missed?

    Cheers

    Sam


  • Moderators

    These are class constructor and a method. What you have in your .cpp are two free-standing functions. Also a constructor in C++ doesn't have a return type (void is a type too).

    In your .cpp change
    @void learningbox()@
    to
    @learningbox::learningbox()@
    and
    @void updateMe()@
    to
    @void learningbox::updateMe()@

    The second error you're getting means the copiler can't see learningbox declaration. Did you forget to #include the header in your .cpp?

    Also, since you're inheriting QObject your constructor should probably be something like
    @learningbox(QObject* parent = nullptr)@
    and then definition:
    @learningbox::learningbox(QObject* parent) : QObject(parent) { ...@



  • Oh no, I'd left the <#include> out. How could I have forgotten!.

    Hmm, yes I couldn't work out how to "join" the two functions together. A bit more research I think.

    I've been an MS developer for years (VB and all that) but I've just started non-MS programming as a hobby. Very interesting to realise how much MS automates things, but limits you at the same time.

    In comparison C++ and QT feels like "proper" programming".

    Thanks for this

    Sam


  • Moderators

    If you're using a decent IDE (like Qt Creator) it can help you with the small stuff like that.
    For example when you hit "alt+enter":http://qt-project.org/doc/qtcreator-3.1/creator-editor-refactoring.html#applying-refactoring-actions with the cursor at the declaration in the header it will allow you to insert the right implementation code in the .cpp automatically.



  • Hi

    I am using Qt Creator but I didn't know that little trick. I'll give it a whirl when I next get to play.

    Thanks for your help on this

    Sam



  • Hi, I just wanted to add one more thing, your code has so many weird bugs you might have missed the bug in the connect call:
    @
    QObject::connect (&mybutton, SIGNAL(clicked()), qApp, SLOT(updateMe()));
    @
    the 3rd parameter should be the object of the slot you want to connect the signal to, so qApp is not correct here I think (qApp is a global pointer to your QApplication, which has no updateMe() slot), you have to use a pointer to your "learningbox" object. :)


  • Moderators

    Yeah, Xander84 is right.
    I didn't even bother to read the contents of the constructor but now that I did it indeed looks like you're trying to write VB code with C++ syntax ;)
    Basically the whole learningbox() function is wrong top to bottom.
    learningbox should probably be a QWidget not QObject and you shouldn't run application loop (exec()) inside of its constructor. The textedit and the button should be created dynamically (with new) and put in the layout. The way you have it now the textedit won't be shown and the button will crash on destruction because its parent will try to destroy it too. You don't need a return statement in a void function or constructor either.

    I'd suggest to look at some of the Qt examples bundled in the installed package and maybe try some pure C++(without Qt) programs first.



  • Haha, you could be right about doing VB in C++ .... old habits are hard to break. Only been doing this a couple of weeks now and again so I'm surely allowed to be a bit rubbish.

    Back to the drawing board then. Thanks Xander84 for pointing that obvious one.

    Sam


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.