Advertisement
Guest User

Untitled

a guest
Jun 29th, 2016
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.91 KB | None | 0 0
  1. import QtQuick 2.0
  2. import QtQuick.Controls 1.2
  3. import QtQuick.Layouts 1.1
  4. import QtQuick.Dialogs 1.2
  5. import QtQuick.Window 2.0
  6.  
  7. Window {
  8. width: 300
  9. height: 300
  10. visible: true
  11.  
  12. RowLayout {
  13. anchors.leftMargin: 8
  14. anchors.topMargin: 8
  15. anchors.rightMargin: 8
  16. anchors.bottomMargin: 8
  17. anchors.fill: parent
  18.  
  19. TextField {
  20. id: fileNameTextField
  21. placeholderText: qsTr("Text Field")
  22. }
  23.  
  24. Button {
  25. id: openButton
  26. text: qsTr("...")
  27. Layout.preferredHeight: 23
  28. Layout.preferredWidth: 24
  29. implicitWidth: parent.width
  30.  
  31. onClicked: fileDialog.open()
  32.  
  33. FileDialog {
  34. id: fileDialog
  35. folder: "."
  36. title: qsTr("Choose a file to open")
  37. selectMultiple: true
  38. nameFilters: [ qsTr("All files (*.*)") ]
  39.  
  40. onAccepted: {
  41. var fileName = fileUrl.toString();
  42.  
  43. // remove prefixed "file:///"
  44. fileName = fileName.replace(/^(file:/{3})/,"");
  45.  
  46. // unescape html codes like '%23' for '#'
  47. fileName = decodeURIComponent(fileName);
  48.  
  49. fileNameTextField.text = fileName;
  50. console.log(fileName);
  51. }
  52.  
  53. onRejected: fileDialog.close()
  54. }
  55. }
  56.  
  57. Button {
  58. id: runButton
  59. text: qsTr("Run")
  60. Layout.preferredHeight: 23
  61. Layout.preferredWidth: 86
  62. implicitWidth: parent.width
  63.  
  64. onClicked: {
  65. Util.run(fileNameTextField.text);
  66. }
  67. }
  68. }
  69.  
  70. }
  71.  
  72. #ifndef UTIL_H
  73. #define UTIL_H
  74.  
  75. #include <QObject>
  76.  
  77. class Util: public QObject {
  78. Q_OBJECT
  79.  
  80. public slots:
  81. void run(QString fileName);
  82. };
  83.  
  84.  
  85. #endif // UTIL_H
  86.  
  87. #include "util.h"
  88. #include <QDesktopServices>
  89. #include <QUrl>
  90.  
  91. void Util::run(QString fileName) {
  92. // Для полного соответствия лучше вызывать из того же каталога, где находится файл.
  93. // Алгоритм на примере python:
  94. //
  95. // import os
  96. // old_cwd = os.getcwd()
  97. // os.chdir(os.path.dirname(file_name))
  98. // QDesktopServices.openUrl(QUrl.fromLocalFile(file_name))
  99. // os.chdir(old_cwd)
  100.  
  101. QDesktopServices::openUrl(QUrl::fromLocalFile(fileName));
  102. }
  103.  
  104. #include <QApplication>
  105. #include <QQmlApplicationEngine>
  106. #include <QQmlContext>
  107. #include <QDebug>
  108. #include "util.h"
  109.  
  110. int main(int argc, char* argv[])
  111. {
  112. QApplication app(argc,argv);
  113.  
  114. QQmlApplicationEngine engine;
  115. QQmlContext* rootContext = engine.rootContext();
  116. rootContext->setContextProperty("Util", new Util());
  117.  
  118. engine.load(QUrl("qrc:/main"));
  119.  
  120. return app.exec();
  121. }
  122.  
  123. <RCC>
  124. <qresource prefix="/">
  125. <file alias="main">main.qml</file>
  126. </qresource>
  127. </RCC>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement