0

I created a left QDockWidget, and added 2 QLabel into it. I want a squared size for my labels (by default 100x100).

So when the user increases the width of the QDockWidget, how should I force my labels to keep a squared size?

I tried a lot of things, without any success :(

Kailas
  • 7,350
  • 3
  • 47
  • 63
hao
  • 228
  • 2
  • 9

2 Answers2

0

I think you shall overwrite the int QWidget::heightForWidth( int aWidth ) const function. So you need to create your own QLabel subclass.

class MyLabel
{
public:
    virtual int heightForWidth( int aWidth ) const override
    {
        return aWidth; // So it will be square.
    }
};
p.i.g.
  • 2,815
  • 2
  • 24
  • 41
0

I think I have the solution.

I create this subclass :

class SquareLabel : public QLabel
{
public:
    SquareLabel(QWidget* parent = 0) : QLabel(parent)
    {
        setMinimumSize(100, 100);

        QSizePolicy pol(QSizePolicy::Preferred, QSizePolicy::Preferred);
        pol.setHeightForWidth(true);
        setSizePolicy(pol);
    }
    virtual int heightForWidth(int w) const { return w; }
    virtual QSize sizeHint() const { int w = this->width(); return QSize(w, heightForWidth(w)); }

protected:
    void resizeEvent(QResizeEvent* event) { }
};

All my labels are added in a QVBoxLayout, and I need this property :

layout->setAlignment(Qt::AlignTop);

Now, I need to resize my pixmap in my label. I will post it before close this question.

hao
  • 228
  • 2
  • 9