QFileDialog to select files AND folders
The problem that i have i that QFileDialog doesnt seem to support choosing files or folders. FileMode flags dont have one that mixes both.
Is there any way to make a QFileDialog able to select files and folders?
FileMode::AnyFiles let you select a file, but if you choose a folder it opens it and shows you the files in it and FileMode::Directory only let you select folders. Since im doing a compressor i must allow the user to select files, folders, or both at the same time (multiple files and folders). Is there any other way that doesnt require craeting a new dialog from scratch?
thanks!
Re: QFileDialog to select files AND folders
I tried to handle the same problem and I found a solution after searching in QT forums. The following is the codes that you could try:
Code:
_f_dlg
->setOption
(QFileDialog::DontUseNativeDialog,
true);
// Try to select multiple files and directories at the same time in QFileDialog
QListView *l
= _f_dlg
->findChild<QListView
*>
("listView");
if (l) {
}
QTreeView *t
= _f_dlg
->findChild<QTreeView
*>
();
if (t) {
}
int nMode = _f_dlg->exec();
Re: QFileDialog to select files AND folders
Thanks! that's a nice workaround! i'll try it
Re: QFileDialog to select files AND folders
This is the way i'm make it possible, by subclass QFileDialog, connect "Choose" button signal to yourself slot, and always setEnable(true) for "Choose" button
Code:
//Subclass QFileDialog for customize allow select both file/folder
Q_OBJECT
private:
public slots:
void chooseClicked();
public:
FileDialog();
};
Code:
{
m_btnOpen = NULL;
m_listView = NULL;
m_treeView = NULL;
m_selectedFiles.clear();
this
->setOption
(QFileDialog::DontUseNativeDialog,
true);
QList<QPushButton*> btns = this->findChildren<QPushButton*>();
for (int i = 0; i < btns.size(); ++i) {
if (text.toLower().contains("open") || text.toLower().contains("choose"))
{
m_btnOpen = btns[i];
break;
}
}
if (!m_btnOpen) return;
m_btnOpen->installEventFilter(this);
//connect(m_btnOpen, SIGNAL(changed()), this, SLOT(btnChanged()))
m_btnOpen->disconnect(SIGNAL(clicked()));
connect(m_btnOpen, SIGNAL(clicked()), this, SLOT(chooseClicked()));
m_listView = findChild<QListView*>("listView");
if (m_listView) {
}
m_treeView = findChild<QTreeView*>();
if (m_treeView) {
}
}
{
QPushButton *btn
= qobject_cast<QPushButton
*>
(watched
);
if (btn)
{
if(event
->type
()==QEvent::EnabledChange) { if (!btn->isEnabled()) {
btn->setEnabled(true);
}
}
}
return QWidget::eventFilter(watched, event
);
}
void FileDialog::chooseClicked()
{
QModelIndexList indexList = m_listView->selectionModel()->selectedIndexes();
{
if (index.column()== 0)
{
m_selectedFiles.append(this->directory().absolutePath() + index.data().toString());
}
}
}
{
return m_selectedFiles;
}