Prevent flat QToolButton from moving when clicked



  • So I'm trying to implement basically the same Settings dialog as in latest versions of Firefox. At the top I've placed a white frame with several QToolButtons placed over it, and styled them with CSS.

    It all looks great but when I click on the button (which is flat btw), it moves a pixel to the right and a pixel to the bottom. Not a big deal but still irritating, so I was wondering if there is any way to compensate for this?

    Thanks

    EDIT: It's not a flat button, it's just a normal QToolButton with "checkable=True"



  • If a checkable tool buttion is checked, it is visually "pushed in", that's the cause of the move. Try without the style sheet to see the original visual appearance.

    If you do not want the user to see whether a tool button is checked or not, you should consider setting checkable to false. That would prevent the "move" too.



  • Thanks - but I need it to be checkable, but not move at the same time. Is this possible?


  • Moderators

    So, when you say you want it to be checkable but not move, are you anticipating just a color change, or what?



  • Correct, just a color change (which I have accomplished with CSS). I would use a regular button and just make it flat, but I cannot align the button label under the icon and have it centered.



  • -Would it be feasible for you to just use different images for your button in the different states? That way, the whole rendering of the original button will be bypassed and your own images will be used instead. You can position those any way you like...-

    Edit: The solution below is much better than this one.



  • Hi,

    if you will keep the rendering through the current style and avoid the shift use a QProxyStyle for the button where you set QStyle::PM_ButtonShiftHorizontal and QStyle::PM_ButtonShiftVertical in QStyle::pixelMetric() to 0.

    @class MyProxyStyle : public QProxyStyle
    {
    public:
    int pixelMetric(PixelMetric metric, const QStyleOption *option = 0, const QWidget *widget = 0) {
    int ret = 0;
    switch (metric) {
    case QStyle::PM_ButtonShiftHorizontal:
    case QStyle::PM_ButtonShiftVertical:
    ret = 0;
    break;
    default:
    ret = QProxyStyle::pixelMetric(metric, option, widget);
    break;
    }
    return ret;
    }
    };@



  • [quote author="Lykurg" date="1333011052"]Hi,

    if you will keep the rendering through the current style and avoid the shift use a QProxyStyle for the button where you set QStyle::PM_ButtonShiftHorizontal and QStyle::PM_ButtonShiftVertical in QStyle::pixelMetric() to 0.
    [/quote]

    Hey! That's great information! Thanks for sharing this.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.