Home > Mobile >  QML - Cannot assign to non-existent property "onYes" or "onNo" in MessageDialog
QML - Cannot assign to non-existent property "onYes" or "onNo" in MessageDialog

Time:08-21

I'm just making a simple message dialog using MessageDialog in QML. I got a problem about connecting onYes (also onNo) signal to a slot. Here's my code

import QtQuick
import QtQuick.Dialogs
import QtQuick.Controls


MessageDialog {
    title: "Save File?"
    text: "The file has been modified"
    informativeText: "Do you want to save your changes?"
    buttons: MessageDialog.Yes | MessageDialog.No | MessageDialog.Cancel

    onYes: console.log("You clicked Yes") // The error started occur here
    onNo: console.log("You clicked No")
}

I ran this code and the compiler said Cannot assign to non-existent property "onNo" // Also onYes.

How can I fix it? I'm using Qt 6.3.1

CodePudding user response:

The correct handlers should have names onYesClicked and onNoClicked. From Qt docs: enter image description here

https://doc.qt.io/qt-6/qml-qt-labs-platform-messagedialog.html#yesClicked-signal

CodePudding user response:

Ok guys, I found the answer.

First, Add QT = widgets to the .pro file, then add the following code to the main.cpp:

#include <QApplication>
#include <QQmlApplicationEngine>

int main(int argc, char *argv[])
{
    QApplication app(argc, argv);
    QQmlApplicationEngine engine;
    engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
    return app.exec();
}

After that, add import Qt.labs.platform to the qml MessageDialog file. Finally, follow the answer of Sergey Lebedev: use onYesClicked, onNoClicked instead of onYes, onNo. Like this:

Before

import QtQuick
import QtQuick.Dialogs
import QtQuick.Controls


MessageDialog {
    title: "Save File?"
    text: "The file has been modified"
    informativeText: "Do you want to save your changes?"
    buttons: MessageDialog.Yes | MessageDialog.No | MessageDialog.Cancel

    onYes: console.log("You clicked Yes") // The error started occur here
    onNo: console.log("You clicked No")
}

After

import QtQuick
import QtQuick.Dialogs
import QtQuick.Controls
import Qt.labs.platform


MessageDialog {
    title: "Save File?"
    text: "The file has been modified"
    informativeText: "Do you want to save your changes?"
    buttons: MessageDialog.Yes | MessageDialog.No | MessageDialog.Cancel

    onYesClicked: console.log("You clicked Yes") // Now we fixed the error!
    onNoClicked: console.log("You clicked No")
}
  • Related