How to launch a finished .bat file using a button in a Qt Quick application under windows?
- I once did a program to store and run files using Qt Quick: github.com/gil9red/ListFiles - gil9red
- Thanks, helped! - asdk
- @ gil9red Please post your comment as a response. - Nicolas Chabanovsky ♦
- @NicolasChabanovsky, I'm afraid the author needs to upload the code that helped him — a lot of things are used, the program is more complicated than the author needed :) - gil9red
- @ gil9red Perhaps you can build the minimum required example? Please note, for example, in my reply . - Nicolas Chabanovsky ♦
|
1 answer
main.qml . Form: two buttons, single line editor. One button opens a dialog for selecting a file and, after selecting, writes the file path to the editor. Another one on the way in the editor tries to run the file through the object 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 is in the qrc resource file called 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->setContextProperty("Util", new Util()); engine.load(QUrl("qrc:/main")); return app.exec(); } Resource file:
<RCC> <qresource prefix="/"> <file alias="main">main.qml</file> </qresource> </RCC> |