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
//Subclass QFileDialog for customize allow select both file/folder
Q_OBJECT
private:
public slots:
void chooseClicked();
public:
FileDialog();
};
//Subclass QFileDialog for customize allow select both file/folder
class FileDialog : public QFileDialog{
Q_OBJECT
private:
QListView *m_listView;
QTreeView *m_treeView;
QPushButton *m_btnOpen;
QStringList m_selectedFiles;
public slots:
void chooseClicked();
public:
FileDialog();
QStringList selectedFiles();
bool eventFilter(QObject* watched, QEvent* event);
};
To copy to clipboard, switch view to plain text mode
{
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;
}
FileDialog::FileDialog() : QFileDialog()
{
m_btnOpen = NULL;
m_listView = NULL;
m_treeView = NULL;
m_selectedFiles.clear();
this->setOption(QFileDialog::DontUseNativeDialog, true);
this->setFileMode(QFileDialog::Directory);
QList<QPushButton*> btns = this->findChildren<QPushButton*>();
for (int i = 0; i < btns.size(); ++i) {
QString text = btns[i]->text();
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_listView->setSelectionMode(QAbstractItemView::ExtendedSelection);
}
m_treeView = findChild<QTreeView*>();
if (m_treeView) {
m_treeView->setSelectionMode(QAbstractItemView::ExtendedSelection);
}
}
bool FileDialog::eventFilter( QObject* watched, QEvent* event )
{
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();
foreach (QModelIndex index, indexList)
{
if (index.column()== 0)
{
m_selectedFiles.append(this->directory().absolutePath() + index.data().toString());
}
}
QDialog::accept();
}
QStringList FileDialog::selectedFiles()
{
return m_selectedFiles;
}
To copy to clipboard, switch view to plain text mode
Bookmarks