Advertisement
Guest User

QFileDialog on WIndows

a guest
Apr 21st, 2014
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.05 KB | None | 0 0
  1. ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  2. main.cpp
  3. -------------
  4. #include "mainwindow.h"
  5. #include <QApplication>
  6.  
  7. int main(int argc, char *argv[])
  8. {
  9.     QApplication a(argc, argv);
  10.     MainWindow w;
  11.     w.show();
  12.  
  13.     return a.exec();
  14. }
  15.  
  16. ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  17. mainwindow.h
  18. -------------
  19. #define MAINWINDOW_H
  20.  
  21. #include <QMainWindow>
  22. #include "ui_mainwindow.h"
  23.  
  24. namespace Ui {
  25. class MainWindow;
  26. }
  27.  
  28. class MainWindow : public QMainWindow
  29. {
  30.     Q_OBJECT
  31.  
  32. public:
  33.     explicit MainWindow(QWidget *parent = 0);
  34.     ~MainWindow();
  35.  
  36. protected slots:
  37.     void on_toolButton_clicked();
  38.  
  39. private:
  40.     Ui::MainWindow *ui;
  41. };
  42.  
  43. #endif // MAINWINDOW_H
  44.  
  45. ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  46. mainwindow.cpp
  47. ---------------
  48. #include <QtWidgets>
  49. #include <QCoreApplication>
  50.  
  51. #include "mainwindow.h"
  52. #include "ui_mainwindow.h"
  53.  
  54. MainWindow::MainWindow(QWidget *parent) :
  55.     QMainWindow(parent),
  56.     ui(new Ui::MainWindow)
  57. {
  58.     ui->setupUi(this);
  59. }
  60.  
  61. MainWindow::~MainWindow()
  62. {
  63.     delete ui;
  64. }
  65.  
  66. void MainWindow::on_toolButton_clicked()
  67. {
  68.     // create a generic FileDialog
  69.     QFileDialog dialog(NULL, tr("Select a disk image"));
  70.     dialog.setNameFilter(tr("Image Files (*.img *.IMG);;*.*"));
  71.     dialog.setFileMode(QFileDialog::AnyFile);  // should be default
  72.     dialog.setViewMode(QFileDialog::Detail);   // should be default
  73.     dialog.setAcceptMode(QFileDialog::AcceptOpen);  // should be default
  74.     dialog.setOption(QFileDialog::DontConfirmOverwrite, true);
  75.  
  76.     QString fileLocation = NULL;
  77.     if (dialog.exec())
  78.     {
  79.         // selectedFiles returns a QStringList - only expecting 1 filename,
  80.         //  so use the zero'th element from that list
  81.         fileLocation = (dialog.selectedFiles())[0];
  82.     }
  83.  
  84.     if (!fileLocation.isNull())
  85.     {
  86.         ui->label->setText(fileLocation);
  87.     }
  88. }
  89.  
  90. ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  91. mainwindow.ui
  92. ----------------
  93. <?xml version="1.0" encoding="UTF-8"?>
  94. <ui version="4.0">
  95.  <class>MainWindow</class>
  96.  <widget class="QMainWindow" name="MainWindow">
  97.   <property name="geometry">
  98.    <rect>
  99.     <x>0</x>
  100.     <y>0</y>
  101.     <width>400</width>
  102.     <height>300</height>
  103.    </rect>
  104.   </property>
  105.   <property name="windowTitle">
  106.    <string>MainWindow</string>
  107.   </property>
  108.   <widget class="QWidget" name="centralWidget">
  109.    <widget class="QToolButton" name="toolButton">
  110.     <property name="geometry">
  111.      <rect>
  112.       <x>180</x>
  113.       <y>110</y>
  114.       <width>25</width>
  115.       <height>19</height>
  116.      </rect>
  117.     </property>
  118.     <property name="text">
  119.      <string>...</string>
  120.     </property>
  121.    </widget>
  122.    <widget class="QLabel" name="label">
  123.     <property name="geometry">
  124.      <rect>
  125.       <x>45</x>
  126.       <y>170</y>
  127.       <width>291</width>
  128.       <height>21</height>
  129.      </rect>
  130.     </property>
  131.     <property name="text">
  132.      <string>TextLabel</string>
  133.     </property>
  134.    </widget>
  135.   </widget>
  136.   <widget class="QMenuBar" name="menuBar">
  137.    <property name="geometry">
  138.     <rect>
  139.      <x>0</x>
  140.      <y>0</y>
  141.      <width>400</width>
  142.      <height>21</height>
  143.     </rect>
  144.    </property>
  145.   </widget>
  146.   <widget class="QToolBar" name="mainToolBar">
  147.    <attribute name="toolBarArea">
  148.     <enum>TopToolBarArea</enum>
  149.    </attribute>
  150.    <attribute name="toolBarBreak">
  151.     <bool>false</bool>
  152.    </attribute>
  153.   </widget>
  154.   <widget class="QStatusBar" name="statusBar"/>
  155.  </widget>
  156.  <layoutdefault spacing="6" margin="11"/>
  157.  <resources/>
  158.  <connections/>
  159. </ui>
  160.  
  161. ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  162. dlgTest.pro
  163. -------------
  164. #-------------------------------------------------
  165. #
  166. # Project created by QtCreator 2014-04-21T15:21:13
  167. #
  168. #-------------------------------------------------
  169.  
  170. QT       += core gui
  171.  
  172. greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
  173.  
  174. TARGET = dlgTest
  175. TEMPLATE = app
  176.  
  177.  
  178. SOURCES += main.cpp\
  179.         mainwindow.cpp
  180.  
  181. HEADERS  += mainwindow.h
  182.  
  183. FORMS    += mainwindow.ui
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement