0
votes

I am getting the following error:

Debugging starts QML debugging is enabled. Only use this in a safe
environment. QML Debugger: Waiting for connection on port 55186...
QQmlApplicationEngine failed to load component qrc:/main.qml:23
Expected token `)'

Line 23

QFile, file("C://new.txt");

The Code

#include <QIODevice>
import QtQuick 2.4
import QtQuick.Controls 1.3
import QtQuick.Window 2.2
import QtQuick.Dialogs 1.2
import QtQml 2.2



ApplicationWindow {
    title: qsTr("File Editor")
    width: 640
    height: 480
    visible: true

    menuBar: MenuBar {
        Menu {
            title: qsTr("&File")
           MenuItem {
                text: qsTr("&Open")
                onTriggered: {
                    var message = ("Hello World!");
                    QFile, file("C://new.txt");
                    file.open(QIODevice::ReadWrite);
                    QTextStream out(&file);
                    out << %message%;
                }
                }
            MenuItem {
                text: qsTr("E&xit")
                onTriggered: Qt.quit();
            }
        }
    }

    MainForm {
        anchors.fill: parent
        button1.onClicked: messageDialog.show(qsTr("Button 1 pressed"))
        button2.onClicked: messageDialog.show(qsTr("Button 2 pressed"))
        button3.onClicked: messageDialog.show(qsTr("Button 3 pressed"))
    }

    MessageDialog {
        id: messageDialog
        title: qsTr("May I have your attention, please?")

        function show(caption) {
            messageDialog.text = caption;
            messageDialog.open();
        }
    }
}
1
I guess 'C://new.txt' should be "C://new.txt". - LogicStuff
Why do you //? Escape character is \\ so maybe you're referring to QFile, file("C:\\new.txt"); instead of QFile, file('C://new.txt');? So I'm talking about \\ and ". More than that, as @SimonWarta says, you cannot mix C++ code with QML... - Iuliu
You are mixing QML code and C++ code. That cannot work. If you need additional help, please format your pasted code correctly first (add 4 spaces in front of each line before you paste to Stack Overflow). Otherwise it is too hard to read for others. - Simon Warta
Signal handlers in QML accepts only javascript code, not C++ code. What you can do is call a C++ method (a SLOT or a Q_INVOKABLE) or create a custom QML type for the purpose. - BaCaRoZzo

1 Answers

1
votes

As stated by Simon Warta and BaCaRoZzo in the comments, you cannot use C++ in QML. You need to use Javascript and create your own custom type for handling file input and output.

Please see this answer.