Qt中Windows与Linux不同的实现方式

Qt不同平台宏写法

// 三个平台,windos、linux、mac
#ifdef Q_OS_WIN
	...
#elif defined(Q_OS_LINUX)
	...
#else
	...
#endif

// Q_OS_UNIX,一般指linux和mac平台。Q_OS_MAC指mac平台

一、打开文件所在文件夹系统框

Windows

QStringList params;
params.append("/select,");
params.append(filePath);
QProcess::startDetached("explorer.exe", params);

Linux

QStringList list("file://" + filePath);
QString strPid = QString::number(qApp->applicationPid());
QDBusInterface dbusInterfase("org.freedesktop.FileManager1",
							 "/org/freedesktop/FileManager1",
							 "org.freedesktop.FileManager1",
							 QDBusConnection::sessionBus());
QDBusReply<void> reply = dbusInterfase.call("ShowItems", list, strPid);
if (reply.error().type != QDBusError::NoError)
	//容错处理

tips:由于linux下桌面环境太多,有GNOME、KDE、Xfce等,不同的桌面环境文件管理器不同,可以使用echo $XDG_CURRENT_DESKTOP命令查看使用的是什么桌面环境。所以最好打开系统内置框,使用进程间通信DBus。参考链接如下:
https://freedesktop.org/wiki/Specifications/file-manager-interface/


二、打开文件属性系统框

Windows

SHELLEXECUTEINFO ShExecInfo ={0};
ShExecInfo.cbSize = sizeof(SHELLEXECUTEINFO);
ShExecInfo.fMask = SEE_MASK_INVOKEIDLIST ;
ShExecInfo.hwnd = NULL;
ShExecInfo.lpVerb = L"properties";
ShExecInfo.lpFile = filePath.toStdWString.c_str();
ShExecInfo.lpParameters = NULL;
ShExecInfo.lpDirectory = NULL;
ShExecInfo.nShow = SW_SHOW;
ShExecInfo.hInstApp = NULL;
ShellExecuteEx(&ShExecInfo);

Linux

QStringList list("file://" + filePath);
QString strPid = QString::number(qApp->applicationPid());
QDBusInterface dbusInterfase("org.freedesktop.FileManager1",
							 "/org/freedesktop/FileManager1",
							 "org.freedesktop.FileManager1",
							 QDBusConnection::sessionBus());
QDBusReply<void> reply = dbusInterfase.call("ShowItemProperties", list, strPid);
if (reply.error().type != QDBusError::NoError)
	//容错处理

三、将字符串转换为时间类型

Windows

QDateTime fromString(const QString &s, const QString &format);

Linux

// linux既可以使用QDateTime,也可以使用strptime。tm结构体必须初始化
#include <time.h>
QString strDateTime =1995/07/08;
struct tm timeStruct = {0};
strptime(strDateTime.toUtf8().data(), "%Y/%m/%d %H:%M:%S", &timeStruct);

四、打开数字签名文件(.cer)

Windows
采用windows SDK

	//创建证书上下文
	PCCERT_CONTEXT pCertContext = CertCreateCertificateContext(PKCS_7_ASN_ENCODING | X509_ASN_ENCODING, pbCertData, nCertLen);
	if (pCertContext == NULL)
		return ;
	//填充证书信息
	CRYPTUI_VIEWCERTIFICATE_STRUCT ViewInfo;
	...
	//创建UI
	BOOL bRet = CryptUIDlgViewCertificate(&ViewInfo, NULL);
	CertFreeCertificateContext(pCertContext);

Linux

QFileInfo fileInfo(strSignPath);
// QDesktopServices::openUrl在linux下会调用xdg-open,会使用系统默认的应用程序打开此文件
// 如果系统没有默认软件还是打不开,可以手动安装 “sudo apt-get install gcr” 或者 “gcr-viewer”
QDesktopServices::openUrl(QUrl::fromLocalFile(fileInfo.canonicalFilePath()));
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值