一、设置HDMI-2为主屏

在main函数里面添加:

#include "mainwindow.h"
#include <QApplication>int main(int argc, char *argv[])
{QApplication a(argc, argv);{long nTotal = 0;char cData[2048] = {0};FILE *fstream = popen("xrandr | grep 'HDMI-2 connected'", "r");if(fstream){while(fgets(cData, sizeof(cData), fstream)){nTotal++;}pclose(fstream);}if(nTotal > 0){system("xrandr --output HDMI-2 --primary --left-of HDMI-1 --auto");}}MainWindow w;w.show();return a.exec();
}

二、代码切换程序在主屏或副屏显示

mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H#include <QMainWindow>namespace Ui {
class MainWindow;
}class MainWindow : public QWidget
{Q_OBJECTpublic:explicit MainWindow(QWidget *parent = 0);~MainWindow();private slots:void StartTimer();void OnClickBtn1();void OnClickBtn2();void OnClickBtn3();void OnClickBtn4();void OnClickBtn5();private:void SetPos(int nScreen);private:void timerEvent(QTimerEvent*);private:int m_nTimerID;private:int m_nScreebResolutions;int m_nScreenID;
};#endif // MAINWINDOW_H

MainWIndow.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QLabel>
#include <QScreen>
#include <QPushButton>
#include <QHBoxLayout>
#include <QDesktopWidget>void OutPut(const char* cData)
{FILE* pFile = fopen("/opt/NVMS/111.log", "ab+");if(pFile){fwrite(cData, 1, strlen(cData), pFile);fclose(pFile);}
}MainWindow::MainWindow(QWidget *parent) :QWidget(parent)
{QLabel* p = new QLabel("123", this);QPushButton* qBtn1 = new QPushButton("0_1920", this);qBtn1->setFixedSize(100, 50);connect(qBtn1, &QPushButton::clicked, this, &MainWindow::OnClickBtn1);QPushButton* pBtn2 = new QPushButton("0_3840", this);pBtn2->setFixedSize(100, 50);connect(pBtn2, &QPushButton::clicked, this, &MainWindow::OnClickBtn2);QPushButton* qBtn3 = new QPushButton("1_1920", this);qBtn3->setFixedSize(100, 50);connect(qBtn3, &QPushButton::clicked, this, &MainWindow::OnClickBtn3);QPushButton* pBtn4 = new QPushButton("1_3840", this);pBtn4->setFixedSize(100, 50);connect(pBtn4, &QPushButton::clicked, this, &MainWindow::OnClickBtn4);QPushButton* pBtn5 = new QPushButton("Query", this);pBtn5->setFixedSize(100, 50);connect(pBtn5, &QPushButton::clicked, this, &MainWindow::OnClickBtn5);QHBoxLayout* pLayout = new QHBoxLayout(this);pLayout->addWidget(p, 0, Qt::AlignLeft | Qt::AlignTop);pLayout->addWidget(qBtn1, 0, Qt::AlignLeft | Qt::AlignTop);pLayout->addWidget(pBtn2, 0, Qt::AlignLeft | Qt::AlignTop);pLayout->addWidget(qBtn3, 0, Qt::AlignLeft | Qt::AlignTop);pLayout->addWidget(pBtn4, 0, Qt::AlignLeft | Qt::AlignTop);pLayout->addWidget(pBtn5, 0, Qt::AlignLeft | Qt::AlignTop);//m_nScreenID = 0;m_nScreebResolutions = 0;SetPos(m_nScreenID);
}MainWindow::~MainWindow()
{}void MainWindow::StartTimer()
{}void MainWindow::timerEvent(QTimerEvent*)
{SetPos(m_nScreenID);if(m_nScreenID == 1){OnClickBtn5();}
}void MainWindow::SetPos(int nScreen)
{char cData[2048] = {0};QDesktopWidget* desktopWidget = QApplication::desktop();QRect rect = desktopWidget->availableGeometry(nScreen);QRect rect1 = desktopWidget->screenGeometry(nScreen);/*if(m_nScreenID == 0){*/setGeometry(rect);sprintf(cData, "AAA:%d   %d   %d   %d   %d\n", nScreen, rect.left(), rect.top(), rect.width(), rect.height());/*}else{setGeometry(QRect(rect1.left(), rect1.top(), rect1.width(), rect1.height()));sprintf(cData, "AAA:%d   %d   %d   %d   %d\n", nScreen, rect1.left(), rect1.top(), rect1.width(), rect1.height());}*/OutPut(cData);/*{int nScreenNum = qApp->desktop()->screenCount();for(int i = 0; i < nScreenNum; i++){QScreen* qScreen = qApp->screens().at(i);memset(cData, 0, 2048);sprintf(cData, "BBB:%d   %d   %d   %d   %d   %d\n", nScreenNum, nScreen, qScreen->geometry().left(), qScreen->geometry().top(), qScreen->geometry().width(), qScreen->geometry().height());OutPut(cData);}memset(cData, 0, 2048);sprintf(cData, "CCC:%d   %d   %d   %d   %d   %d\n", nScreenNum, nScreen, rect1.left(), rect1.top(), rect1.width(), rect1.height());OutPut(cData);}*/
}void MainWindow::OnClickBtn1()
{char cData[2048] = {0};FILE *fstream = popen("xrandr --output HDMI-2 --mode 1920x1080 --left-of HDMI-1", "r");//xrandr -s 1920x1080 --screen 0if(fstream){while(fgets(cData, sizeof(cData), fstream)){OutPut(cData);}pclose(fstream);}m_nScreenID = 0;m_nScreebResolutions = 0;m_nTimerID = startTimer(1000);
}void MainWindow::OnClickBtn2()
{char cData[2048] = {0};FILE *fstream = popen("xrandr --output HDMI-2 --mode 3840x2160 --left-of HDMI-1", "r");//xrandr -s 3840x2160 --screen 0if(fstream){while(fgets(cData, sizeof(cData), fstream)){OutPut(cData);}pclose(fstream);}m_nScreenID = 0;m_nScreebResolutions = 1;m_nTimerID = startTimer(1000);
}
void MainWindow::OnClickBtn3()
{char cData[2048] = {0};FILE *fstream = popen("xrandr --output HDMI-1 --mode 1920x1080 --right-of HDMI-2 ", "r");if(fstream){while(fgets(cData, sizeof(cData), fstream)){OutPut(cData);}pclose(fstream);}m_nScreenID = 1;m_nScreebResolutions = 0;m_nTimerID = startTimer(1000);
}void MainWindow::OnClickBtn4()
{char cData[2048] = {0};FILE *fstream = popen("xrandr --output HDMI-1 --mode 3840x2160 --right-of HDMI-2 ", "r");if(fstream){while(fgets(cData, sizeof(cData), fstream)){OutPut(cData);}pclose(fstream);}m_nScreenID = 1;m_nScreebResolutions = 1;m_nTimerID = startTimer(1000);
}void MainWindow::OnClickBtn5()
{char cData[2048] = {0};FILE *fstream = popen("xrandr ", "r");if(fstream){while(fgets(cData, sizeof(cData), fstream)){OutPut(cData);}pclose(fstream);}
}

