0

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

ugo_capeto
  • 129
  • 8
  • 1
    Does it work if class A is a Q_OBJECT too? – JarMan Feb 21 '23 at 21:52
  • @JanMar actually, I get the same error for class A if I do, makes me think that this is a deeper issue... – ugo_capeto Feb 21 '23 at 23:36
  • this is just a snippet of your code. it could be because you declared your constructor but didn't define it (in your nested class). So if you say the above example works, it can't be because of the integration in the project file. – BanAnn Feb 21 '23 at 23:46
  • Possibly related: https://stackoverflow.com/a/50098191/4885321 – alagner Feb 21 '23 at 23:48
  • @BanAnn unfortunately, I thought it worked but when I recently tried to create an instance of that "working" class it failed... – ugo_capeto Feb 21 '23 at 23:52
  • 1
    You really need to show us a full example, not pseudo code. Please provide a [mre]. – JarMan Feb 21 '23 at 23:53
  • What do you have in your above class as `public'` you don't provide an example of what you could possibly use it for. so - since the macro will not work nested anyway - there may be another solution. – BanAnn Feb 22 '23 at 00:14
  • All A, B, C must be `Q_OBJECT`, you did it only for C. – 273K Feb 22 '23 at 00:34
  • @273K sure, I now get 3 vtable errors. I get the same error with a simple non nested class. – ugo_capeto Feb 22 '23 at 00:39
  • Have you run cmake after file changes? You may be build a target that doesn't run the mock generator. – 273K Feb 22 '23 at 01:15
  • @273K I have yes. – ugo_capeto Feb 22 '23 at 01:17
  • This is my experience, too. MOC compiler is picky about how and where a `QObject`-derived class is defined. It must be at the top level in a .h file (can't put it into a .cpp file). Got to live with that. – Igor Tandetnik Feb 22 '23 at 01:31

0 Answers0