QFileDialog實現同時選擇檔案和資料夾,確認取消按鈕英文問題解決方法

一字千金發表於2023-02-27

如下圖所示,需求是同時能夠選擇檔案或者資料夾,但是QFileDialog檔案視窗類要麼只能選檔案,要麼只能選資料夾,無法同時去選擇檔案和資料夾;

 

要實現這樣的需求,封裝了一個類,實現同時選擇檔案和資料夾;

標頭檔案

#ifndef MULTIFILEDIRDIALOG_H
#define MULTIFILEDIRDIALOG_H

#include <QObject>
#include <QFileDialog>
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<QListView>
#include <QTreeView>
#include <QDialogButtonBox>
#include <QTranslator>
#include <QPushButton>
#include <QDialogButtonBox>
#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);
                    }
                }

            }
        }
    }
}

 

    }

}

相關文章