左边的4k显示器和右边的4k显示器分别显示程序的主界面

UBuntu18.04 Qt之双HDMI接2个4K屏并分别设置分辨率、主屏、副屏相关推荐

  1. ubuntu18.04 Qt Creator 安装+ROS Qt Creator Plug-in 插件安装笔记

    ubuntu18.04 Qt Creator 安装+ROS Qt Creator Plug-in 插件安装笔记: 一.Qt Creator 5.10安装 官网下载地址: http://download ...

  2. Acer 笔记本双硬盘安装Ubuntu18.04.4+Win10双系统

    系统:原装Win10装在128G GPT分区表的固态硬盘            1T MBR分区表的东芝机械盘分出80GB安装ubuntu18.04.4 LTS 硬件:Acer笔记本Aspire E ...

  3. Ubuntu18.04 双屏显示 双显卡设置

    Ubuntu18.04 双屏显示 双显卡设置 对于习惯了windows下的双屏幕环境,安装好ubuntu后也想整个双屏.给电脑插上hdimi线后,发现系统没有识别到第二个屏幕.然后在设置display ...

  4. 神州战神Z7-KP7EC 安装Ubuntu18.04 和win10双系统操作流程

    神州战神Z7-KP7EC 安装Ubuntu18.04 和win10双系统操作流程 存在问题点 参考的相关有效文章 处理流程 存在问题点 1.神州战神Z7-KP7EC为双硬盘系统,1T机械硬盘+256G ...

  5. Ubuntu18.04+Qt+Opencv+opencv_contrib 解决putText中文乱码问题

    目录 问题描述: 目的: 配置环境的过程记录: 1 opencv的卸载 1.1 自带的opencv3.3.1卸载方式 1.2 源码安装的opencv卸载方式 2 opencv及opencv_contr ...

  6. Ubuntu18.04与Win10 双系统安装分区详解

    在wWn10安装Ubuntu18.04的时候,遇到的一个不太敢操作的就是分区,因为是在win10下安装双系统,担心把文件误删除了,如果方便可以先把所有文件备份到移动硬盘上. 完整步骤可参考以下链接: ...

  7. Ubuntu18.04和win10双系统完美安装(dell笔记本电脑)

    里主要记录我的安装过程,仅供大家参考,我的安装不一定适合您的电脑,只是提供参考. 我电脑的参数:戴尔笔记本电脑precision 3541. 这几年算起来,我装了十多次ubuntu系统,应该比较熟悉一 ...

  8. 安装Ubuntu18.04与win10双系统,U盘安装,亲测可用

    让我们开始吧 知识储备 准备工作 制作启动U盘 给Ubuntu预先分配磁盘空间 安装Ubuntu 我是杰出的小茄子 知识储备 1.Secure Boot:安全启动,只可以启动Win8及以上系统,不能启 ...

  9. UEFI启动+GPT分区模式下双硬盘SSD+HDD+Win10安装Ubuntu18.04.1组成双系统

    以下叙述均基于UEFI+GPT模式 1 环境: 处理器.内存.系统: 硬盘:256G三星固态盘+2T希捷SATA机械盘 Win10已经安装在固态 2 安装流程 2.1 准备空闲分区 磁盘管理中选择压缩 ...

