QFileDialog实现同时选择文件和文件夹,确认取消按钮英文问题解决方法
阅读原文时间:2023年07月08日阅读:1

如下图所示,需求是同时能够选择文件或者文件夹,但是QFileDialog文件窗口类要么只能选文件,要么只能选文件夹,无法同时去选择文件和文件夹;

要实现这样的需求,封装了一个类,实现同时选择文件和文件夹;

头文件

#ifndef MULTIFILEDIRDIALOG_H
#define MULTIFILEDIRDIALOG_H

#include
#include
class MultiFileDirDialog : public QFileDialog
{
Q_OBJECT

public:
MultiFileDirDialog(QWidget *parent);
~MultiFileDirDialog();
QStringList m_listSelect;
signals:

public slots :  
    void go();

private:

};

#endif // MULTIFILEDIRDIALOG_H

源文件

#include "MultiFileDirDialog.h"
#include
#include
#include
#include
#include
#include
#pragma execution_character_set("utf-8")
MultiFileDirDialog::MultiFileDirDialog(QWidget *parent)
: QFileDialog(parent)
{

setOption(QFileDialog::DontUseNativeDialog, true);  
//支持多选  
QListView \*listView = findChild<QListView\*>("listView");  
if (listView)  
    listView->setSelectionMode(QAbstractItemView::ExtendedSelection);  
QTreeView \*treeView = findChild<QTreeView\*>();  
if (treeView)  
    treeView->setSelectionMode(QAbstractItemView::ExtendedSelection);  
QDialogButtonBox \*button = findChild<QDialogButtonBox \*>("buttonBox");

if (button)  
{  
    /\*QPushButton\* selectbutton = button->button(QDialogButtonBox::Open);  
    if (selectbutton)  
    {  
        button->removeButton(selectbutton);  
        const QString strselect = "选择";  
        selectbutton = button->addButton(QDialogButtonBox::Ok);  
        selectbutton->setText("选择");  
    }  
    QPushButton\* canclebutton = button->button(QDialogButtonBox::Cancel);  
    if (canclebutton)  
    {  
        button->removeButton(canclebutton);  
        const QString strcalcel = "取消";  
        canclebutton=button->addButton(strcalcel, QDialogButtonBox::RejectRole);  
        canclebutton->setText("取消");  
    }\*/  
    button->clear();  
    QPushButton\* accpetbutton = new QPushButton(button);  
    accpetbutton->setText("确定");  
    button->addButton((QAbstractButton\*)accpetbutton, QDialogButtonBox::AcceptRole);  
    QPushButton\* cancel = new QPushButton(button);  
    cancel->setText("取消");  
    button->addButton((QAbstractButton\*)cancel, QDialogButtonBox::RejectRole);  
    /\*button->button(QDialogButtonBox::Ok)->setText(tr("确定"));  
    button->button(QDialogButtonBox::Cancel)->setText(tr("取消"));\*/  
    disconnect(button, SIGNAL(accepted()), this, SLOT(accept()));//使链接失效  
    connect(button, SIGNAL(accepted()), this, SLOT(go()));//改成自己的槽  
}  
//无效  
//setLabelText(QFileDialog::FileName, "选择文件:");//英文化文件名称  
//setLabelText(QFileDialog::Reject, "取消");//英文化取消按钮  
//setLabelText(QFileDialog::Accept, "选择");//英文化打开按钮

}

MultiFileDirDialog::~MultiFileDirDialog()
{

}
void MultiFileDirDialog::go()
{
m_listSelect = selectedFiles();
QDialog::accept();
}

其中默认的取消确认按钮始终无法显示中文,所以直接将默认的按钮清除掉,再自己添加两个按钮上去,建立信号槽连接就可以了;

再需求导入选择文件的地方调用创建对象调用;

void AddVedioAnaTask::OnBtnSelectClicked()
{
//ps/ts/hik/mp4/asf/avi/dahv/flv
if (m_addType==0)//导入案件
{
m_pMultiFileDialog->setOption(QFileDialog::ShowDirsOnly, true);
//m_pMultiFileDialog->setFileMode(QFileDialog::Directory);
}
else//导入子文件
{
//m_pMultiFileDialog->setFileMode(QFileDialog::AnyFile);
m_pMultiFileDialog->setOption(QFileDialog::ShowDirsOnly, false);
m_pMultiFileDialog->setNameFilter("vedio(*.mp4 *.avi *.dav *.asf *.wmv *.mpg *.ts *.3g2 *.swf *.flv *.m2ts *.mkv *.3gp *.vob *.rmvb *.mov *.webm *.rm *.mts *.mpeg *.264 *.692 *.800 *.801 *.av *.avx *.djl *.dvr *.g64 *.h3crd *.h64 *.h264 *.ifv *.jmv *.kyd *.lvf *.mpk *.nsf *.nv4 *.ps *.rec *.sdv *.sv5 *.swf *.tm4 *.tp *.bhd *.dat *.h265 *.he4 *.m4c *.pep *.smf *.sv4 *.vdg *.601 *.452094848)");
}

if (m\_pMultiFileDialog->exec() == QDialog::Accepted)  
{  
    QStringList selectFiles = m\_pMultiFileDialog->selectedFiles();  
    if (selectFiles.size() > 0)  
    {  
        QString strItem = "";  
        for (int i = 0; i < selectFiles.size(); i++)  
        {  
            strItem = selectFiles\[i\];  
            if (!strItem.isEmpty())  
            {  
                if (m\_TaskFileMap.find(strItem) == m\_TaskFileMap.end())  
                {  
                    addFileToTable(strItem);  
                }  
            }

        }  
    }  
}  

}

}

}