2011-11-22 150 views
0

Qt中只有一个对话框来选择文件或文件夹吗?选择一个文件夹或文件

我的意思是我想要一个选项,我们称它为select,通过使用此用户可以从同一个对话框中选择一个文件夹或文件。

+0

好像没有。如何编写自己的文件对话框类?如果不是,那么我只是使用两种不同的操作,一个用于文件,另一个用于目录 – zebrilo

+0

@zebrilo确定不够公平。自己的文件对话框类可能是要走的路。谢谢 – smallB

回答

0

没有内置小工具,但很容易将QDirModel连接到QTreeView并获取选择信号。

下面是一个例子,让你开始:

TEST.CPP

#include <QtGui> 

#include "print.h" 

int main(int argc, char** argv) 
{ 
    QApplication app(argc, argv); 

    QDirModel mdl; 
    QTreeView view; 
    Print print(&mdl); 

    view.setModel(&mdl); 
    QObject::connect(
     view.selectionModel(), 
     SIGNAL(currentChanged(const QModelIndex&,const QModelIndex&)), 
     &print, 
     SLOT(currentChanged(const QModelIndex&,const QModelIndex&))); 
    view.show(); 

    return app.exec(); 
} 

print.h

#ifndef _PRINT_H_ 
#define _PRINT_H_ 
#include <QtGui> 

class Print : public QObject 
{ 
    Q_OBJECT 
    public: 
     Print(QDirModel* mdl); 
     ~Print(); 
    public slots: 
     void currentChanged(const QModelIndex& current, const QModelIndex&); 
    private: 
     QDirModel* mModel; 
     Q_DISABLE_COPY(Print) 
}; 

#endif 

print.cpp

#include "print.h" 

Print::Print(QDirModel* mdl) : QObject(0), mModel(mdl) {} 
Print::~Print() {} 

void Print::currentChanged(const QModelIndex& current, const QModelIndex&) 
{ 
    qDebug() << mModel->filePath(current); 
} 
相关问题