QPushButton icon auto resize
-
Hi everyone,
I'm using QT from few months and I encounter a little problem when using QPushButton.
I create my GUI from the code , I'm not using QTCreator wysiwyg or QTQuick.For example:
QGridLayout* pLayout = new QGridLayout ( this ); QPushButton pZRegistration = new QPushButton ( ); pZRegistration ->setFlat ( true ); pZRegistration ->setToolTip ( QString ( "Recalage en Z" ) ); pZRegistration ->setSizePolicy( QSizePolicy::Expanding, QSizePolicy::Expanding ); pZRegistration ->setIcon ( QIcon ( ":/Images/Volume.png" ) ); connect ( pZRegistration , SIGNAL( clicked( ) ) , this , SLOT ( registrationAlongZ( ) ) ); pLayout->addWidget ( pZRegistration , 0, 0 ); QPushButton pTRegistration = new QPushButton ( ); pTRegistration ->setFlat ( true ); pTRegistration ->setToolTip ( QString ( "Recalage en T" ) ); pTRegistration ->setSizePolicy( QSizePolicy::Expanding, QSizePolicy::Expanding ); pTRegistration ->setIcon ( QIcon ( ":/Images/Time.png" ) ); connect ( pTRegistration , SIGNAL( clicked( ) ) , this , SLOT ( registrationAlongT( ) ) ); pLayout->addWidget ( pTRegistration , 0, 1 ); setLayout ( pLayout );
When I use this exemple, the two QPushButton propagate themselves based on layout. That's perfect!
The real problem is that my icon keep to be small inside my QPushButton.
I tried the solution at this topic: https://forum.qt.io/topic/7978/icon-in-pushbutton-does-not-resize/5
Catching the resize event of my pushbuttons and resizing icon.When I used this solution I got another problem, my QGridLayout constraints are not respected : PushButtons are growing until others components are at them minimum size ( rowStretch/columsStrech are not respected and other trouble using subWidgets...).
Are there a good (perfect?) way to do this properly?
Thanks a lot! -
After all this time, I don't fin a way to do it properly.
I'm using QML and it's fine now. For people intereested in, this is my QML component:
import QtQuick 2.5
import QtQuick.Controls 1.4
import QtQuick.Controls.Styles 1.4Button
{
property color backgroundColor
property alias tooltip : buttonAction.tooltip
property string imageSource
signal buttonClicked ( )action : buttonAction activeFocusOnPress : true Keys.onPressed : { if ( (event.key == Qt.Key_Return||event.key == Qt.Key_Enter) && activeFocus) { buttonClicked ( ); event.accepted = true; } } style : ButtonStyle { background: Rectangle { id : backgroundRectangle color : backgroundColor Rectangle { id : roundedRectangle color : control.pressed?Qt.darker(backgroundColor, 1.1):backgroundColor anchors.fill : parent radius : 10 scale : ( control.hovered||control.activeFocus )?1:0.9 Image { id : buttonImage anchors.fill: parent fillMode : Image.PreserveAspectFit source : imageSource } } } } Action { id : buttonAction onTriggered : buttonClicked () }
}