qt在qgridlayout中设置相同大小的小部件

qt set widgets in qgridlayout with same size

我有以下布局:

当我拉伸 window 时,大小变成这样:

有没有办法在 qgridlayout 大小改变时将 qgridlayout 中的小部件设置为相同大小?

谢谢。

更新

qgridlayout 中的小部件:

#ifndef CVIMAGEWIDGET_H
#define CVIMAGEWIDGET_H

#include <QWidget>
#include <QImage>
#include <QPainter>
#include <QGraphicsView>
#include <QGraphicsScene>
#include <QMainWindow>

class CVImageWidget : public QGraphicsView
{
    Q_OBJECT

public:
    explicit CVImageWidget(QMainWindow *GUIWindow, QWidget *parent = 0);
    void drawBoarder(bool flag);
    void showImage(const QImage& image);

    void SetCameraID(int camID);
    int GetCameraID();
    void closeDisplay();
    ~CVImageWidget();

private:
    QGraphicsScene *scene_;
    int camera_id_;

public slots:

    void resizeEvent(QResizeEvent *event);

};



#endif // CVIMAGEWIDGET_H

CPP 文件

#include "cvimagewidget.h"
#include <QMouseEvent>
#include <iostream>

CVImageWidget::CVImageWidget(QMainWindow *GUIWindow, QWidget *parent) : QGraphicsView(parent)
{
    scene_ = new QGraphicsScene(parent);
    scene_->setSceneRect(this->sceneRect());
    this->setScene(scene_);
    this->installEventFilter(GUIWindow);
    this->setStyleSheet( "CVImageWidget { border-color: rgb(125,125,116); border-width: 3px; border-style: solid;}");
//    this->setMinimumSize(136, 114);
    camera_id_ = -1;
}


void CVImageWidget::showImage(const QImage& image) {
    if(!image.isNull()){
        scene_->clear();
        scene_->addPixmap(QPixmap::fromImage(image));
    }
}

void CVImageWidget::closeDisplay()
{
    scene_->clear();
}

void CVImageWidget::SetCameraID(int camID)
{
    camera_id_ = camID;
}

int CVImageWidget::GetCameraID()
{
    return camera_id_;
}

void CVImageWidget::resizeEvent(QResizeEvent *event)
{
    this->fitInView(this->sceneRect(), Qt::IgnoreAspectRatio);
    QGraphicsView::resizeEvent(event);
}

void CVImageWidget::drawBoarder(bool flag)
{
    if(flag)
    {
        this->setStyleSheet( "CVImageWidget { border-color: rgb(0,255,0); border-width: 3px; border-style: solid;}");
    }else
    {
        this->setStyleSheet( "CVImageWidget { border-color: rgb(125,125,116); border-width: 3px; border-style: solid;}");
    }
}


CVImageWidget::~CVImageWidget()
{
    delete scene_;
}

将每个 row/column

的列拉伸和行拉伸设置为 1

在布局中添加项目时,请确保为所有行和列添加拉伸因子。

   int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);

        QWidget widget;
        QGridLayout layout;
        for( int i =0 ; i < 4; i++ )
        {
            layout.setRowStretch( i, 1);
            for ( int j = 0; j < 4; j++)
            {
                if( i == 0 )
                {
                    layout.setColumnStretch( j, 1 );
                }
                CVImageWidget * temp = new CVImageWidget( &widget );
                layout.addWidget( temp, i, j, 1, 1 );
                if( i ==0 && j== 0 )
                {
                    temp->showImage( QImage("/Users/sacp/Desktop/check.png") );
                }
            }
        }
        widget.setLayout( &layout );
        widget.show();
        a.exec();
        return 0;
    }

这是调整大小后的样子

如果您想同时为所有元素设置 ColumnStretchRowStretch,这也适用:

QGridLayout layout;

// Fill layout ...

for(int c=0; c < layout.columnCount(); c++) layout.setColumnStretch(c,1);
for(int r=0; r < layout.rowCount(); r++)  layout.setRowStretch(r,1);