上网搜索找到的方法如下:
http://www.qtforum.org/article/20841/how-to-add-a-qwidget-in-qfiledialog.html#post78422
http://www.qtcentre.org/threads/42858-Creating-a-Custom-FileOpen-Dialog
http://stackoverflow.com/questions/16987916/add-widgets-to-qfiledialog
其实前两个链接里面提供的方法我都试了,行不通,这些方法都是直接在QFileDialog上添加widget,首先都得获得QFileDialog的layout,估计是他们用的是以前Qt的版本吧,反正5.0.0以上的版本是没有用了,用了直接程序会Abort。这个他们说了也不推荐用。因为Qt版本内部的数据结构是不断变化的。
所以以下我采用了一种巧妙的方式,绕过了直接在QFileDialog上添加widget,实质上是自己创建一个QDialog,把QFileDialog作为一个widget添加到QDialog里面。
以下是Demo:
myFileDlg.h
1 #ifndef MY_FILE_DIALOG_H 2 #define MY_FILE_DIALOG_H 3 #include <QLineEdit> 4 #include <QDialog> 5 /*class MyFileDialog : public QFileDialog 6 { 7 public: 8 MyFileDialog(QWidget *parent); 9 private: 10 QLineEdit *line_edit; 11 QFileDialog *p; 12 };*/ 13 14 class Dialog : public QDialog 15 { 16 17 public: 18 Dialog(QWidget *parent = 0); 19 QFileDialog* GetFileDlg() 20 { 21 return m_filedlg; 22 } 23 24 25 private: 26 27 QFileDialog *m_filedlg ; 28 QLabel *textLabel; 29 QLineEdit *textEdit ; 30 31 QGridLayout *mainLayout ; 32 33 QString openFilesPath; 34 35 }; 36 37 38 39 40 #endif
myFileDlg.cpp
1 #include <QWidget> 2 #include <QGridLayout> 3 #include <QFileDialog> 4 #include <assert.h> 5 #include <QHBoxLayout> 6 #include <QVBoxLayout> 7 #include <QLabel> 8 #include <QDialog> 9 10 #include "myFileDlg.h" 11 12 13 14 Dialog::Dialog(QWidget *parent): QDialog(parent) 15 { 16 17 /***********************************************************/ 18 19 QFileDialog *m_filedlg = new QFileDialog(this,Qt::SubWindow); 20 21 m_filedlg->setOption(QFileDialog::DontUseNativeDialog,true); 22 23 if(m_filedlg->testOption(QFileDialog::DontUseNativeDialog)) 24 printf("cuccee "); 25 26 27 28 //m_filedlg ->getOpenFileName(0,"Select File","D:\","Video Files (*.avi *.mpg);;3D STL Files (*.stl)",NULL,QFileDialog::DontUseNativeDialog); 29 30 31 32 //m_filedlg->setWindowFlags(Qt::SubWindow) ; 33 34 /***********************************************************/ 35 36 textLabel = new QLabel( tr(" StepX: ") ); 37 textEdit = new QLineEdit(); 38 39 mainLayout = new QGridLayout(this); 40 41 42 mainLayout->addWidget( m_filedlg , 0 , 0 , 2 , 1 ) ; 43 mainLayout->addWidget( textLabel , 2 , 0 , 1 , 1 ) ; 44 mainLayout->addWidget(textEdit , 2 , 1 , 1 , 1 ) ; 45 46 47 /***********************************************************/ 48 49 //setMinimumHeight( 400 ) ; 50 //setMinimumWidth( 600 ) ; 51 52 setSizePolicy( QSizePolicy( QSizePolicy::Expanding , QSizePolicy::Expanding ) ) ; 53 54 setLayout(mainLayout); 55 56 57 }
main.cpp
1 #include <stdio.h> 2 #include <QApplication> 3 #include <QString> 4 #include <QtCore/QtPlugin> 5 #include <QFileDialog> 6 #include <QLabel> 7 #include <QLineEdit> 8 #include <QGridLayout> 9 #include "myFileDlg.h" 10 11 12 int main(int argc, char* argv[]) 13 { 14 Q_IMPORT_PLUGIN(QWindowsIntegrationPlugin);//之前上一篇文章有描述过这个问题,必须要加入这行,不然不起作用。运行时会出错,http://www.cnblogs //.com/foohack/p/3909320.html 15 QApplication app(argc,argv); 16 17 18 19 20 21 22 23 24 Dialog *dialog =new Dialog(0); 25 26 // dlg ->getOpenFileName(0,"Select File","D:\","Video Files (*.avi *.mpg);;3D STL Files (*.stl)",NULL); 27 dialog->setWindowTitle("Select BIG Image File"); 28 dialog->setModal(true); 29 //dialog->show( ) ; 30 31 dialog->exec(); 32 return app.exec(); 33 34 35 36 }