With Qt 5 it was possible to use code like this:
// Test.h
#include <QObject>
#include <QMetaObject>
class LanguageModel;
class Test : public QObject
{
Q_OBJECT
Q_PROPERTY(LanguageModel*, ...)
public:
};
// Test.cpp
#include "Test.h"
#include "LanguageModel.h"
// LanguageModel.h
#include <QObject>
#include <QMetaObject>
class LanguageModel : public QObject
{
Q_OBJECT
}
Now I am trying to convert the project to Qt 6 but the above code fails in the generated "debug\moc_Test.cpp" file with this error message:
C:\Qt\6.1.0\msvc2019_64\include\QtCore\qmetatype.h:778: error: C2338: Type argument of Q_PROPERTY or Q_DECLARE_METATYPE(T*) must be fully defined
Replacing the class forward definition with a direct header include works:
// Test.h
#include <QObject>
#include <QMetaObject>
#include "LanguageModel.h" //class LanguageModel;
class Test : public QObject
{
Q_OBJECT
Q_PROPERTY(LanguageModel*, ...)
public:
};
How can I keep using class defintions instead of including the headers in Qt 6?
Regards,
I've just asked here on Qt forum as I had the same issue today using Models.
It's problematic for me to have to include the header.
Two solutions were given to me by VRonin:
Q_MOC_INCLUDE to include the header only for the moc file:
// Test.h
#include <QObject>
#include <QMetaObject>
Q_MOC_INCLUDE("LanguageModel.h")
class Test : public QObject
{
Q_OBJECT
Q_PROPERTY(LanguageModel*, ...)
public:
};
Q_DECLARE_OPAQUE_POINTER: to tell Qt we want to keep an opaque pointer
// Test.h
#include <QObject>
#include <QMetaObject>
#ifndef OPAQUE_PTR_LanguageModel
#define OPAQUE_PTR_LanguageModel
Q_DECLARE_OPAQUE_POINTER(LanguageModel*)
#endif // OPAQUE_PTR_LanguageModel
class Test : public QObject
{
Q_OBJECT
Q_PROPERTY(LanguageModel*, ...)
public:
};
Personally I would prefer the second option, it could save some compilation time.
I've added the macro just in case you would use it in several objects of a same project.
I'm not an expert, but it looks like it's required since Qt 6.0 because of static assertion. Proof: https://embeddeduse.com/2021/01/17/migrating-a-harvester-hmi-from-qt-5-12-to-qt-6-0/ (chapter 'Error: invalid application of ‘sizeof’ to incomplete type ‘IntegerRangeModel’')
Related
I am new in C++ Qt and struggling with the correct use of forward declarations and #include.
What I want to do:
I have a Qt Gui (Class Ui::Gui) where we can set values.
I want to save these values in Gui Class variables.
As soon as a button (Generate Xml) is clicked, I want to pass the object
'ui' to the XmlGeneratorClass, So i can use the values to generate a Xml.
gui.h
#ifndef GUI_H
#define GUI_H
#include <QMainWindow>
#include <QDebug>
#include "xmlgeneratorqobject.h"
namespace Ui {
class Gui;
}
class Gui : public QMainWindow
{
Q_OBJECT
public:
explicit Gui(QWidget *parent = nullptr);
~Gui();
qint8 testvalue = 1;
signals:
void transmitToXmlGen(Ui::Gui*);
private slots:
void on_pushButtonGenerateXml_clicked();
private:
Ui::Gui *ui;
XmlGeneratorQObject *xmlgenerator = new XmlGeneratorQObject();
};
#endif // GUI_H
gui.cpp
#include "gui.h"
#include "ui_gui.h"
Gui::Gui(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::Gui)
{
ui->setupUi(this);
connect(this,SIGNAL(transmitToXmlGen(Ui::Gui*)),xmlgenerator,SLOT(receiveFromGui(Ui::Gui*)));
}
Gui::~Gui()
{
delete ui;
}
void Gui::on_pushButtonGenerateXml_clicked()
{
emit transmitToXmlGen(ui);
}
xmlgeneratorqobject.h
#ifndef XMLGENERATORQOBJECT_H
#define XMLGENERATORQOBJECT_H
#include <QObject>
#include <QDebug>
namespace Ui {
class XmlGeneratorQObject;
class Gui;
}
class XmlGeneratorQObject : public QObject {
Q_OBJECT
public:
explicit XmlGeneratorQObject(QObject * parent = nullptr);
private slots:
void receiveFromGui(Ui::Gui*);
};
#endif // XMLGENERATORQOBJECT_H
xmlgeneratorqobject.cpp
#include "xmlgeneratorqobject.h"
XmlGeneratorQObject::XmlGeneratorQObject(QObject *parent){}
void XmlGeneratorQObject::receiveFromGui(Ui::Gui* objectFromGui)
{
qDebug() << objectFromGui->testvalue; // ERROR member access into incomplete type 'Ui::Gui'
}
Expected result:
Access to public variables from passed gui-object should be possible
Actual result:
member access into incomplete type 'Ui::Gui'
Can you please help me learn forward declaration / include?
Is my approach in general okay?
Your xmlgeneratorqobject.cpp needs the line
#include "ui_gui.h"
This gives it the details of the ui widgets. This file is generated by the Qt build system.
I make a project of music player in Qt based on QWidget class
and using widget.ui form.
I want to check that user will not add two labels with the same text, so I'm trying to add field: QList labelsList;
in my widget.h file.
(every time user add label, than: labelsList.append(label), and before he can add this label program would iterate through labelsList and check if in list exists label with patricular text).
Although "myqlabel.h" is included, compiler says that 'myQLabel' was not declared in this scope... I don't know why. Kind of weird for me, but maybe I lack/forget some basic knowledge... ;/
Thanks for help!
Code (just needed fragments) below:
widget.h file:
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include "myqlabel.h"
#include <QList>
#include <QFormLayout>
#include <QSqlDatabase>
#include <QtSql>
#include <QMediaPlayer>
namespace Ui {
class Widget;
}
class Widget : public QWidget
{
Q_OBJECT
public:
explicit Widget(QWidget *parent = 0);
~Widget();
// to check if label with input text already exists:
// HERE OUR BAD FIELD:
QList<myQLabel*> labelsList;
private:
Ui::Widget *ui;
QMediaPlayer player;
qint64 duration;
};
#endif // WIDGET_H
myqlabel.h file:
#ifndef MYQLABEL_H
#define MYQLABEL_H
#include <QLabel>
#include "widget.h"
#include "ui_widget.h"
#include <QFormLayout>
#include <QMouseEvent>
class myQLabel : public QLabel {
Q_OBJECT
public:
myQLabel(QString& text, QFormLayout* parent = 0) : QLabel(text){
setAcceptDrops(true);
position = amount;
this->parent = parent;
labelsList.append(this);
}
};
#endif // MYQLABEL_H
You have a circular dependency in your includes. This means that one of the headers is not seen depending on the order in which they are first included (due to the include guards).
Remove #include "myqlabel.h" from widget.h and add it to widget.cpp. Then forward declare myQLabel by adding
class myQLabel;
to widget.h.
I have a Cpp Class which I want to refactor into a header and .cpp file. No problem normally, but when I try to do this Qt Quick one I can't get it to compile. It's fine if I put all of it in the header file, but otherwise I get various different errors depending on how I try to do it. Is there a proper way. I think it has to do with the Q_INVOKABLE bit, but not sure.
Here is my code...
#ifndef APPLICATIONDATA_H
#define APPLICATIONDATA_H
#include <QDateTime>
#include <QObject>
class ApplicationData : public QObject
{
Q_OBJECT
public:
ApplicationData(){}
Q_INVOKABLE QDateTime getCurrentDateTime() const{
return QDateTime::currentDateTime();
}
};
#endif // APPLICATIONDATA_H
Thanks for any pointers.
This compiles but I'm not sure why it does or why it didn't:
//header file
#ifndef APPLICATIONDATA_H
#define APPLICATIONDATA_H
#include <QDateTime>
#include <QObject>
class ApplicationData : public QObject
{
Q_OBJECT
public:
ApplicationData(); //constructor
Q_INVOKABLE QDateTime getCurrentDateTime() const; //function
};
#endif // APPLICATIONDATA_H
//.cpp file
#include "applicationdata.h"
#include <QDateTime>
#include <QObject>
ApplicationData::ApplicationData(){} //constructor implementation
QDateTime ApplicationData::getCurrentDateTime() const{ //function implementation
return QDateTime::currentDateTime();
}
Basically I am trying to inherit everything from friction into base (or even the other way round) however, it is not identifying the classes I put in.
base.h
#ifndef BASE_H
#define BASE_H
#include <QMainWindow>
namespace Ui {
class Base;
}
class Base : public QMainWindow{
Q_OBJECT
public:
explicit Base(QWidget *parent = 0);
~Base();
private:
Ui::Base *ui;
};
#endif // BASE_H
friction.h:
#ifndef FRICTION_H
#define FRICTION_H
class Friction : public Base{ // THIS IS WHERE THE ERROR IS
public:
Friction();
};
#endif // FRICTION_H
base.cpp
#include "friction.h"
#include "base.h"
#include "ui_base.h"
Base::Base(QWidget *parent) :QMainWindow(parent),ui(new Ui::Base){
ui->setupUi(this);
}
Base::~Base(){
delete ui;
}
friction.cpp
#include "friction.h"
#include "base.h"
#include "ui_base.h"
Friction::Friction(){
}
and finally main.cpp
int main(int argc, char *argv[]){
QApplication a(argc, argv);
Base w;
w.show();
Friction f;
return a.exec();
}
I receive the error "expected class name before '{' token", I have cut the project down as much as I can and the error still comes up and I really don't know why.
I am fairly new to c++ however I find inheritance not much of an issue on a basic program but upon moving to Qt I couldn't seem to get it working. I have tried numerous things regarding changing the includes etc etc as I am completely oblivious as to why it's not identifying the class.
If friction inherits Base , than you should put:
#include "base.h"
in friction.h file , like so:
#ifndef FRICTION_H
#define FRICTION_H
#include "base.h"
class Friction : public Base{ // THIS IS WHERE THE ERROR IS
public:
Friction();
};
what am I doing wrong?
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include "fileoperations.h"
namespace Ui {
class MainWindow;
}
class FileOperations;
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
FileOperations FileController;
private slots:
void on_OpenButton_clicked();
void on_SaveButton_clicked();
void on_EncodeButton_clicked();
void on_DecodeButton_clicked();
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
When i try to compile and run the program, it says:
g:\ke\c++ projects\projects\qt\shitlencoder\mainwindow.h:18: error: C2079: 'MainWindow::FileController' uses undefined class 'FileOperations'
Here's the strange thing, if I change 'FileOperations FileController;' to 'FileOperations *FileController;'(Obviously this compiles wrongly, because the rest of my codes that you can't see havn't been adapted to '->' instead of '.')
Then if I change it back to 'FileOperations FileController;' it lets me compile the program once (And it works fine), then it has the error the next time I try to compile it.
I'm using Qt 5.0.
fileoperations.h:
#ifndef FILEOPERATIONS_H
#define FILEOPERATIONS_H
#include "ui_mainwindow.h"
#include "mainwindow.h"
#include <QFileDialog>
#include <string>
#include <time.h>
#include <iostream>
#include <conio.h>
#include <windows.h>
#include <fstream>
using namespace std;
class FileOperations
{
public:
FileOperations();
void SetInputFile(QString x);
void SetOutputFile(QString x);
void EncryptAndSave(Ui::MainWindow *NUI);
void DecryptAndSave(Ui::MainWindow *NUI);
void createid(int id, int id2);
int GetCFuncion();
void SetCFuncion(int x);
long long Get_Size(string filename);
bool Get_Toobig(string path);
//DWORD WINAPI Thread_no_1();
private:
string InputFilename;
string OutputFilename;
int CFuncion;//CurrentFunction;
vector<int> conbyte1;
vector<int> conbyte2;
vector<int> opbyte1;
vector<int> opbyte2;
vector<int> passwordbytes;
};
#endif // FILEOPERATIONS_H
I assume that, in your .cpp file, you are using
#include "fileoperations.h"
Then, in fileoperations.h, you are including mainwindow.h which again includes fileoperations.h which is basically correct, since you are using a FileOperations object as parameter. But, due to the guards, class FileOperations is not seen by the compiler this time, hence FileOperations is unknown when used as parameter in your method. You need to break this dependency:
In fileoperations.h, use a forward declaration for Ui::MainWindow and remove the #include "mainwindow.h":
namespace Ui {
class MainWindow;
}
...
Since you are holding a FileOperations object in your class, you need the full class declaration. This means you have to include the header, you cannot simply forward declare the class like you are doing now. If you hold only a pointer, and do not have any code in your header that attempts to dereference the pointer, then the forward declaration is enough.
EDIT You have a cyclical include. You are including mainwindow.h in fileoperations.h. You can fix if by removing that include completely.
You have circular include issue, mainwindow.h and fileoperations.h include each other, try to remove below line from fileoperations.h
#include "mainwindow.h"