2011-11-22 105 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); 
}