效果:按钮弹出分屏选择
#ifndef GRIDPOPWIDGET_H
#define GRIDPOPWIDGET_H#include <QWidget>
#include <QMouseEvent>class GridPopWidget : public QWidget
{Q_OBJECT
public:explicit GridPopWidget(QWidget *parent = nullptr);~GridPopWidget();protected:virtual void mouseMoveEvent(QMouseEvent *event) override;virtual void mousePressEvent(QMouseEvent *event) override;
signals:void sig_layout(int, int);private:QList<QList<QWidget *> > wArr;
};#endif
#include "gridpopwidget.h"
#include <QGridLayout>#define BACKGROUND "background-color:black; padding:0px;margin:0px;border:1px solid gray;"
#define HIGHLIGHT "background-color:white"
#define CSSNORMAL "background-color:darkgray;"
#define GRIDROW 4
#define GRIDCOL 5
#define GRIDSIZE 25
#define GRIDSPACE 0
GridPopWidget::GridPopWidget(QWidget *parent) :QWidget(parent)
{setMouseTracking(true);setStyleSheet(QString("QWidget{%1}").arg(BACKGROUND));QGridLayout *glayout = new QGridLayout(this);for(int i=0; i<GRIDROW; ++i){QList<QWidget*> ws;for (int j=0; j<GRIDCOL; ++j){QWidget* w = new QWidget(this);w->setMouseTracking(true);w->setStyleSheet(CSSNORMAL);glayout->addWidget(w,i,j,Qt::AlignCenter);w->setFixedSize(GRIDSIZE,GRIDSIZE);ws.append(w);}wArr.append(ws);}glayout->setSpacing(GRIDSPACE);setLayout(glayout);resize(GRIDSIZE*GRIDCOL,GRIDSIZE*GRIDROW);hide();
}GridPopWidget::~GridPopWidget()
{
}void GridPopWidget::mouseMoveEvent(QMouseEvent *e)
{QWidget::mouseMoveEvent(e);QPoint p = e->pos();foreach (QList<QWidget *> ws, wArr) {foreach (QWidget *w, ws) {if( w->pos().x() < p.x() && w->pos().y() < p.y() )w->setStyleSheet(HIGHLIGHT);elsew->setStyleSheet(CSSNORMAL);}}
}void GridPopWidget::mousePressEvent(QMouseEvent *e)
{QWidget::mousePressEvent(e);int row = 0, col = 0;QPoint p = e->pos();QPoint lim = this->rect().bottomRight();if (p.x() > lim.x() || p.y() > lim.y()) return;foreach (QList<QWidget *> ws, wArr) {if (p.y() > ws.first()->pos().y()) row++;}foreach (QWidget *w, wArr[0]) {if (p.x() > w->pos().x()) col++;}emit sig_layout(row,col);hide();
}
#include <gridpopwidget.h>class MainWindow : public QMainWindow
{Q_OBJECT
public:
protected:virtual void mousePressEvent(QMouseEvent *event) override;virtual void resizeEvent(QResizeEvent *event) override;
public slots:void setSplitLayout(int row,int col);void setGridPopWidget();
private:GridPopWidget *gridwidget;
};
#include <QToolButton>
#include <QMenu>
#include <QComboBox>
MainWindow::MainWindow(QWidget *parent)
{gridwidget = new GridPopWidget(this); ui->statusBar->addPermanentWidget(ui->toolButton);connect(gridwidget,&GridPopWidget::sig_layout,this,&MainWindow::setSplitLayout);connect(ui->toolButton,&QToolButton::clicked,this,&MainWindow::setGridPopWidget);
}void MainWindow::resizeEvent(QResizeEvent *e)
{if(gridwidget->isVisible())gridwidget->hide();
}void MainWindow::mousePressEvent(QMouseEvent *e)
{if( e->pos().x() < gridwidget->pos().x() || e->pos().y() < gridwidget->pos().y() ){gridwidget->hide();}
}void MainWindow::setSplitLayout(int row, int col)
{ qDebug() << "row:" << row << ", col:" << col;
}void MainWindow::setGridPopWidget()
{gridwidget->move(width()-gridwidget->width(),height()-gridwidget->height()-ui->statusBar->height()+5);gridwidget->show();
}