UBuntu18.04 Qt之双HDMI屏切换

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

一、设置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_OBJECT
 
public:
    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 0
    if(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 0
    if(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显示器分别显示程序的主界面

相关推荐
Qter_Sean17 分钟前
自己动手写Qt Creator插件
开发语言·qt
何曾参静谧21 分钟前
「QT」文件类 之 QIODevice 输入输出设备类
开发语言·qt
爱吃生蚝的于勒1 小时前
C语言内存函数
c语言·开发语言·数据结构·c++·学习·算法
小白学大数据3 小时前
Python爬虫开发中的分析与方案制定
开发语言·c++·爬虫·python
冰芒猓4 小时前
SpringMVC数据校验、数据格式化处理、国际化设置
开发语言·maven
失落的香蕉4 小时前
C语言串讲-2之指针和结构体
java·c语言·开发语言
红中马喽4 小时前
JS学习日记(webAPI—DOM)
开发语言·前端·javascript·笔记·vscode·学习
杜杜的man5 小时前
【go从零单排】Closing Channels通道关闭、Range over Channels
开发语言·后端·golang
java小吕布5 小时前
Java中Properties的使用详解
java·开发语言·后端
versatile_zpc6 小时前
C++初阶:类和对象(上)
开发语言·c++