1. 程式人生 > >qt多個視窗排列

qt多個視窗排列

qq:609162385
https://blog.csdn.net/cqltbe131421
在這裡插入圖片描述

#ifndef QSPLITTERWIDGET_H
#define QSPLITTERWIDGET_H

#include <QWidget>
#include <QList>
#include <QWidget>

class QSplitterWidget : public QWidget
{
    Q_OBJECT
public:
    explicit QSplitterWidget(QWidget *parent = 0);

protected:
    void arrangeWidget();
signals:

public:
    void addWidget(QWidget* w,int index);
    void addWidgetFirst(QWidget* w);
    void addWidgetEnd(QWidget* w);
    void reduceWidgetFirst();
    void reduceWidgetEnd();
    void reduceWidget(QString objectName);
private:
    QList<QWidget*> listw;
    int space;
};

#endif // QSPLITTERWIDGET_H

#include "qsplitterwidget.h"
#include "formwidget.h"
#include <QDebug>

QSplitterWidget::QSplitterWidget(QWidget *parent) : QWidget(parent)
{
    space = 5;
}

void QSplitterWidget::arrangeWidget()
{
    int fixedSizeW = 0;
    int fixedSizeH = 0;
    int hPoint = 0;
    int wPoint = 0;
    int recordHnum  = 0;
    int recordWnum  = 0;

    for(int i = 1; i <= 30; i++){
        if(listw.size () == 1 ){
            listw.at (0)->setGeometry (space,space,this->width () - space*2,this->height () - space*2);
            listw.at (0)->show ();
            break;
        }else if(listw.size () > i*i && listw.size ()  <= (i+1)*(i+1)){
            fixedSizeW = (this->width () - space*(i+2)) / (i+1);
            fixedSizeH = (this->height () - space*(i+2)) / (i+1);
            hPoint = space;
            for(int j = 0; j < listw.size (); j++){
                hPoint = (fixedSizeH + space)*recordHnum + space;
                wPoint = (fixedSizeW + space)*recordWnum + space;
                listw.at (j)->setGeometry (wPoint,hPoint,fixedSizeW,fixedSizeH);
                listw.at (j)->show ();
                recordWnum++;
                if(recordWnum >= i+1){
                    recordWnum = 0;
                    recordHnum++;
                }
            }
            break;
        }
    }
}

void QSplitterWidget::addWidget(QWidget *w, int index)
{
    w->setParent (this);
    listw.insert (index,w);
    arrangeWidget();
}

void QSplitterWidget::addWidgetFirst(QWidget *w)
{
    w->setParent (this);
    listw.insert (0,w);
    arrangeWidget();
}

void QSplitterWidget::addWidgetEnd(QWidget *w)
{
    w->setParent (this);
    listw.append (w);
    arrangeWidget();
}

void QSplitterWidget::reduceWidgetFirst()
{
    if(listw.size () >= 1){
        QWidget *w =  listw.at (0);
        listw.removeAt (0);
        w->close ();
        arrangeWidget();
    }
}

void QSplitterWidget::reduceWidgetEnd()
{
    if(listw.size () >= 1){
        QWidget *w =  listw.at (listw.size ()-1);
        listw.removeAt (listw.size ()-1);
        w->close ();
        arrangeWidget();
    }
}

void QSplitterWidget::reduceWidget(QString objectName)
{
    for(int i = 0; i < listw.size (); i++){
        if(listw.at (i)->objectName () == objectName){
            QWidget *w =  listw.at (i);
            listw.removeAt (i);
            w->close ();
            arrangeWidget();
        }
    }
}