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

12
  • 1
    Does it work if class A is a Q_OBJECT too? Commented Feb 21, 2023 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... Commented Feb 21, 2023 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. Commented Feb 21, 2023 at 23:46
  • Possibly related: stackoverflow.com/a/50098191/4885321 Commented Feb 21, 2023 at 23:48
  • 1
    You really need to show us a full example, not pseudo code. Please provide a minimal reproducible example. Commented Feb 21, 2023 at 23:53

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.