Pass custom C++ object to Qml error (no properties) - c++

I am trying to write code that will pass some data from C++ engine to the Qml scripts via signal, but it looks like that I doing some thing wrong, because when I receive signal in Qml my object don't any method or properties! Look at that code:
Signaller - class that invoke signal:
signaller.h:
class Signaller : public QObject
{
Q_OBJECT
public:
explicit Signaller(QObject *parent = 0);
Q_INVOKABLE void invokeSignal();
signals:
void mysignal(TestClass test);
public slots:
};
signaller.cpp:
Signaller::Signaller(QObject *parent) :
QObject(parent)
{
}
void Signaller::invokeSignal()
{
TestClass s;
emit mysignal(s);
}
TestClass - class that will be passed to Qml engine, and which must have test method in Qml script:
Test.h:
class TestClass : public QObject
{
Q_OBJECT
public:
explicit TestClass(QObject *parent = 0);
TestClass(const TestClass& obj);
~TestClass();
Q_INVOKABLE void test();
signals:
public slots:
};
Q_DECLARE_METATYPE(TestClass)
Test.cpp:
TestClass::TestClass(QObject *parent) :
QObject(parent)
{
qDebug()<<"TestClass::TestClass()";
}
TestClass::TestClass(const TestClass &obj) :
QObject(obj.parent())
{
qDebug()<<"TestClass::TestClass(TestClass &obj)";
}
TestClass::~TestClass()
{
qDebug()<<"TestClass::~TestClass()";
}
void TestClass::test()
{
qDebug()<<"TestClass::test";
}
Those 2 classes also registered in main function:
int main(int argc, char *argv[])
{
// SailfishApp::main() will display "qml/template.qml", if you need more
// control over initialization, you can use:
//
// - SailfishApp::application(int, char *[]) to get the QGuiApplication *
// - SailfishApp::createView() to get a new QQuickView * instance
// - SailfishApp::pathTo(QString) to get a QUrl to a resource file
//
// To display the view, call "show()" (will show fullscreen on device).
qmlRegisterType<TestClass>("Test", 1, 0, "Test");
qmlRegisterType<Signaller>("Signaller", 1, 0, "Signaller");
return SailfishApp::main(argc, argv);
}
That is my Qml file with test code:
import Signaller 1.0
Page {
Signaller {
id: sig
Component.onCompleted: sig.invokeSignal()
onMysignal: {
console.log("signal",typeof test);
console.log(typeof test.test);
}
}
}
And the log:
[D] TestClass::TestClass:6 - TestClass::TestClass()
[D] TestClass::TestClass:12 - TestClass::TestClass(TestClass &obj)
[D] TestClass::TestClass:12 - TestClass::TestClass(TestClass &obj)
[D] onMysignal:41 - signal object
[D] onMysignal:42 - undefined
[D] TestClass::~TestClass:18 - TestClass::~TestClass()
[D] TestClass::~TestClass:18 - TestClass::~TestClass()
As you can see from log, TestClass.test field is empty after passing to the Qml.
What I am doing wrong?

You are passing a QObject derived object by value through the signal/slot system - you should never do this (docs). Create it on the heap and send it's pointer through.
In this case you'll probably want it's lifetime controlled via QML, you can set that by calling QQmlEngine::setObjectOwnership( s, QQmlEngine::JavaScriptOwnership).

QObject should not be used by value in signals and slots. Also, It is a bad idea to implement a copy constructor for a QObject subclass when QObject itself hides its copy constructor.
Thus change your signals to pass pointers to QObject and It will be fine. There is a short and good reference on how to communicate between C++ and Qml
ps: You don't need to register classes which declare the Q_OBJECT macro.

