QT QFileDialog允许选择同时选择多个文件和多个文件夹,也可以只选一个文件或一个文件夹

本文介绍如何使用Qt创建一个自定义的文件选择对话框,通过重写QFileDialog类来实现多选功能,并展示了如何调整对话框的界面元素如列表视图、树视图及按钮行为。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

myfileselect.h

#ifndef MYFILESELECT_H
#define MYFILESELECT_H

#include <QWidget>
#include <QFileDialog>
#include <QListView>
#include <QTreeView>
#include <QDialogButtonBox>
class myFileSelect :public QFileDialog
{
Q_OBJECT
public:
    explicit myFileSelect(QWidget *parent = 0);

signals:

public slots:
        void go();
};

#endif // MYFILESELECT_H

myfileselect.cpp

#include "myfileselect.h"

myFileSelect::myFileSelect(QWidget *parent) :
    QFileDialog(parent)
{

}
void  myFileSelect::go()
{
    QDialog::accept();
}
main.cpp

#include "myfileselect.h"
#include <QtGui/QApplication>
#include <QDebug>
int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    myFileSelect *a =new myFileSelect(this);
        a->setOption(QFileDialog::DontUseNativeDialog,true);

        //支持多选
        QListView *listView = a->findChild<QListView*>("listView");
        if (listView)
            listView->setSelectionMode(QAbstractItemView::ExtendedSelection);
        QTreeView *treeView = a->findChild<QTreeView*>();
        if (treeView)
            treeView->setSelectionMode(QAbstractItemView::ExtendedSelection);
        QDialogButtonBox *button = a->findChild<QDialogButtonBox *>("buttonBox");


       disconnect(button,SIGNAL(accepted()),a,SLOT(accept()));//使链接失效

       connect(button,SIGNAL(accepted()),a,SLOT(go()));//改成自己的槽

    if(a->exec()==QDialog::Accepted)
    {
        qDebug()<<a->selectedFiles();
    }
 return a.exec();
}



参考:http://blog.youkuaiyun.com/redpetallsg/article/details/7008853



评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值