Results 1 to 3 of 3

Thread: Dynamically change spacing between icon and label in QPushButton

  1. #1
    Join Date
    Aug 2013
    Posts
    2
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Windows

    Default Dynamically change spacing between icon and label in QPushButton

    I am frustratingly trying to dynamically change the spacing between the icon and the label inside a QPushButton, for different screen sizes. Looking through the source code in qcommonstyle.cpp, this spacing is hardcoded as 4.
    Qt Code:
    1. if (!button->icon.isNull()) {
    2. ...
    3. QPixmap pixmap = button->icon.pixmap(button->iconSize, mode, state);
    4. int labelWidth = pixmap.width();
    5. int labelHeight = pixmap.height();
    6. int iconSpacing = 4; //### 4 is currently hardcoded in QPushButton::sizeHint()
    7. int textWidth = button->fontMetrics.boundingRect(opt->rect, tf, button->text).width();
    8. if (!button->text.isEmpty())
    9. labelWidth += (textWidth + iconSpacing);
    10. ...
    11. }
    To copy to clipboard, switch view to plain text mode 
    Changing it to what I want seems trivial, if I could recompile QT. But there should be a solution using default/precompiled libraries. I tried to create a QProxyStyle, which would basically reimplement the CE_PushButtonLabel case of its drawControl method. But then it wasn't properly drawn, changing the previously css-setted border and the background-color of the pressed state. I applied the following QProxyStyle using both QApplication::setStyle() and directly instantiating the style in the paintEvent of my button, neither changed only the spacing.
    Qt Code:
    1. class ProperlyAlignedPushButtonStyle : public QProxyStyle
    2. {
    3. public:
    4. ProperlyAlignedPushButtonStyle() : QProxyStyle(QStyleFactory::create("windows")) {}
    5.  
    6. void drawControl(ControlElement element, const QStyleOption *opt, QPainter *p, const QWidget *widget) const
    7. {
    8. if (element == CE_PushButtonLabel) {
    9. ...
    10. int iconSpacing = (textRect.width() - (labelWidth + textWidth) ) / 3;
    11. ...
    12. else {
    13. QProxyStyle::drawControl(element, opt, p, widget);
    14. }
    15. }
    16. };
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. void OrientationButton::paintEvent(QPaintEvent* event) {
    2. ProperlyAlignedPushButtonStyle pa;
    3. pa.drawControl(QStyle::CE_PushButton, &getStyleOption(), &p, this);
    4. }
    To copy to clipboard, switch view to plain text mode 
    Any ideas? Thanks in advance!

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Dynamically change spacing between icon and label in QPushButton

    In general trying to mix stylesheets with real styles is a bad idea. Decide to use one and stick with this decision.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. The following user says thank you to wysota for this useful post:

    bernardoreis (5th September 2013)

  4. #3
    Join Date
    Aug 2013
    Posts
    2
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Dynamically change spacing between icon and label in QPushButton

    You're probably right. It doesn't look like that I can easily retrieve stylesheets' infomation and use it to paint a widget myself.

    Thanks for the advice.

Similar Threads

  1. Replies: 3
    Last Post: 19th June 2013, 15:41
  2. spacing between icon and text
    By mrchumley in forum Newbie
    Replies: 2
    Last Post: 21st May 2012, 12:19
  3. How to resize the QIcon inside QPushButton dynamically?
    By gboelter in forum Qt Programming
    Replies: 2
    Last Post: 18th February 2010, 13:34
  4. changing QPushButton label
    By Ferric in forum Newbie
    Replies: 3
    Last Post: 31st January 2010, 10:57
  5. Dynamically resize spacing
    By trskel in forum Qt Programming
    Replies: 6
    Last Post: 28th September 2007, 12:52

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.