当焦点出现在QToolButton上时,如何调整QToolButton的大小

时间:2014-01-15 11:18:29

标签: c++ qt

如何在QToolButton焦点发生时调整QToolButton.的大小 我有5 QToolButton,当焦点在第二QToolButton时,它的大小会自动增加。怎么这样呢?

2 个答案:

答案 0 :(得分:2)

你必须创建一个自定义类,继承QToolButton。

class MyButton : public QToolButton
{
    Q_OBJECT 

    private:
         int originalWidth, originalHeight;
         int bigWidth, bigHeight;
};

然后重新实现focusInEvent并退出。

void focusInEvent ( QFocusEvent * event ) { 
                   resize(bigWidth,bigHeight); 
                   QToolButton::focusInEvent(event); // Don't forget to call parent focus in / out in order to make the "hover" effect work. 
}

void focusOutEvent ( QFocusEvent * event ) { 
                   resize(originalWidth,originalHeight); 
                   QToolButton::focusOutEvent(event);
}

干杯。

答案 1 :(得分:2)

也可以通过QSS:

#MySecondButton:focus
{
  width: 300px;
  height: 200px;
}

取决于布局和尺寸政策,可能需要设置“max-width”/“max-height”/“min-width”等属性。

相关问题