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. Passing Data From QT To C++
Forum Updated to NodeBB v4.3 + New Features

Passing Data From QT To C++

Scheduled Pinned Locked Moved General and Desktop
4 Posts 3 Posters 1.0k 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.
  • P Offline
    P Offline
    physicsguy
    wrote on last edited by
    #1

    I was working through one of the examples on signals and slots and ive found that there is one line of code that is causing compilation issues. Here is my code

    @#include <iostream>
    #include <QObject>
    using namespace std;

    class Counter : public QObject{
    Q_OBJECT
    signals:
    void valueChanged(int newValue);
    public:
    Counter(){
    m_value = 1;
    }
    int value() const {
    return m_value;
    }
    public slots:
    void setValue(int value);
    private:
    int m_value;

    };
    void Counter::setValue(int value){
    if(value != m_value){
    m_value = value;
    emit valueChanged(value); // This line is causing errors
    }

    }
    int main(void){
    Counter a, b;
    QObject :: connect(&a, SIGNAL(valueChanged(int)), &b, SLOT(setValue(int)));

    a.setValue(7);
    
    cout << "a.setValue(7) " << endl;
    cout << "(a.value, b.value) = " << a.value() << b.value() << endl;
    
    
    
    return 0;
    

    }@

    Particularly, the error its causing is

    john:~/Desktop/QT/gui1$ make
    g++ -m64 -Wl,-O1 -o gui1 example1.o -L/usr/X11R6/lib64 -lQt5Gui -L/usr/lib/x86_64-linux-gnu -lQt5Core -lGL -lpthread
    example1.o: In function Counter::setValue(int)': example1.cpp:(.text+0x9): undefined reference to Counter::valueChanged(int)'
    collect2: error: ld returned 1 exit status
    make: *** [gui1] Error 1

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

      Hi and welcome to devnet,

      since you declared your QObject class in main.cpp moc is not run. Just move it to it's own header and you should be good to go

      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
      • P Offline
        P Offline
        physicsguy
        wrote on last edited by
        #3

        Hi,
        thanks for the quick response, that was indeed the issue!

        1 Reply Last reply
        0
        • U Offline
          U Offline
          utcenter
          wrote on last edited by
          #4

          SGaist - you can declare QObject derived classes in main.cpp, after your class throw in @#include "main.moc"@

          So it seems the problem is not about the moc not running.

          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