Qt实现在多个屏幕下指定一个显示

    在工作中遇到多个屏幕显示运行系统时,将当前应用程序通过配置文件指定到某一个屏幕中显示。
    配置文件放置到bin/config下,实现代码如下:

//获取当前软件所在屏幕的序号	
	int currentScreenIndex = 0;
	QJsonObject json;
	QString strPath = QCoreApplication::applicationDirPath();
	QDir dir(strPath);
	dir.cd("config/");
	QString _strPath = dir.absolutePath() + "/screen.json";
	QFile _file(_strPath);
	if (_file.open(QIODevice::ReadWrite))
	{
		QByteArray _content = _file.readAll();
		QJsonParseError error;
		QJsonDocument _doc = QJsonDocument::fromJson(_content, &error);
		if (!_doc.isEmpty())
		{
			json = _doc.object();			
			currentScreenIndex = json["screenIndex"].toInt();
			//如果设置的index值超过屏幕数量,设为0
			if (currentScreenIndex >= QGuiApplication::screens().count())
				currentScreenIndex = 0;
		}
	}
	QDesktopWidget* desktop = QApplication::desktop();
	desktop->screenGeometry(currentScreenIndex);
	int current_screen = desktop->screenNumber(&w);
	QRect _rect = desktop->screenGeometry(currentScreenIndex);
	w.setWindowState(Qt::WindowMaximized);	//将当前程序全屏显示
	w.setGeometry((_rect.width() - w.width()) / 2 + _rect.x(), (_rect.height() - w.height()) / 2, w.width(), w.height());	

screen.json的文件格式比较简单,如下:

{
	"screenIndex":0
}

好了,代码就是这么简单。但是里面有很多知识点需要留心记忆。

  • 0
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值