I have a problem with a MessageDialog
signal in QML. In my MessageDialog
I have two buttons for Yes and No. I want to connect each button with a signal.
Here is my qml file:
import QtQuick 2.2
import QtQuick.Dialogs 1.1
Item{
MessageDialog {
signal qmlYesSig(string msg)
signal qmlNoSig (string msg)
title: "Send data?"
icon: StandardIcon.Question
text: "Do you want to save your data on the online platform?"
detailedText: "Click Yes "
standardButtons: StandardButton.Yes | StandardButton.No
Component.onCompleted: visible = true
onYes: qmlYesSig("From yes")
onNo: qmlNoSig("From no")
}
}
Here is my slot:
class MyClass : public QObject
{
Q_OBJECT
public slots:
void cppSlot(const QString &msg) {
qDebug() << "Called the C++ slot with message:" << msg;
}
};
And here is how i use this in main:
QQuickView view(QUrl::fromLocalFile("window.qml"));
QObject *item = view.rootObject();
AddData myClass;
QObject::connect(item, SIGNAL(qmlSignal(QString)),
&myClass, SLOT(cppSlot(QString)));
view.show();
It give me the error:
C2665: 'QObject::connect': none of the 3 overloads could convert all the argument types
I have try many times but I can't make work QML signal and C++ slots. Also I have try the example from here Qt doc and give me the same error.
Can somebody give me an idea how to connect QML signal and C++ slots for a MessageDialog
?
A Connections object creates a connection to a QML signal. However, it is not possible to connect to a signal in this way in some cases, such as when: Multiple connections to the same signal are required. Creating connections outside the scope of the signal sender.
All QML methods are exposed to the meta-object system and can be called from C++ using QMetaObject::invokeMethod(). You can specify types for the parameters and the return value after the colon character, as shown in the code snippet below.
Your QML file is:
Item{
MessageDialog {
signal qmlYesSig(string msg)
signal qmlNoSig (string msg)
[...]
}
}
And your C++ code is:
QObject *item = view.rootObject();
AddData myClass;
QObject::connect(item, SIGNAL(qmlSignal(QString)),
&myClass, SLOT(cppSlot(QString)));
It means that you are looking for a signal called "qmlSignal" in the root item of your QML file. This root item is simply
Item{}
As you can see, there is no signal called "qmlSignal".
You have to define the signal in the root item and emit it from the message box.
Item{
signal qmlSignal(string msg)
MessageDialog {
onYes: parent.qmlSignal("From yes")
onNo: parent.qmlSignal("From no")
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With