This is an example I made for myself for testing C++ <--> QML interaction:
//File: animal.h
#ifndef ANIMAL_H
#define ANIMAL_H
#include <QObject>
class Animal : public QObject
{
Q_OBJECT
Q_PROPERTY(QString animal_name READ get_animal_name WRITE set_animal_name)
public:
explicit Animal(QObject *parent = 0);
QString get_animal_name();
void set_animal_name(QString p_name);
private:
QString animal_name;
};
#endif // ANIMAL_H
//File: animal.cpp
#include "animal.h"
Animal::Animal(QObject *parent) : QObject(parent)
{
}
void Animal::set_animal_name(QString p_name) {
animal_name=p_name;
}
QString Animal::get_animal_name() {
return animal_name;
}
//File: zoo.h
#ifndef ZOO_H
#define ZOO_H
#include <QObject>
class Animal;
class Zoo : public QObject
{
Q_OBJECT
public:
explicit Zoo(QObject *parent = 0);
Q_INVOKABLE Animal* get_animal_by_index(int index);
Q_INVOKABLE void add_animal(Animal *a);
Q_INVOKABLE void dump_animal_info(Animal *a);
Q_INVOKABLE void emit_signal();
signals:
void some_signal(Animal *animal_object);
public slots:
private:
QList<Animal*> animals;
};
#endif // ZOO_H
//File: zoo.cpp
#include <QDebug>
#include "zoo.h"
#include "animal.h"
Zoo::Zoo(QObject *parent) : QObject(parent)
{
Animal *a;
a=new Animal();
a->set_animal_name("Black Bear");
add_animal(a);
a=new Animal();
a->set_animal_name("Gray Wolf");
add_animal(a);
}
Animal* Zoo::get_animal_by_index(int index) {
return animals.at(index);
}
void Zoo::add_animal(Animal *a) {
animals.append(a);
}
void Zoo::dump_animal_info(Animal *a) {
qWarning() << "animal_name=" << a->get_animal_name();
}
void Zoo::emit_signal() {
Animal *a;
a=animals.at(0);
emit some_signal(a);
}
//File: main.cpp
#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include "animal.h"
#include "zoo.h"
int main(int argc, char *argv[])
{
QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
QGuiApplication app(argc, argv);
qmlRegisterType<Zoo>("zoo",1,0,"Zoo");
qmlRegisterType<Animal>("zoo.animal",1,0,"Animal");
QQmlApplicationEngine engine;
engine.load(QUrl(QLatin1String("qrc:/main.qml")));
return app.exec();
}
//File: main.qml
import QtQuick 2.7
import QtQuick.Controls 2.0
import QtQuick.Layouts 1.0
import zoo 1.0
import zoo.animal 1.0
ApplicationWindow {
visible: true
width: 640; height: 480; title: qsTr("Zoo")
Zoo {
id: zoopark
}
Column {
Button {
text: "Test C++ <--> QML data exchange by Method"
onClicked: {
var animal=zoopark.get_animal_by_index(1);
zoopark.dump_animal_info(animal);
}
}
Button {
text: "Text C++ <--> QML data exchage by Signal"
onClicked: {
zoopark.emit_signal();
}
}
}
Connections {
target: zoopark
onSome_signal: {
console.log('signal received');
console.log('from qml: animal name=' + animal_object.animal_name)
console.log('dumping animal info from c++:')
zoopark.dump_animal_info(animal_object)
}
}
function process_signal() {
console.log('from qml animal name=' + animal_object.animal_name)
zoopark.dump_animal_info(animal_object)
}
}

Related

Arguments to C++ signal show up as `undefined` inside QML when a QObject is passed by reference

I have a C++ object with a signal that I connected to a QML handler. However, even though I am passing arguments to the signal, the QML arguments show up as undefined.
With Balloon and Air as custom QObjects, I'm connecting Balloon's signal void popped(Air const& air, int volume, QString message); to a QML handler:
Balloon.qml:
import QtQuick 2.12
import QtQuick.Controls 2.12
import ReproImpl 0.1 as Impl
Item {
id: root
width: 500
height: 500
property int sharpness: 50
readonly property Impl.Balloon impl: Impl.Balloon {
onPopped: {
// Output: {}
console.log(JSON.stringify(arguments));
// TypeError: cannot read property 'type' of undefined
console.log("Bang!", air.type, volume, message);
}
}
Button {
anchors.centerIn: parent
text: "Click me"
onClicked: {
console.log("Clicked!");
impl.prick(sharpness)
}
}
}
main.cpp:
#include <QGuiApplication>
#include <QObject>
#include <QQmlEngine>
#include <QQuickView>
namespace my::ns {
// Used as a parameter of the signal
class Air : public QObject {
Q_OBJECT
Q_PROPERTY(QString type READ type)
public:
Air(QString type, QObject* parent = nullptr)
: QObject{parent}
, type_{type}
{}
Air(QObject* parent = nullptr)
: Air{"", parent}
{}
QString type() const { return type_; }
private:
QString type_;
};
class Balloon : public QObject {
Q_OBJECT
public:
Balloon(int toughness, QObject* parent = nullptr)
: QObject{parent}
, toughness{toughness}
{}
Balloon(QObject* parent = nullptr)
: Balloon{10, parent}
{}
Q_SIGNALS:
void popped(Air const& air, int volume, QString message);
public Q_SLOTS:
void prick(int sharpness)
{
if (sharpness > toughness) {
Air air{"Hello"};
Q_EMIT popped(air, 10, "POP!");
}
}
private:
int toughness;
};
void registerModule()
{
qmlRegisterModule("ReproImpl", 0, 1);
qmlRegisterType<Air>("ReproImpl", 0, 1, "Air");
qmlRegisterType<Balloon>("ReproImpl", 0, 1, "Balloon");
qmlProtectModule("ReproImpl", 0);
}
}
int main(int argc, char** argv)
{
QGuiApplication app(argc, argv);
my::ns::registerModule();
QQuickView window(QUrl("Balloon.qml"));
window.show();
return app.exec();
}
#include "main.moc"
What can I do to fix this?
This occurs in Qt 5.12.0 and 5.13.0.
For whatever reason, QML doesn't support reference signal parameters. You are passing the air by const&:
void popped(Air const& air, int volume, QString message);
To get this working, you need to pass a non-const pointer:
void popped(Air* air, int volume, QString message);
// ...
void prick(int sharpness)
{
if (sharpness > toughness) {
Air air{"Hello"};
Q_EMIT popped(&air, 10, "POP!");
}
}
Note that it is safe to pass a pointer to the stack allocated air; QML does not take ownership of it:
When data is transferred from C++ to QML, the ownership of the data always remains with C++ [unless a QObject is returned from an explicit C++ method call].

Sending QPixmap pointer via signal-slots, empty pixmap data

I have problem with sharing pointer to Pixmap via signal-slot mechanism.
Inside slot function I have correct QPixmap with fulled data.
In reciever I have unaccessible QPixmap with empty data.
Any ideas?
class A
{
public:
A:A():pixmap(0){};
void fillPixmap()
{
// correct Pixmap and data isn't null
}
public signals:
void sendQPixmapToB(QPixmap*);
private:
QPixmap *pixmap;
}
class B
{
public:
B:B(){};
public slots:
void recievePixmap(QPixmap* pixmap)
{
// here in debugger pixmap is unaccesible and data is 0
}
}
void onButtonClicked()
{
a.fillPixmap();
}
int main()
{
A a;
B b;
.....
connect(a, SIGNAL(sendQPixmapToB(QPixmap*)),b,SLOT(recievePixmap(QPixmap*)));
return 0;
}
I can not tell you what is wrong with your code because as you indicate you are showing a pseudo-code, so I will show you the correct way to do it.
#include <QGuiApplication>
#include <QPixmap>
#include <QTimer>
#include <QDebug>
class A: public QObject{
Q_OBJECT
public:
using QObject::QObject;
void sendPixmap(){
fillPixmap();
emit sendQPixmapToB(pixmap);
}
signals:
void sendQPixmapToB(const QPixmap & pixmap);
private:
void fillPixmap(){
pixmap = QPixmap(64, 64);
pixmap.fill(Qt::red);
}
QPixmap pixmap;
};
class B: public QObject{
Q_OBJECT
public:
using QObject::QObject;
public slots:
void recievePixmap(const QPixmap & pixmap){
qDebug()<<pixmap;
}
};
int main(int argc, char *argv[])
{
QGuiApplication a(argc, argv);
A obja;
B objb;
QObject::connect(&obja, &A::sendQPixmapToB, &objb, &B::recievePixmap);
QTimer::singleShot(1000, &obja, &A::sendPixmap);
return a.exec();
}
#include "main.moc"ยท

Exposing to QML the serial port names from C++

I'm trying to expose the QSerialPort.available() through an Q_INVOKABLE QStringList availablePorts() function from a class I expose directly to QML in my main class.
Main:
qmlRegisterType<SerialPortManager>("com.MyApp.qml", 1, 0, "SerialPortManager");
SerialPortManager
class SerialPortManager : public QObject
{
Q_OBJECT
public slots:
Q_INVOKABLE virtual QStringList availablePorts() {
QList<QSerialPortInfo> portsAvailable = QSerialPortInfo::availablePorts();
QStringList names_PortsAvailable;
for(QSerialPortInfo portInfo : portsAvailable) {
names_PortsAvailable.append(portInfo.portName());
}
return names_PortsAvailable;
}
Which is not valid for a model type in QML because it raises Unable to assign QStringList to QQmlListModel* error.
QML
ComboBox {
model: serial.availablePorts()
}
SerialPortManager {
id: serial
}
So how do I get around this?
One solution is to return a QVariant as recommended by the docs, for this we use QVariant::fromValue()
#ifndef SERIALPORTMANAGER_H
#define SERIALPORTMANAGER_H
#include <QObject>
#include <QSerialPortInfo>
#include <QVariant>
class SerialPortManager : public QObject
{
Q_OBJECT
public:
Q_INVOKABLE static QVariant availablePorts() {
QList<QSerialPortInfo> portsAvailable = QSerialPortInfo::availablePorts();
QStringList names_PortsAvailable;
for(const QSerialPortInfo& portInfo : portsAvailable) {
names_PortsAvailable<<portInfo.portName();
}
return QVariant::fromValue(names_PortsAvailable);
}
};
#endif // SERIALPORTMANAGER_H

QT c++ crashes while calling method of class from another class using signals and slots

I am trying to create QQuickWidget based application.
What i am trying to do:
Class A(game.h) and Class B(gamestate.h) are forward declared. Class A is main QQuickWidget class with methods. Class B QObject derived class contains signals, slots, variables and methods.
Class B Variable values can set from class A -- Working
When variable value changes signal should be emitted -- working
when signal was emitted slot method should be called in class B -- working
Class B should invoke a method in class A -- working
Class A should create another qquickwidget -- NOT WORKING
(No compiling error. Application crashes on load)
I tried to call from class A and showIntro() function working fine. But when tried to call from class B its not working.
Game.h
#ifndef GAME_H
#define GAME_H
#include <QQuickWidget>
class GameState;
class Game: public QQuickWidget
{
Q_OBJECT
public:
Game();
GameState *gameState;
void showIntro();
public slots:
void onStatusChanged(QQuickWidget::Status);
};
#endif // GAME_H
Game.cpp
#include "game.h"
#include <QQuickWidget>
#include <QDebug>
#include "gamestate.h"
Game::Game(): QQuickWidget()
{
gameState = new GameState(this);
mainScreen = new QQuickWidget();
connect(this, SIGNAL(statusChanged(QQuickWidget::Status)), this, SLOT(onStatusChanged(QQuickWidget::Status)));
setFixedSize(450, 710);
setSource(QUrl("qrc:/EmptyScreen.qml"));
}
void Game::onStatusChanged(QQuickWidget::Status status)
{
switch(status)
{
case QQuickWidget::Ready:
qDebug() << "hi";
gameState->setValue(1);
//showIntro();
break;
case QQuickWidget::Error:
qDebug() << "Error";
break;
}
}
void Game::showIntro()
{
mainScreen->setSource(QUrl("qrc:/MainScreen.qml"));
mainScreen->setAttribute(Qt::WA_TranslucentBackground);
mainScreen->setParent(this);
}
Here is my Gamestate.h
#ifndef GAMESTATE_H
#define GAMESTATE_H
#include <QObject>
class Game;
class GameState : public QObject
{
Q_OBJECT
public:
explicit GameState(QObject *parent = 0);
int value() const {return m_value; }
Game *game;
signals:
void valueChanged(int newValue);
public slots:
void setValue(int value);
void stateChanged(int value);
private:
int m_value;
};
#endif // GAMESTATE_H
GameState.cpp
#include "gamestate.h"
#include "game.h"
GameState::GameState(QObject *parent) : QObject(parent)
{
m_value = 0;
connect(this,SIGNAL(valueChanged(int)), this, SLOT(stateChanged(int)));
}
void GameState::setValue(int value)
{
if(value != m_value)
{
m_value = value;
emit valueChanged(value);
}
}
void GameState::stateChanged(int value)
{
if(value == 1)
{
game->showIntro();
}
}
and my final main.cpp
#include <QApplication>
#include <QQmlApplicationEngine>
#include "game.h"
Game *game;
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
game = new Game();
game->show();
return app.exec();
}
Pls suggest me what could be the issue.
Member variable Game* game of class GameState is not initialized and therefore the program crashes when trying to dereference the pointer within GameState::stateChanged().
Change the constructor of GameState to the following:
// in gamestate.h
explicit GameState(Game *parent = 0);
// in gamestate.cpp
GameState::GameState(Game *parent) : QObject(parent), game(parent)
{
m_value = 0;
connect(this,SIGNAL(valueChanged(int)), this, SLOT(stateChanged(int)));
}

Refresh QML Listview from Slot

I have problem with the refreshing Listview in QML I looked through many solutions but there is nothing concrete i want from event "onClicked" refresh all listview but how to do this?
sourcecode: http://s000.tinyupload.com/?file_id=86538244635919176055
main.cpp
#include <QApplication>
#include <QQmlApplicationEngine>
#include <QQmlContext>
#include <QDebug>
#include "message.h"
#include "dataobject.h"
int main(int argc, char *argv[]) {
QList<QObject*> dataList;
dataList.append(new DataObject("1"));
dataList.append(new DataObject("2"));
dataList.append(new DataObject("3"));
QApplication app(argc, argv);
QQmlApplicationEngine engine;
Message msg;
msg.setListInstance(&dataList);
auto root_context = engine.rootContext();
root_context->setContextProperty("message",&msg);
//root_context->setContextProperty("myModel", QVariant::fromValue(dataList));
engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
QQmlContext *ctxt = new QQmlContext(engine.rootContext());
//ctxt->setContextProperty("myModel", QVariant::fromValue(dataList));
return app.exec();
}
message.h
#ifndef MESSAGE_H
#define MESSAGE_H
#include <QQmlListProperty>
#include <QObject>
class Message : public QObject {
Q_OBJECT
Q_PROPERTY(QQmlListProperty<QObject> myModel READ getList NOTIFY listChanged)
public:
explicit Message(QObject *parent = 0);
~Message();
void setListInstance(QList<QObject *> *dataList){ list = dataList; }
QQmlListProperty<QObject> myModel() const;
public slots:
void refreshLista();
QQmlListProperty<QObject> getList();
private:
QList<QObject *> *list;
signals:
void listChanged();
};
#endif // MESSAGE_H
message.cpp
#include "message.h"
#include "dataobject.h"
#include <QDebug>
Message::Message(QObject *parent):QObject(parent){}
Message::~Message(){}
void Message::refreshLista(){
list->append(new DataObject("44444"));
emit listChanged();
qDebug() << " REFRESH LISTA ";
}
QQmlListProperty<QObject> Message::getList(){
return QQmlListProperty<QObject>(this, *list);
}
dataobject.h
#ifndef DATAOBJECT_H
#define DATAOBJECT_H
#include <QObject>
class DataObject : public QObject { Q_OBJECT
Q_PROPERTY( QString title READ title WRITE setTitle NOTIFY info)
public:
DataObject(QObject * parent = 0 );
DataObject(const QString &_title,QObject * parent=0 );
QString title() const;
void setTitle(const QString &);
signals:
void info();
private:
QString m_id;
QString m_title;
};
#endif // DATAOBJECT_H
dataobject.cpp
#include "dataobject.h"
DataObject::DataObject(QObject * parent): QObject(parent){}
DataObject::DataObject(const QString &_title,QObject * parent)
:QObject(parent),m_title(_title)
{}
QString DataObject::title() const { return m_title;}
void DataObject::setTitle(const QString &title) {
if ( title != m_title ) { m_title = title; emit info();}
}
Two things to do:
Message has to modify the model, therefore Message needs the instance of the model.
When model is changed, emits signal to QML so QML can reload data.
Assume that your Message class is responsible for the model. First, pass the model to Message.
class Message : public QObject {
//...
private:
QList<QObject *> *list;
public:
void setListInstance(QList<QObject *> *dataList){ list = dataList; }
}
//main.cpp
msg.setListInstance(&dataList);
You can easily change the content of model now:
void Message::refreshLista(){ list->append(new DataObject("new")); /*whatever*/}
However, QML won't reload the model because setContextProperty("myModel", QVariant::fromValue(dataList)); cannot emit signals. Remove this line from main.cpp and create a new property in Message instead:
class Message : public QObject {
Q_OBJECT
Q_PROPERTY(QQmlListProperty<QObject> myModel READ getList NOTIFY listChanged)
public:
QQmlListProperty<QObject> getList();
signals:
void listChanged();
}
In the implementation, create a QQmlListProperty and emits property-changed signal when necessary.
void Message::refreshLista(){
list->append(new DataObject("new"));
emit listChanged();
}
QQmlListProperty<QObject> Message::getList(){
return QQmlListProperty<QObject>(this, *list);
}
Finally, the ListView in QML should bind to message.myModel instead of myModel:
ListView {
width: 400; height: 300;
model: message.myModel
//...
}