QT项目开发中,有可能会出现同一界面多个屏幕展示,对比的情况,尤其是在视频监控行业,这中情况非常多,对于如何实现QMainWindow分屏,这里做下说明,这里主要是以多个label为例子,实现右键菜单弹出来切换不同的屏幕布局,可以按照规则实现你想要的屏幕布局,具体代码如下:

global.h代码:

#ifndef GLOBAL_H
#define GLOBAL_H

enum VideoLayoutType
{
    OneVideo = 0,
    VTwoVideo,
    HTwoVideo,
    ThreeVideo,
    FourVideo,
    FiveVideo,
    SixVideo,
    SevenVideo,
    EightVideo,
    NineVideo,
};

#endif // GLOBAL_H

mainwindow.h代码如下:

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include "global.h"
#include <QList>
#include <QLabel>
#include <QMouseEvent>
#include <QMenu>
#include <QAction>
#include <QWidget>

QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    MainWindow(QWidget *parent = nullptr);
    ~MainWindow();

protected:
    void mousePressEvent(QMouseEvent* event);
private slots:
    void switchLayout(VideoLayoutType type);
    void oneSlot();
    void twoSlot();
    void htwoSlot();
    void threeSlot();
    void fourSlot();
    void fiveSlot();
    void sixSlot();
    void nineSlot();

private:
    Ui::MainWindow *ui;
    QList<QLabel*> m_videoLabelList;
    QMenu* m_switchMenu;
    QWidget *Q_widget;
};
#endif // MAINWINDOW_H

mainwindow.cpp代码如下:

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QLayoutItem>
#include <QLayout>
#include <QMap>
#include <QDebug>
#include <QGridLayout>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    // 初始化9个label空间
    for (int i = 0;i < 9;i++)
    {
        QLabel* label = new QLabel;
        label->setStyleSheet(QString("QLabel{background-image:url(:/resources/%1.png); \
                                     border:1px solid gray; \
                                     background-position:center; \
                                     background-repeat:no-repeat; \
                                     }").arg(QString::number(i+1)));
        m_videoLabelList.append(label);
    }

    setCentralWidget(ui->myWidget);
//    QGridLayout* gLayout = new QGridLayout(this);
//    gLayout->addWidget(Q_widget);
//    setLayout(gLayout);

}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::switchLayout(VideoLayoutType type)
{
    QLayout* layout = ui->myWidget->layout();

    // 清除布局内所有元素
    if (layout)
    {
        QLayoutItem* child;
        while((child = layout->takeAt(0)) != 0)
        {
            if (child->widget())
            {
                child->widget()->setParent(NULL);
            }
            delete child;
        }
        delete layout;
    }

    switch(type)
    {
    case OneVideo:
    {
        qDebug() << "OneVideo";
        QGridLayout* gLayout = new QGridLayout(ui->myWidget);
        gLayout->addWidget(m_videoLabelList[0]);
        gLayout->setMargin(0);
        ui->myWidget->setLayout(gLayout);
    }
    break;
    case VTwoVideo:
    {
        QVBoxLayout* vLayout = new QVBoxLayout(ui->myWidget);
        vLayout->addWidget(m_videoLabelList[0]);
        vLayout->addWidget(m_videoLabelList[1]);
        vLayout->setMargin(0);
        vLayout->setSpacing(0);
        ui->myWidget->setLayout(vLayout);
    }
    break;
    case HTwoVideo:
    {
        QHBoxLayout* hLayout = new QHBoxLayout(ui->myWidget);
        hLayout->addWidget(m_videoLabelList[0]);
        hLayout->addWidget(m_videoLabelList[1]);
        hLayout->setMargin(0);
        hLayout->setSpacing(0);
        ui->myWidget->setLayout(hLayout);
    }
    break;
    case ThreeVideo:
    {
        QGridLayout* gLayout = new QGridLayout(ui->myWidget);
        gLayout->addWidget(m_videoLabelList[0],0,0);
        gLayout->addWidget(m_videoLabelList[1],0,1);
        gLayout->addWidget(m_videoLabelList[2],0,2);
        ui->myWidget->setLayout(gLayout);
    }
    break;
    case FourVideo:
    {
        qDebug() << "FourVideo";
        QGridLayout* gLayout = new QGridLayout(ui->myWidget);
        gLayout->setSpacing(0);
        gLayout->setMargin(0);

        for (int i = 0;i < 4;i++)
        {
            gLayout->addWidget(m_videoLabelList[i],i/2,i%2);
        }
        ui->myWidget->setLayout(gLayout);
    }
    break;
    case FiveVideo:
    {
        QVBoxLayout* pVlay = new QVBoxLayout(ui->myWidget);
        pVlay->setSpacing(0);
        QHBoxLayout* pHTopLay = new QHBoxLayout(ui->myWidget);
        pHTopLay->setSpacing(0);
        for (int i = 0;i < 3;i++)
        {
            pHTopLay->addWidget(m_videoLabelList[i]);
        }

        QHBoxLayout* pHBottomLay = new QHBoxLayout(ui->myWidget);
        pHBottomLay->setSpacing(0);
        for (int i = 3;i < 5;i++)
        {
            pHBottomLay->addWidget(m_videoLabelList[i]);
        }
        pVlay->addLayout(pHTopLay);
        pVlay->addLayout(pHBottomLay);
        ui->myWidget->setLayout(pVlay);
    }
    break;
    case SixVideo:
    {
        QGridLayout* gLayout = new QGridLayout(ui->myWidget);
        gLayout->addWidget(m_videoLabelList[0],0,0,2,2);
        gLayout->addWidget(m_videoLabelList[1],0,2);
        gLayout->addWidget(m_videoLabelList[2],1,2);
        gLayout->addWidget(m_videoLabelList[3],2,0);
        gLayout->addWidget(m_videoLabelList[4],2,1);
        gLayout->addWidget(m_videoLabelList[5],2,2);
        gLayout->setSpacing(0);
        gLayout->setMargin(0);
        ui->myWidget->setLayout(gLayout);
    }
    break;
    case NineVideo:
    {
        QGridLayout* gLayout = new QGridLayout(ui->myWidget);
        gLayout->setSpacing(0);
        gLayout->setMargin(0);
        for (int i = 0;i < 9;i++)
        {
            gLayout->addWidget(m_videoLabelList[i],i/3,i%3);
        }
        ui->myWidget->setLayout(gLayout);
    }
    break;
    default:
        break;
    }
}

