Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QPushButton icon auto resize
Qt 6.11 is out! See what's new in the release blog

QPushButton icon auto resize

Scheduled Pinned Locked Moved General and Desktop
qpushbuttoniconautomaticresize
2 Posts 1 Posters 4.2k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Romain CR Offline
    Romain CR Offline
    Romain C
    wrote on last edited by
    #1

    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!

    1 Reply Last reply
    0
    • Romain CR Offline
      Romain CR Offline
      Romain C
      wrote on last edited by
      #2

      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.4

      Button
      {
      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 ()
      }
      

      }

      1 Reply Last reply
      0

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved