QFileDialog实现同时选择文件和文件夹,确认取消按钮英文问题解决方法
2023/2/28 1:20:51
本文主要是介绍QFileDialog实现同时选择文件和文件夹,确认取消按钮英文问题解决方法,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
如下图所示,需求是同时能够选择文件或者文件夹,但是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); } } } } } }
}
}
这篇关于QFileDialog实现同时选择文件和文件夹,确认取消按钮英文问题解决方法的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-12-25初学者必备:订单系统资料详解与实操教程
- 2024-12-24内网穿透资料入门教程
- 2024-12-24微服务资料入门指南
- 2024-12-24微信支付系统资料入门教程
- 2024-12-24微信支付资料详解:新手入门指南
- 2024-12-24Hbase资料:新手入门教程
- 2024-12-24Java部署资料
- 2024-12-24Java订单系统资料:新手入门教程
- 2024-12-24Java分布式资料入门教程
- 2024-12-24Java监控系统资料详解与入门教程