void MainWindow::mousePressEvent(QMouseEvent* event)
{
    if (event->button() == Qt::RightButton)
    {
        // 创建右键菜单
        m_switchMenu = new QMenu(this);
        m_switchMenu->setContextMenuPolicy(Qt::CustomContextMenu);
        QAction *pOne = new QAction(QString(u8"一分屏"),this);
        QAction *pVTwo = new QAction(QString(u8"垂直二分屏"),this);
        QAction *pHTwo = new QAction(QString(u8"水平二分屏"),this);
        QAction *pThree = new QAction(QString(u8"三分屏"),this);
        QAction *pFour = new QAction(QString(u8"四分屏"),this);
        QAction *pFive = new QAction(QString(u8"五分屏"),this);
        QAction *pSix = new QAction(QString(u8"六分屏"),this);
        QAction *pNine = new QAction(QString(u8"九分屏"),this);
        m_switchMenu->addAction(pOne);
        m_switchMenu->addAction(pVTwo);
        m_switchMenu->addAction(pHTwo);
        m_switchMenu->addAction(pThree);
        m_switchMenu->addAction(pFour);
        m_switchMenu->addAction(pFive);
        m_switchMenu->addAction(pSix);
        m_switchMenu->addAction(pNine);

        connect(pOne,SIGNAL(triggered()),this,SLOT(oneSlot()));
        connect(pVTwo,SIGNAL(triggered()),this,SLOT(twoSlot()));
        connect(pHTwo,SIGNAL(triggered()),this,SLOT(htwoSlot()));
        connect(pThree,SIGNAL(triggered()),this,SLOT(threeSlot()));
        connect(pFour,SIGNAL(triggered()),this,SLOT(fourSlot()));
        connect(pFive,SIGNAL(triggered()),this,SLOT(fiveSlot()));
        connect(pSix,SIGNAL(triggered()),this,SLOT(sixSlot()));
        connect(pNine,SIGNAL(triggered()),this,SLOT(nineSlot()));

        m_switchMenu->exec(QCursor::pos());

        delete pOne;
        delete pVTwo;
        delete pHTwo;
        delete pThree;
        delete pFour;
        delete pFive;
        delete pSix;
        delete pNine;
        delete m_switchMenu;
    }
}

void MainWindow::oneSlot()
{
    switchLayout(VideoLayoutType::OneVideo);
}

void MainWindow::twoSlot()
{
    switchLayout(VideoLayoutType::VTwoVideo);
}

void MainWindow::fourSlot()
{
    qDebug() << "four";
    switchLayout(VideoLayoutType::FourVideo);

}
void MainWindow::fiveSlot()
{
    switchLayout(VideoLayoutType::FiveVideo);
}
void MainWindow::sixSlot()
{
    switchLayout(VideoLayoutType::SixVideo);
}
void MainWindow::nineSlot()
{
    switchLayout(VideoLayoutType::NineVideo);
}
void MainWindow::htwoSlot()
{
    switchLayout(VideoLayoutType::HTwoVideo);
}
void MainWindow::threeSlot()
{
    switchLayout(VideoLayoutType::ThreeVideo);
}

运行效果:

 

总结:可以在源码的基础上开发自己想要的界面, 希望能帮到你。

Logo

有“AI”的1024 = 2048,欢迎大家加入2048 AI社区

更多推荐