Qt 利用QFileDialog实现一个带预览功能的文件选择预览界面

Qt 利用QFileDialog实现一个带预览功能的文件选择预览界面

需求描述

实现一个文件预览的功能,FileDialog中切换不同的文件,能将文件中我们需要展示的信息显示出来。在实现过程思路就是利用Qt自带的QFileDialog 以 Qwidget 的方式添加到新的 QDialog 中,然后新增一个预览区域的QWidget来实现具体预览交互功能,在实现过程中遇到如下问题:

  • 由于 使用了qss文件进行界面优化,QFileDialog部分控件有效果,十分不协调。
需要实现的功能点
  • QFileDialog的界面设置和优化
  • 去掉QFileDialog右下角拖动改变大小功能
    file_dialog->setSizeGripEnabled(false);
  • QFileDialog切换文件的信号处理,利用QFileDialog原有的打开和取消功能来实现QDialog的对应功能
  • QFileDialog 去掉右键菜单和对应的中文设置
主要代码

界面设置代码

void tdms_widget_t::create_file_dialog() {
	auto  file_dialog = new QFileDialog(this, Qt::SubWindow | Qt::FramelessWindowHint);
	file_dialog->setAttribute(Qt::WA_TranslucentBackground);
	file_dialog->setOption(QFileDialog::DontUseNativeDialog, true);
    
    //去掉右下角的拖动
	file_dialog->setSizeGripEnabled(false);
    //设置文件类型
	file_dialog->setNameFilter(u8"TDMS文件(*.tdms)");
    //切换位置
	file_dialog->setLabelText(QFileDialog::LookIn, u8"文件目录:");
	file_dialog->setLabelText(QFileDialog::FileName, u8"文件名字:");
	file_dialog->setLabelText(QFileDialog::FileType, u8"文件类型:");
	file_dialog->setLabelText(QFileDialog::Accept, u8"打开");
	file_dialog->setLabelText(QFileDialog::Reject, u8"取消");
    
    //设置模式
	file_dialog->setViewMode(QFileDialog::List);
    //设置stylesheet
	QListView* pListView = file_dialog->findChild<QListView*>("listView");
	if (pListView) {
		pListView->setContextMenuPolicy(Qt::NoContextMenu);
	}

	QTreeView* pTreeView = file_dialog->findChild<QTreeView*>("treeView");
	if (pTreeView) {
		pTreeView->header()->setDefaultAlignment(Qt::AlignLeft | Qt::AlignVCenter);
		pTreeView->setContextMenuPolicy(Qt::NoContextMenu);
	}

	QLineEdit* fileNameEdit = file_dialog->findChild<QLineEdit*>("fileNameEdit");
	if (fileNameEdit) {
		fileNameEdit->setContextMenuPolicy(Qt::NoContextMenu);
	}
	
	QComboBox* fileTypeCombo = file_dialog->findChild<QComboBox*>("fileTypeCombo");
	if (fileTypeCombo) {
		fileTypeCombo->setMinimumHeight(24);
		fileTypeCombo->setView(new QListView());
	}

	QComboBox* lookInCombo = file_dialog->findChild<QComboBox*>("lookInCombo");
	if (lookInCombo) {
		lookInCombo->setMinimumHeight(24);
		lookInCombo->setView(new QListView());
	}

	QString style_str = "QPushButton{color:#ffffff;font - size:14px;background: #29363A;border:1px solid #3BB09E;margin: 2px;}"
		                "QPushButton::hover{color:#3CB09E;background: #29474A;}"
	                    "QPushButton::disabled{color:#ffffff;border: 1px solid #333333;}";

	QDialogButtonBox* buttonBox = file_dialog->findChild<QDialogButtonBox*>("buttonBox");
	if (QPushButton* button = buttonBox->button(QDialogButtonBox::Open)) {
		button->setStyleSheet(style_str);
		button->setFixedSize(80, 32);
	}
	if (QPushButton* button = buttonBox->button(QDialogButtonBox::Cancel)) {
		button->setStyleSheet(style_str);
		button->setFixedSize(80, 32);
	}
    //连接关联函数
	//buttonBox->disconnect();
	connect(buttonBox, SIGNAL(accepted()), this, SLOT(onAccepted()));
	connect(buttonBox, SIGNAL(rejected()), this, SLOT(onRejected()));
    //
	connect(file_dialog, SIGNAL(fileSelected(const QString)), this, SLOT(file_selected(const QString)));
    //连接文件切换的信号  作为预览功能的入口
	connect(file_dialog, SIGNAL(currentChanged(const QString)), this, SLOT(file_selected(const QString)));

	main_layout_->addWidget(file_dialog,1);
}

void tdms_widget_t::onAccepted() {
	this->accept();
}
void tdms_widget_t::onRejected() {
	this->reject();
}

void tdms_widget_t::channel_changed(int channel)
{
	file_path_.channel_ = channel;
	rtsa_file_setting(rtsa_, file_path_);
	::rtsa_start(rtsa_, runtime_type_t::ANALYSIS);
}
void tdms_widget_t::file_selected(const QString data) {
	path_ = data;
	read_tdms_info();
}
  • 2
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值