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

文章目录

      • Qt不同平台宏写法
      • 一、打开文件所在文件夹系统框
      • 二、打开文件属性系统框
      • 三、将字符串转换为时间类型
      • 四、打开数字签名文件(.cer)

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 
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()));

你可能感兴趣的:(linux,qt,windows)