2015-07-10 63 views
0

我創建了左邊的QDockWidget,並在其中添加了2 QLabel。 我想要標籤的平方尺寸(默認爲100x100)。Qt:QDockWidget中QLabel的大小

因此,當用戶增加QDockWidget的寬度時,我應該如何強制我的標籤保持平方尺寸?

我嘗試了很多事情,沒有任何成功:(

回答

0

我認爲你應該重寫int QWidget::heightForWidth(int aWidth) const功能。所以,你需要創建自己的QLabel子類。

class MyLabel 
{ 
public: 
    virtual int heightForWidth(int aWidth) const override 
    { 
     return aWidth; // So it will be square. 
    } 
}; 
+0

這是不夠的。 – hao

0

我想我有。該解決方案

我創建這個子類:

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

我所有的標籤都在QVBoxLayout加入,我需要這個屬性:

layout->setAlignment(Qt::AlignTop); 

現在,我需要調整我的像素圖在我的標籤。 我會在關閉此問題之前發佈它。

+0

http://stackoverflow.com/a/22618496/999943 – phyatt