UBuntu18.04 Qt之双HDMI屏切换

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

一、设置HDMI-2为主屏

在main函数里面添加:

#include "mainwindow.h"
#include 
 
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 
 
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 
#include 
#include 
#include 
#include 
 
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显示器分别显示程序的主界面

你可能感兴趣的:(qt,开发语言)