#include "cfiledialog.h"
#include <QListView>
#include <QTreeView>
#include <QDialogButtonBox>
//#include "CFileDialog.h"
CFileDialog::CFileDialog(QWidget *parent)
: QFileDialog(parent)
{
this->setOption(QFileDialog::DontUseNativeDialog,true);
//this->setFileMode(QFileDialog::AnyFile);
**this->setFileMode(QFileDialog::Directory);**
//支持多选
QTreeView *pTreeView = this->findChild<QTreeView*>();
if (pTreeView)
pTreeView->setSelectionMode(QAbstractItemView::MultiSelection);
QListView *pListView = this->findChild<QListView*>("listView");
if (pListView)
pListView->setSelectionMode(QAbstractItemView::MultiSelection);
QDialogButtonBox *pButton = this->findChild<QDialogButtonBox *>("buttonBox");
disconnect(pButton, SIGNAL(accepted()), this, SLOT(accept()));//使链接失效
connect(pButton, SIGNAL(accepted()), this, SLOT(onChiose()));//改成自己的槽
//this->exec();
}
CFileDialog::~CFileDialog()
{
}
void CFileDialog::onChiose()
{
QDialog::accept();
}
#include "mainwindow.h"
#include <QDebug>
#include <cfiledialog.h>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
CFileDialog* w =new CFileDialog(this);
w->show();
if ( w->exec() )
{
QStringList filepath = w->selectedFiles();
for (int i=0;i<filepath.size();i++)
{
QFileInfo fileinfo(filepath.at(i));
// QDir dir = filepath.at(i);
// QFileInfoList dielist = dir.entryInfoList();
// dielist.at(1).isFile();
if( fileinfo.isFile() )
qDebug() << "is file" <<filepath.at(i);
else if(fileinfo.isDir())
qDebug() << "is dir " <<filepath.at(i);
}
}
}
MainWindow::~MainWindow()
{
}
在这里插入代码片
同时可以显示文件和目录了,但是有一个小bug
必须先点击目录才可以选择文件,不然choose按钮会变成灰色,这样后将diretory栏的信息都删除就又都可以选了。。。