//dialog.h #ifndef DIALOG_H #define DIALOG_H #include <QDialog> #include <QLineEdit> #include <QGridLayout> class Dialog : public QDialog { Q_OBJECT public: Dialog(QWidget *parent = 0); ~Dialog(); private: QPushButton *fileBtn; QLineEdit *fileLineEdit; QGridLayout *mainLayout; private slots: void showFile(); }; #endif // DIALOG_H
//dialog.cpp #include "dialog.h" #include <QGridLayout> #include <QFileDialog> #include <QPushButton> Dialog::Dialog(QWidget *parent) : QDialog(parent) { setWindowTitle(tr("Every Standard Dialog Instance")); fileBtn=new QPushButton; fileBtn->setText(tr("File Standard Dialog Instance")); fileLineEdit=new QLineEdit; mainLayout=new QGridLayout(this); mainLayout->addWidget(fileBtn,0,0); mainLayout->addWidget(fileLineEdit,0,1); connect(fileBtn,SIGNAL(clicked()),this,SLOT(showFile())); } void Dialog::showFile() { QString s=QFileDialog::getOpenFileName(this,"open file dialog","/",tr("Program Files (*.cpp *.c *.h)")); fileLineEdit->setText(s); } Dialog::~Dialog() { }
效果圖:
本文出自 “大白菜” 博客,請務必保留此出處http://zhengyongkun.blog.51cto.com/1163218/1296498