3

I have following layouts:

enter image description here

when I stretch the window, the size becomes like this:

enter image description here

Is there a way to set the widgets in qgridlayout with the same size when the qgridlayout size changes ?

Thanks.

UPDATE

The widget in 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 file

#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_;
}
Johnnylin
  • 507
  • 2
  • 7
  • 26

3 Answers3

5

When you add the items in layout, make sure you add stretch factor for all the rows and columns.

   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;
    }

This is how it looks after resize enter image description here

sanjay
  • 735
  • 1
  • 5
  • 23
  • In my QMainWindow Class, I have an eventFilter which enables user to double click the selected CVImageWidget. The double clicked CVImageWidget will expand to full QGridLayout size. Other CVImageWidgets will be hidden? is there a way to do that ? If I set the column and row stretch, How can I achieve the expansion effect ? Thanks – Johnnylin Oct 20 '16 at 12:46
0

Set The column stretch and row stretch to 1 for each row/column

Lifeisabug
  • 366
  • 2
  • 6
  • 1
    As you can see in the first screenshot, that all the widgets are using the same size. It only happens in resize. So it's the widget inside which is creating this problem. – sanjay Oct 20 '16 at 11:47
0

In case you want to set the ColumnStretch and RowStretch at once for all elements, this works as well:

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);
Turtle10000
  • 213
  • 1
  • 13