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. undefined reference .... to custom slot
Forum Updated to NodeBB v4.3 + New Features

undefined reference .... to custom slot

Scheduled Pinned Locked Moved General and Desktop
9 Posts 4 Posters 4.2k 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
    samopn
    wrote on last edited by Chris Kawa
    #1

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

    1 Reply Last reply
    0
    • Chris KawaC Offline
      Chris KawaC Offline
      Chris Kawa
      Lifetime Qt Champion
      wrote on last edited by Chris Kawa
      #2

      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) { ...

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

        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

        1 Reply Last reply
        0
        • Chris KawaC Offline
          Chris KawaC Offline
          Chris Kawa
          Lifetime Qt Champion
          wrote on last edited by
          #4

          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.

          1 Reply Last reply
          0
          • S Offline
            S Offline
            samopn
            wrote on last edited by
            #5

            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

            1 Reply Last reply
            0
            • X Offline
              X Offline
              Xander84
              wrote on last edited by Chris Kawa
              #6

              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. :)

              1 Reply Last reply
              0
              • Chris KawaC Offline
                Chris KawaC Offline
                Chris Kawa
                Lifetime Qt Champion
                wrote on last edited by
                #7

                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.

                1 Reply Last reply
                0
                • S Offline
                  S Offline
                  samopn
                  wrote on last edited by
                  #8

                  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

                  1 Reply Last reply
                  0
                  • B Offline
                    B Offline
                    baritoslot
                    wrote on last edited by
                    #9
                    This post is deleted!
                    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