最新文章

  1. [转载]WorldWind实时确定、更新、初始化和渲染地形和纹理数据
  2. linux(八)__yum工具
  3. 上周热点回顾(6.17-6.23)
  4. python color属性_Python中类的属性、方法及内置方法
  5. 登顶AI服务器全球第一后,浪潮发布AI系统浪潮元脑
  6. Android 四大组件学习之ContentProvider四
  7. QtCreator无法启动进程“mingw32-make.exe
  8. 百度网盘设置自定义提取码
  9. 儿童计算机编程竞赛,一年获全国少儿编程比赛一等奖,他是怎么做到的?
  10. Vscode tab键使用
  11. 计算机常用工具软件实训总结报告,计算机常用工具软件实训报告
  12. Java Io中涉及到的类和涉及模式
  13. dot格式绘图工具 html,使用dot来绘图
  14. 迅雷9屏蔽所有游览器和网站相应
  15. Physically Based Rendering——史上最容易理解的BRDF中D函数NDF的中文资料
  16. 08运行程序常见错误
  17. 【我的渲染技术进阶之旅】基于Filament渲染引擎绘制一个不停旋转的彩色矩形
  18. ACPI Spec Chapter 10 Power Source And Power Meter Devices
  19. MySQL基础之多表查询
  20. 如何利用fooview实现钉钉自动打卡

热门文章

  1. 数美科技的智能文本审核能为社交行业带来什么价值 | 数美人工智能研究院
  2. ffmpeg 实现音频aac编码
  3. uniapp上下滑屏切换支持视频和图片轮播实现,类似抖音效果
  4. 服装行业如何用手持PDA盘点?
  5. 9位院士!10所一流大学,迎“新帅”
  6. bzoj 4372 烁烁的游戏——动态点分治+树状数组
  7. 大数据智能算法及测评技术(二)
  8. C++计算机软件系统
  9. LFS(linux for stratch)关于急救盘的制作问题
  10. postgresql 备份 还原