Qt/Widgets/FileDialogDefaultSuffix
From ProgrammingExamples
< Qt
FileDialogDefaultSuffix.cpp
#include <QApplication> #include "form.h" int main(int argc, char *argv[]) { QApplication app(argc, argv); Form myForm; myForm.show(); return app.exec(); }
form.cpp
#include <QtGui> #include "form.h" #include <iostream> #include <string> #include <QFileDialog> #include <QString> Form::Form(QWidget* parent) : QWidget(parent) { setupUi(this); } void Form::on_btnSave_clicked() { #include <QtGui> #include "form.h" #include <iostream> #include <string> #include <QFileDialog> #include <QString> Form::Form(QWidget* parent) : QWidget(parent) { setupUi(this); } void Form::on_btnSave_clicked() { /* // This doesn't work because it still calls the static getSaveFileName QFileDialog dialog(this); //dialog.setFilter(".png .bmp"); dialog.setNameFilter("*.png;;*.bmp"); dialog.setDefaultSuffix("png"); QString fileName = dialog.getSaveFileName(); */ QFileDialog dialog(this); dialog.setNameFilter("*.png;;*.bmp"); dialog.setDefaultSuffix("png"); dialog.exec(); QString fileName = dialog.selectedFiles()[0]; std::cout << "Set save filename: " << fileName.toStdString() << std::endl; }
form.h
#ifndef FORM_H #define FORM_H #include <QWidget> #include "ui_form.h" class Form : public QWidget, private Ui::Form { Q_OBJECT public: Form(QWidget *parent = 0); private slots: void on_btnSave_clicked(); }; #endif
form.ui
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>Form</class>
<widget class="QWidget" name="Form">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>400</width>
<height>300</height>
</rect>
</property>
<property name="windowTitle">
<string>Form</string>
</property>
<widget class="QPushButton" name="btnOpenFile">
<property name="geometry">
<rect>
<x>40</x>
<y>90</y>
<width>91</width>
<height>27</height>
</rect>
</property>
<property name="text">
<string>Open File</string>
</property>
</widget>
<widget class="QPushButton" name="btnOpenDirectory">
<property name="geometry">
<rect>
<x>170</x>
<y>90</y>
<width>181</width>
<height>27</height>
</rect>
</property>
<property name="text">
<string>Open Directory</string>
</property>
</widget>
<widget class="QPushButton" name="btnSave">
<property name="geometry">
<rect>
<x>140</x>
<y>180</y>
<width>91</width>
<height>28</height>
</rect>
</property>
<property name="text">
<string>Save</string>
</property>
</widget>
</widget>
<resources/>
<connections/>
</ui>CMakeLists.txt
cmake_minimum_required(VERSION 2.6) PROJECT(FileDialogDefaultSuffix) FIND_PACKAGE(Qt4 REQUIRED) INCLUDE(${QT_USE_FILE}) include_directories(${include_directories} ${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_CURRENT_BINARY_DIR}) QT4_WRAP_UI(UISrcs form.ui) QT4_WRAP_CPP(MOCSrcs form.h) ADD_EXECUTABLE(FileDialogDefaultSuffix FileDialogDefaultSuffix.cpp form.cpp ${MOCSrcs} ${UISrcs}) TARGET_LINK_LIBRARIES(FileDialogDefaultSuffix ${QT_LIBRARIES})