上网搜索找到的方法如下:

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

 #ifndef MY_FILE_DIALOG_H
#define MY_FILE_DIALOG_H
#include <QLineEdit>
#include <QDialog>
/*class MyFileDialog : public QFileDialog
{
public:
MyFileDialog(QWidget *parent);
private:
QLineEdit *line_edit;
QFileDialog *p;
};*/ class Dialog : public QDialog
{ public:
Dialog(QWidget *parent = );
QFileDialog* GetFileDlg()
{
return m_filedlg;
} private: QFileDialog *m_filedlg ;
QLabel *textLabel;
QLineEdit *textEdit ; QGridLayout *mainLayout ; QString openFilesPath; }; #endif

myFileDlg.cpp

 #include <QWidget>
#include <QGridLayout>
#include <QFileDialog>
#include <assert.h>
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QLabel>
#include <QDialog> #include "myFileDlg.h" Dialog::Dialog(QWidget *parent): QDialog(parent)
{ /***********************************************************/ QFileDialog *m_filedlg = new QFileDialog(this,Qt::SubWindow); m_filedlg->setOption(QFileDialog::DontUseNativeDialog,true); if(m_filedlg->testOption(QFileDialog::DontUseNativeDialog))
printf("cuccee\n"); //m_filedlg ->getOpenFileName(0,"Select File","D:\\","Video Files (*.avi *.mpg);;3D STL Files (*.stl)",NULL,QFileDialog::DontUseNativeDialog); //m_filedlg->setWindowFlags(Qt::SubWindow) ; /***********************************************************/ textLabel = new QLabel( tr(" StepX: ") );
textEdit = new QLineEdit(); mainLayout = new QGridLayout(this); mainLayout->addWidget( m_filedlg , , , , ) ;
mainLayout->addWidget( textLabel , , , , ) ;
mainLayout->addWidget(textEdit , , , , ) ; /***********************************************************/ //setMinimumHeight( 400 ) ;
//setMinimumWidth( 600 ) ; setSizePolicy( QSizePolicy( QSizePolicy::Expanding , QSizePolicy::Expanding ) ) ; setLayout(mainLayout); }

main.cpp

 #include <stdio.h>
#include <QApplication>
#include <QString>
#include <QtCore/QtPlugin>
#include <QFileDialog>
#include <QLabel>
#include <QLineEdit>
#include <QGridLayout>
#include "myFileDlg.h" int main(int argc, char* argv[])
{
Q_IMPORT_PLUGIN(QWindowsIntegrationPlugin);//之前上一篇文章有描述过这个问题,必须要加入这行,不然不起作用。运行时会出错,http://www.cnblogs //.com/foohack/p/3909320.html
QApplication app(argc,argv); Dialog *dialog =new Dialog(); // dlg ->getOpenFileName(0,"Select File","D:\\","Video Files (*.avi *.mpg);;3D STL Files (*.stl)",NULL);
dialog->setWindowTitle("Select BIG Image File");
dialog->setModal(true);
//dialog->show( ) ; dialog->exec();
return app.exec(); }
04-16 06:07