Ready file launch.



  • How do we start a ready file...bat with a Qt Quick Clip on the windows?


  • QA Engineer

    https://github.com/gil9red/QML_run_file ♪

    main.qml♪ Form: two buttons, one-stop editor. One button opens a dialogue to choose the file and, after choosing, records the way the file goes to the editor. The other one on his way to the editor is trying to run the file through the site with++ Util

    import QtQuick 2.0
    import QtQuick.Controls 1.2
    import QtQuick.Layouts 1.1
    import QtQuick.Dialogs 1.2
    import QtQuick.Window 2.0
    

    Window {
    width: 300
    height: 300
    visible: true

    RowLayout {
        anchors.leftMargin: 8
        anchors.topMargin: 8
        anchors.rightMargin: 8
        anchors.bottomMargin: 8
        anchors.fill: parent
    
        TextField {
            id: fileNameTextField
            placeholderText: qsTr("Text Field")
        }
    
        Button {
            id: openButton
            text: qsTr("...")
            Layout.preferredHeight: 23
            Layout.preferredWidth: 24
            implicitWidth: parent.width
    
            onClicked: fileDialog.open()
    
            FileDialog {
                id: fileDialog
                folder: "."
                title: qsTr("Choose a file to open")
                selectMultiple: true
                nameFilters: [ qsTr("All files (*.*)") ]
    
                onAccepted: {
                    var fileName = fileUrl.toString();
    
                    // remove prefixed "file:///"
                    fileName = fileName.replace(/^(file:\/{3})/,"");
    
                    // unescape html codes like '%23' for '#'
                    fileName = decodeURIComponent(fileName);
    
                    fileNameTextField.text = fileName;
                    console.log(fileName);
                }
    
                onRejected: fileDialog.close()
            }
        }
    
        Button {
            id: runButton
            text: qsTr("Run")
            Layout.preferredHeight: 23
            Layout.preferredWidth: 86
            implicitWidth: parent.width
    
            onClicked: {
                Util.run(fileNameTextField.text);
            }
        }
    }
    

    }

    Util.h/cpp

    #ifndef UTIL_H
    #define UTIL_H

    #include <QObject>

    class Util: public QObject {
    Q_OBJECT

    public slots:
        void run(QString fileName);
    

    };

    #endif // UTIL_H

    /

    #include "util.h"
    #include <QDesktopServices>
    #include <QUrl>

    void Util::run(QString fileName) {
    // Для полного соответствия лучше вызывать из того же каталога, где находится файл.
    // Алгоритм на примере python:
    //
    // import os
    // old_cwd = os.getcwd()
    // os.chdir(os.path.dirname(file_name))
    // QDesktopServices.openUrl(QUrl.fromLocalFile(file_name))
    // os.chdir(old_cwd)

    QDesktopServices::openUrl(QUrl::fromLocalFile(fileName));
    

    }

    main.cpp
    main.qml in the resource file qrc by name main

    #include <QApplication>
    #include <QQmlApplicationEngine>
    #include <QQmlContext>
    #include <QDebug>
    #include "util.h"

    int main(int argc, char* argv[])
    {
    QApplication app(argc,argv);

    QQmlApplicationEngine engine;
    QQmlContext* rootContext = engine.rootContext();
    rootContext-&gt;setContextProperty("Util", new Util());
    
    engine.load(QUrl("qrc:/main"));
    
    return app.exec();
    

    }

    Resource base:

    <RCC>
    <qresource prefix="/">
    <file alias="main">main.qml</file>
    </qresource>
    </RCC>




Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2