Im using a QML frontend for my C++ App which worked fine so far. However, I planned to tidy up my code and split functions into smaller classes
At first, my Property decleration looked like this:
class mainBoard : public QObject
{
Q_OBJECT
Q_PROPERTY(double baroAltitude MEMBER baroAltitude NOTIFY pressureChanged)
public:
explicit mainBoard(QObject *parent = 0);
void start();
private:
double baroAltitude = 0;
signals:
void pressureChanged();
};
Now, I do have this external class, with my getter method.
#include "pressuresensor.h"
class mainBoard : public QObject
{
Q_OBJECT
Q_PROPERTY(double baroAltitude READ pressureSensors.getBaroAltitude NOTIFY pressureSensors.pressureChanged)
public:
explicit mainBoard(QObject *parent = 0);
void start();
private:
pressureSensor pressureSensors;
};
But now, all I get is:
mainboard.h:25: Parse error at "pressureSensors"
error: [moc_mainboard.cpp] Error 1
Is there a better, or correct (because its working :D ) way for it?
thanks!
Q_PROPERTY does not support getters/setters methods which are not part of the class in question.
If you really want to keep the pressureSensor class you have to provide getters/setters in the mainBoard class and forward the calls.
class mainBoard : public QObject
{
Q_OBJECT
Q_PROPERTY(double baroAltitude READ getBaroAltitude)
public:
double getBaroAltitude() const {
return pressureSensors.getBaroAlitude();
}
private:
pressureSensor pressureSensors;
};
Related
This question already has answers here:
QObject Multiple Inheritance
(3 answers)
Closed 3 years ago.
I have an interesting issue. I don't know how to combine two classes inhereted from QObject. Base accent to my problem using Q_INVOKABLE method in derived class from base class. I want use "loadFromJson" multiple times in various "models".
I've allready trying to define Q_OBJECT macro, and rebuild the logic of classes.
//model.h
class Model : public QObject{
Q_OBJECT
public:
Q_INVOKABLE bool loadFromJson(const QString &data);
}
//sqltablemodel.h
class SqlTableModel : public QSqlTableModel{
//some code
}
//server.h
class Server : public SqlTableModel, Model
{
Q_OBJECT
public:
explicit Server(QString tableName = "");
};
//main.cpp
Server *server = new Server();
context->setContextProperty("server", server);
//
//server.qml
server.loadFromJson("123"); // not working
/*
If i remove QObject from Model then i can't call loadFromJson even Q_OBJECT defined, otherwise Server is ambigous using QObject. But i want to use methods from both base classes SqlTableModel & Model in derived Server with Q_INVOKABLE ability.
*/
main.cpp:41: ошибка: ‘QObject’ is an ambiguous base of ‘Server’
context->setContextProperty("server", server);
model.h:27: ошибка: undefined reference to `vtable for Model'
I would use a "has a" relationship instead of "is a" one. It would look something like this:
class Server : public SqlTableModel {
Q_OBJECT
Q_PROPERTY(Model* model READ model WRITE setModel NOTIFY modelChanged)
Model* _model;
public:
explicit Server(QObject* parent = nullptr)
: SqlTableModel(parent), _model(new Model(this)) {}
signals:
void modelChanged(Model* model);
public:
Model* model() const noexcept { return _model; }
void setModel(Model* m) noexcept { _model = m; }
};
Then you'd call it in your qml like this: server.model.loadFromJson("123");
But also you can make Model inherit private QObject if you try harder to solve issues that arise after that.
I solved the problem with changing the order of inheritence. SqlTableModel->Model->Server.
I would like to share signals and possibly slot implementations among different classes, but it seems Qt does not allow this.
Basically I would like to have something like:
class CommonSignals
{
signals:
void mysignal();
};
class A :
public QObject,
public CommonSignals
{
Q_OBJECT
public:
void doSomething()
{
emit mysignal();
}
};
class B :
public QObject,
public CommonSignals
{
Q_OBJECT
public:
B()
{
connect(&a, &A::mysignal, this, &B::mysignal);
}
A a;
};
So that when for some reason A emits a signal B emits the same signal too. This to avoid useless code replication and improve maintainability.
Any ideas?
PS I've tried also with virtual inheritance but I've got classical qmake problems
You cannot do this. QObject cannot be used with multiple inheritance of multiple QObject bases. Only the first inherited class can be a QObject. See:
https://doc.qt.io/qt-5/moc.html#multiple-inheritance-requires-qobject-to-be-first
Since you need both base classes to be a QObject (CommonSignals provides signals, it would need to be a QObject), you're out of luck. Your only option here is using plain old macros:
#define COMMON_SIGNALS \
void signal1(); \
void signal2();
class A: public QObject
{
Q_OBJECT
public:
// ...
signals:
COMMON_SIGNALS
};
class B: public QObject
{
Q_OBJECT
public:
// ...
signals:
COMMON_SIGNALS
};
The core issue with all this is that Qt uses moc to generate the underlying code for signals and slots. However, moc is just a simple preprocessor that doesn't understand most of C++.
You could use Verdigris to get rid of moc:
https://github.com/woboq/verdigris
This allows you to have templated QObject classes, for example. I have not tried it myself and thus don't know if it actually allows multiple inheritance. Might be worth looking into.
Why not just move the inheritance from QObject away from the derived classes A and B and into CommonSignals...
class CommonSignals: public QObject {
Q_OBJECT;
signals:
void mysignal();
};
class A: public CommonSignals {
Q_OBJECT;
public:
void doSomething ()
{
emit mysignal();
}
};
class B: public CommonSignals {
Q_OBJECT;
public:
B ()
{
connect(&a, &A::mysignal, this, &B::mysignal);
}
A a;
};
Will that not work for you?
Hey guys I know there are already some threads for this question but I think I made none of the mistakes others did which led to the problem. So here is my code:
#include "consolerender.h"
consoleRender::consoleRender(QObject *parent) :
QObject(parent) {
connect(Enviroment::instance, &Enviroment::enviromentChanged,
this, &consoleRender::renderField);
}
And the header:
class consoleRender : public QObject
{
Q_OBJECT
public:
explicit consoleRender(QObject *parent = 0);
public slots:
void renderField();
};
And the Enviroment.h
class Enviroment : public QObject
{
Q_OBJECT
public:
static Enviroment& instance();
virtual ~Enviroment();
//stuff...
signals:
void enviromentChanged();
I already tried to do the connect in a separate class, I tried to use the old connect syntax (SIGNAL/SLOT(function)) and tried it with >>all<< my classes inheriting from QObject but it showed the same error. Also it says something that the function expects 3 arguments but gets 4. and seems to point at the connect(...renderField). I heard of a solution to just do all of that in the MainWindow class but that is not an option for me.
You have to pass the instance pointer:
connect(&Enviroment::instance(), &Enviroment::enviromentChanged,
this, &consoleRender::renderField);
suppose I've the following classes :
class A:public QObject{
Q_OBJECT
...
signals:
void sendData(QString data);
}
class B:public QObject{
Q_OBJECT
public:
A a;
...
public slots:
void onSendData(QString);
signals:
void sendData(QString data);
}
class C:public QObject{
Q_OBJECT
public:
B b;
...
public slots:
void onSendData(QString);
signals:
void sendData(QString data);
}
.
.
.
class MainWindow:public QMainWindow{
Q_OBJECT
public:
LastClass lc;
public slots:
void onSendData(QString);//show data on ui
}
class A digs data and when it finds a special data it must send it to ui(mainwindow) , so it calls sendData signal, then class B which contains an instance of A, grabs the signal and from it's slot sends it to above class ,...
as you can see, it causes a recursive signal/slot send and receiving which I doubt that it is a good design.
Is it a correct way or must I change the design ? ( it's hard to change design in some circumstances though).
(I cannot use inheritance because each class has a different functionality and different functions.)
This is not an obligation to connect the signals to a slot, in your case you can directly connect the signals between them. In the class B for example:
CONNECT(a, SIGNAL(sendData(QString)), this, SIGNAL(sendData(QString)));
Alternatively, you can use events so that you don't have to make this chain (but you won't know for sure who is catching the event then). Documentation here.
Is it possible to make MyObject be always equal (one same instance) in all it's qml definitions?
C++:
class MyObject : public QObject {
Q_OBJECT
Q_DISABLE_COPY(MyObject)
Q_PROPERTY(QString test READ test NOTIFY testChanged)
public:
explicit MyObject(QObject *parent = 0);
signals:
void testChanged();
private:
QString test() const {
return _test;
}
QString _test;
};
QML:
Item {
MyObject { id: myObject1 }
MyObject { id: myObject2 }
}
I want myObject1 to be equal myObject2. Some kind of singleton (but no qmlRegisterSingletonType)
I can interpret your question as if you want more than one entry of MyObject in QML code referring to the same C++ object. You also know what singleton is. How about the wrapper over the singleton that you can use with QML like:
class MyObject : public QObject {
Q_OBJECT
Q_DISABLE_COPY(MyObject)
Q_PROPERTY(QString test READ test NOTIFY testChanged)
public:
explicit MyObject(QObject *parent = 0);
signals:
void testChanged();
private:
QString test() const {
return MySingleton::instance().test();
}
// QString _test; // this supposed to be implemented in MySingleton
};
Or I in my application for many different types of communication between C++ and QML use some kind of MessageBoard from the article Exposing Attributes of C++ Types to QML. That one is even more convenient considering many uses.