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
    评论
要将两个.ui界面显示在两个屏幕上,需要使用多窗口和多屏幕技术。 首先,需要在代码中创建两个窗口对象,即两个.ui文件对应的窗口类。可以使用QWidget或QMainWindow作为窗口的基类。 然后,需要获取系统上的所有屏幕,并将窗口分别显示在不同的屏幕上。可以使用QScreen类获取系统上的所有屏幕,并使用QWindow类将窗口显示指定屏幕上。 下面是一个示例代码,演示如何将两个.ui界面显示在两个屏幕上: ```cpp #include <QtWidgets> int main(int argc, char *argv[]) { QApplication app(argc, argv); // 创建第一个窗口对象 QWidget window1; window1.setWindowTitle("Window 1"); // 创建第二个窗口对象 QWidget window2; window2.setWindowTitle("Window 2"); // 获取系统上的所有屏幕 QList<QScreen*> screens = QGuiApplication::screens(); // 将第一个窗口显示在第一个屏幕上 if (screens.count() > 0) { QScreen *screen1 = screens.at(0); window1.show(); window1.setGeometry(screen1->geometry()); window1.setWindowState(Qt::WindowFullScreen); } // 将第二个窗口显示在第二个屏幕上 if (screens.count() > 1) { QScreen *screen2 = screens.at(1); window2.show(); window2.setGeometry(screen2->geometry()); window2.setWindowState(Qt::WindowFullScreen); } return app.exec(); } ``` 在上面的示例代码中,我们首先创建了两个窗口对象window1和window2,并将它们分别显示在第一个和第二个屏幕上。我们使用QGuiApplication::screens()获取系统上的所有屏幕,并使用QScreen::geometry()获取每个屏幕的位置和大小。然后,我们使用QWidget::setGeometry()将窗口显示指定屏幕上,并使用QWidget::setWindowState()将窗口设置为全屏模式。 注意,在使用多屏幕技术时,需要确保窗口的尺寸和位置在不同屏幕之间保持一致,以确保界面的连续性和一致性。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值