0

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();
        }
    }
}
BaCaRoZzo
  • 7,502
  • 6
  • 51
  • 82
Andria
  • 4,712
  • 2
  • 22
  • 38
  • I guess `'C://new.txt'` should be `"C://new.txt"`. – LogicStuff Mar 24 '15 at 21:05
  • 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 Mar 24 '15 at 21:14
  • 2
    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 Mar 24 '15 at 21:16
  • 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`](http://developer.nokia.com/community/wiki/Calling_Qt_class_methods_from_QML)) or [create a custom QML type](http://stackoverflow.com/a/8915904/2538363) for the purpose. – BaCaRoZzo Mar 24 '15 at 21:31

1 Answers1

1

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.

Community
  • 1
  • 1
Exa
  • 4,020
  • 7
  • 43
  • 60