undefined refderence to vtable with Qt library using Q_OBJECT

72 Views Asked by At

After some research I found a solution for this error that has happened to a fiew fellow programmers:

#include <QtCharts/QChartView>

class A {
    class MyQObject: public QtCharts::QChartView {
        Q_OBJECT
        ...
    };
    ...
};

this code can't be compiled, the moc struggles to understand the Q_OBJECT label in the nested class.

However this works when the class is not nested:

#include <QtCharts/QChartView>


class MyQObject: public QtCharts::QChartView {
    Q_OBJECT
    ...
};

Does anyone know if this is the only way to make this work? I really wanted my object to be encapsulated and in the namespace of the class A...

edit: I have tried to create an instance of the class MyQObject from the second example, I only tried to compile the code before and it compiled. The same issue presents itself when an instance of the class with the Q_OBJECT label is created in my code. That brings me to my problem, I can't get this to work, I am using vscode and cMake for this project, the Qt library is linked correctly and I used the following commands in my CMakeLists.txt:

set(CMAKE_AUTOUIC ON)
set(CMAKE_AUTORCC ON)
set(CMAKE_AUTOMOC ON)

I tried clean compiling the code but the Q_OBJECT label just does not seem to work.

edit 2: Here is a more elaborate example:

#include <QtCharts/QChartView>
#include <QMainWindow>
#include <iostream>


class A: public QMainWindow {
public: 
    /*******FINE CLASS B******************/
    class B: public QtCharts::QChartView {
    public:
        B() { std::cout << "I do work\n"; }
    };
    /*****PROBLEMATIC CLASS C*************/
    class C: public QtCharts::QChartView {
        Q_OBJECT
    public:
        C() { std::cout << "I do not work\n"; }
    };
    /****A CONSTRUCTOR**************/
    A() { std::cout << "I work\n"; }
};

edit: @igorTandetnik I can't even use this header:

#ifndef HEADER_HPP
#define HEADER_HPP
#include <QMainWindow>

class A: public QMainWindow {
    Q_OBJECT
};

#endif//HEADER_HPP

At this point I might as well tell you what I was trying to achieve: I wanted to create a graph widget in my main window and resize it, hopefully calling a function to correctly size the object of the resized graph. I thought I could do that with the connect function, but as far as I understood, I need this Q_OBJECT label to use signals and slots...

0

There are 0 best solutions below