文章目录
- 前言
- 一、头文件
- 二、源文件
- 三、qss文件
- 四、效果
前言
在开发过程中,经常遇到使用文件选择框的情况,qt默认的文件选择框,样式可能与开发的桌面程序差异比较大.此时,我们可以自定义文件选择框,设置其样式,尽量做到样式统一.
一、头文件
#ifndef CUSTOMFILEDIALOG_H
#define CUSTOMFILEDIALOG_H
#include <QDialog>
#include <QFileDialog>
#include <QHBoxLayout>
#include <QMouseEvent>
#include "mymessagedialog.h"
namespace Ui {
class CustomFileDialog;
}
class CustomFileDialog : public QDialog
{
Q_OBJECT
public:
explicit CustomFileDialog(QWidget *parent = nullptr);
~CustomFileDialog();
void createFileDialog(QString);
QStringList selectedFiles() const;
signals:
void selectFileSignal(QString);
private slots:
void on_minBtn_clicked();
void on_closeBtn_clicked();
void onFileDialogFinished(int);
private:
void init();
void loadUI();
private:
Ui::CustomFileDialog *ui;
QFileDialog *m_fileDialog = nullptr;
QHBoxLayout *hLayout;
QPoint last = QPoint(0,0);
QStringList m_fileList;
MyMessageDialog *m_nonRulesMsgDialog = nullptr;
protected:
void mousePressEvent(QMouseEvent *);
void mouseMoveEvent(QMouseEvent *);
void mouseReleaseEvent(QMouseEvent *);
};
#endif // CUSTOMFILEDIALOG_H
二、源文件
#include "customfiledialog.h"
#include "ui_customfiledialog.h"
#include <QComboBox>
#include <QLineEdit>
#include <QDialogButtonBox>
#include <QDebug>
CustomFileDialog::CustomFileDialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::CustomFileDialog)
{
ui->setupUi(this);
this->setWindowFlags(Qt::FramelessWindowHint);
this->setWindowTitle("文件对话框");
this->setWindowIcon(QIcon(":/logo.ico"));
init();
}
CustomFileDialog::~CustomFileDialog()
{
if(m_nonRulesMsgDialog){
delete m_nonRulesMsgDialog;
m_nonRulesMsgDialog = nullptr;
}
delete ui;
}
void CustomFileDialog::init()
{
ui->label->setText("文件选择框");
ui->minBtn->hide();
//
loadUI();
}
void CustomFileDialog::createFileDialog(QString defaultDir){
if(m_fileDialog == nullptr)
m_fileDialog = new QFileDialog(ui->bodyWidget,"选择文件啊",defaultDir);
m_fileDialog->setWindowFlags(Qt::FramelessWindowHint);
m_fileDialog->setFileMode(QFileDialog::FileMode::ExistingFile);
m_fileDialog->setOption(QFileDialog::DontUseNativeDialog, true);
m_fileDialog->setAcceptMode(QFileDialog::AcceptSave);
m_fileDialog->setLabelText(QFileDialog::Accept, tr("选择"));
m_fileDialog->setViewMode(QFileDialog::Detail);
m_fileDialog->setNameFilter("W3060_*_V*.alx");
//
hLayout = new QHBoxLayout;
hLayout->addWidget(m_fileDialog);
hLayout->setSpacing(0);
hLayout->setMargin(0);
ui->bodyWidget->setLayout(hLayout);
//
QLabel* lookinLabel = m_fileDialog->findChild<QLabel*>("lookInLabel");
if (lookinLabel)
lookinLabel->setText("文件目录:");
QComboBox* fileTypeCombo = m_fileDialog->findChild<QComboBox*>("fileTypeCombo");
if (fileTypeCombo)
fileTypeCombo->setMinimumHeight(40);
QComboBox* lookInCombo = m_fileDialog->findChild<QComboBox*>("lookInCombo");
if (lookInCombo)
lookInCombo->setMinimumHeight(40);
QLineEdit* fileNameEdit = m_fileDialog->findChild<QLineEdit*>("fileNameEdit");
if (fileNameEdit)
{
fileNameEdit->setReadOnly(true);
fileNameEdit->setMinimumHeight(40);
}
m_fileDialog->setLabelText(QFileDialog::FileName,"文件目录");
m_fileDialog->setLabelText(QFileDialog::FileType,"文件类型");
QDialogButtonBox *buttonBox = m_fileDialog->findChild<QDialogButtonBox *>("buttonBox");
/*if (QPushButton *button = buttonBox->button(QDialogButtonBox::Open))
{
button->setStyleSheet("QPushButton{background-color: qlineargradient(x1: 0, y1: 0, x2: 1, y2: 0,stop: 0 #00BAFF, stop: 1 #00A1FF);font-size: 12px;color: #FFFFFF;border-radius:2px;}"
"QPushButton:hover{background: #00C1FF;font-size: 12px;color: #FFFFFF;border-radius:2px;}"
"QPushButton:disabled{background: #F0F0F0;border: 1px solid #DDDDDD;font-size: 12px;color: #BBBBBB;border-radius:2px;}");
button->setFixedSize(68, 30);
}*/
if (QPushButton *button = buttonBox->button(QDialogButtonBox::Save))
{
button->setStyleSheet("QPushButton{background-color: rgb(1,141,235);font-size: 20px;color: #FFFFFF;border-radius:2px;}"
"QPushButton:hover{background: rgb(1,141,235);font-size: 20px;color: #FFFFFF;border-radius:2px;}"
"QPushButton:disabled{background: #F0F0F0;border: 1px solid #DDDDDD;font-size: 20px;color: #BBBBBB;border-radius:2px;}");
button->setFixedSize(68, 40);
}
if (QPushButton *button = buttonBox->button(QDialogButtonBox::Cancel))
{
button->setStyleSheet("QPushButton{border: 1px solid #DDDDDD;font-size: 20px;color: #666666;border-radius:2px;}"
"QPushButton:hover{font-size: 20px;color: #00A1FF;border: 1px solid #00A1FF;border-radius:2px;}"
"QPushButton:disabled{background: #F0F0F0;border: 1px solid #DDDDDD;font-size: 20px;color: #BBBBBB;border-radius:2px;}");
button->setText("取消");
button->setFixedSize(68, 40);
}
connect(m_fileDialog, SIGNAL(finished(int)), this, SLOT(onFileDialogFinished(int)));
}
QStringList CustomFileDialog::selectedFiles() const
{
return m_fileList;
}
void CustomFileDialog::onFileDialogFinished(int result)
{
if (result == QDialog::Accepted)
{
//qDebug()<<"CustomFileDialog::onFileDialogFinished Accepted";
m_fileList.clear();
m_fileList = m_fileDialog->selectedFiles();
QRegularExpression regex("^W3060_.*_V\\d+\\.\\d+\\.\\d+\\.alx$");
for(const QString &selectedFile : m_fileList)
{
QString text = selectedFile.section('/',-1);
//qDebug()<<"text ="<<text;
QRegularExpressionMatch match = regex.match(text);
if(match.hasMatch()){
//qDebug()<<"选中的文件符合规则";
emit selectFileSignal(m_fileList.first());
}
else{
//qDebug()<<"选中的文件不符合规则";
if(m_nonRulesMsgDialog == nullptr){
m_nonRulesMsgDialog = new MyMessageDialog;
m_nonRulesMsgDialog->setCancleBtn(false);
}
m_nonRulesMsgDialog->setInfo("安装包不符合命名规则");
m_nonRulesMsgDialog->setModal(true);
m_nonRulesMsgDialog->show();
}
}
//
delete m_fileDialog;
m_fileDialog = nullptr;
delete hLayout;
QDialog::accept();
}
else
{
//qDebug()<<"CustomFileDialog::onFileDialogFinished reject";
delete m_fileDialog;
m_fileDialog = nullptr;
delete hLayout;
QDialog::reject();
}
}
void CustomFileDialog::loadUI()
{
QString fileName = QString(":/image/customFileDialog.qss");
QFile file(fileName);
if(file.open(QIODevice::ReadOnly))
{
QString str = QString::fromUtf8(file.readAll());
this->setStyleSheet(str);
file.close();
}
}
void CustomFileDialog::on_minBtn_clicked()
{
this->showMinimized();
}
void CustomFileDialog::on_closeBtn_clicked()
{
delete m_fileDialog;
m_fileDialog = nullptr;
delete hLayout;
this->close();
}
void CustomFileDialog::mousePressEvent(QMouseEvent *event)
{
if(event->y() < ui->titleWidget->height())
{
last = event->globalPos();
}
}
void CustomFileDialog::mouseMoveEvent(QMouseEvent *event)
{
if(event->y() < ui->titleWidget->height())
{
int dx = event->globalX() - last.x();
int dy = event->globalY() - last.y();
last = event->globalPos();
this->move(this->x() + dx,this->y() + dy);
}
}
void CustomFileDialog::mouseReleaseEvent(QMouseEvent *event)
{
if(event->y() < ui->titleWidget->height())
{
int dx = event->globalX() - last.x();
int dy = event->globalY() - last.y();
this->move(this->x() + dx,this->y() + dy);
}
}
三、qss文件
#titleWidget{
background-color:rgb(1,141,235)/*#4E69E1*/;
}
#label{
color:white;
font:24px;
}
#closeBtn{
border-image:url(:/image/sysclose.png);
width:40px;
height:40px;
}
#minBtn{
border-image: url(:/image/sysmin.png);
width:40px;
height:40px;
}
#minBtn:hover{
border-image: url(:/image/sysmin_hover.png);
}
#minBtn:pressed{
border-image: url(:/image/sysmin_hover.png);
}
QLabel{
font-size:20px;
color:#666666;
}
QComboBox::down-arrow{
image: url(:/image/drop-down.png);
padding-right:4px;
}
QComboBox::down-arrow:on{
image: url(:/image/drop-up.png);
padding-right:4px;
}
QComboBox::drop-down{
background-color:rgb(255,255,255);
}
QComboBox::drop-down:disabled,QComboBox:disabled{
background-color:#f8f8f8
}
QLineEdit,QComboBox{
color: #666666;
border: 1px solid #DDDDDD;
border-radius:2px;
}
QListView, QTreeView{
outline: 0px;
background-color:white;
border: 1px solid #DDDDDD;
color:#666666;
selection-color:#666666;
selection-background-color: #f6f6f6;
border-radius:2px;
}
QListView::item, QTreeView::item{
height:30px;
text-align:center;
}
QListView::item:selected,QTreeView::item:selected{
/*border: 1px solid #6a6ea9;*/
background-color: #1e90ff;
color : rgb(255, 255, 255);
}
QScrollBar:horizontal{
background: #ffffff;
height: 20px;
border-width:0px 10px 0px 10px;
margin-left:6px;
margin-right:20px;
padding-left:14px;
}
QScrollBar::handle:horizontal{
background:rgb(1,141,235)/*#1c86ee*/;
border-radius:3px;
height: 20px;
max-width:40px;
}
QSplitter::handle {
background-color: rgb(255, 255, 255);
}
QScrollBar::sub-line:horizontal{
/*background:transparent;*/
width:20px;
height:20px;
subcontrol-position:left;
subcontrol-origin:margin;
}
QScrollBar::add-line:horizontal{
/*background:transparent;*/
width:20px;
height:20px;
subcontrol-position:right;
subcontrol-origin:margin;
}
QScrollBar:vertical {
background: #ffffff;
width: 20px;
border-width:10px 0px 10px 0px;
margin-top:7px;
margin-bottom:0px;
padding-top:17px;
padding-bottom:24px;
}
QScrollBar::handle:vertical {
background:rgb(1,141,235)/*#1c86ee*/;
border-radius:3px;
width: 20px;
}
QScrollBar::sub-line:vertical {
width:20px;
height:24px;
subcontrol-position:top left;
subcontrol-origin:margin;
}
QScrollBar::add-line:vertical {
width:20px;
height:24px;
subcontrol-position:bottom;
subcontrol-origin:border;
}
QHeaderView::section {
color: #333333;
height:18px;
font-size:12px;
background-color: #f8f8f8;
border:1px solid #eeeeee;
border-left:none;
border-top:none;
padding:6px 10px 1px 10px;
}
/*QHeaderView::down-arrow {
image: url(~/image/drop-down.png);
}
QHeaderView::up-arrow {
image: url(~/image/drop-up.png);
} */
QMenu{
background-color:white;
border-style:none;
}
QMenu::item {
font-size: 12px;
color: #666666;
background-color:#ffffff;
padding:8px;
}
QMenu::item:selected {
background-color:#f6f6f6;
color:#666666;
}
QMenu::item:!enabled {
background-color:white;
color:#999999;
}
QMenu::item:hover {
color:#666666;
}
QMenu::separator {
height: 1px;
background: #eeeeee;
margin: 2px 5px 2px 10px;
}