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.
-
-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.
-
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;
}
};@This post is deleted! -
Hello, everyone!
Team lead from neighbor department told me very interesting alternative idea for solving this problem.
You can use such strategy:QPushButton *p = new QPushButton("", yourWidget); QLabel *l = new QLabel("yor button text", p);
And you get text without shifting and still clickable button without special code.
Only you need is to control QLabel size to be the same as button size.
This was tried for Qt5.7.0. -
Hello, everyone!
Team lead from neighbor department told me very interesting alternative idea for solving this problem.
You can use such strategy:QPushButton *p = new QPushButton("", yourWidget); QLabel *l = new QLabel("yor button text", p);
And you get text without shifting and still clickable button without special code.
Only you need is to control QLabel size to be the same as button size.
This was tried for Qt5.7.0.@Kirik516 said in Prevent flat QToolButton from moving when clicked:
Only you need is to control QLabel size to be the same as button size.
You can use a layout for that:
QPushButton *p = new QPushButton(QString(), yourWidget); p->setLayout(new QVBoxLayout()); p->layout()->addWidget(new QLabel("your button text"));
But honestly adding a sub widget to position text is a bit overkill. In reality what you want is a button with custom drawing, so I would just inherit QToolButton and customize its
paintEvent
. -
@Kirik516 said in Prevent flat QToolButton from moving when clicked:
Only you need is to control QLabel size to be the same as button size.
You can use a layout for that:
QPushButton *p = new QPushButton(QString(), yourWidget); p->setLayout(new QVBoxLayout()); p->layout()->addWidget(new QLabel("your button text"));
But honestly adding a sub widget to position text is a bit overkill. In reality what you want is a button with custom drawing, so I would just inherit QToolButton and customize its
paintEvent
.@Chris-Kawa
You are right. QLayout controls size very well.
But if we about the best method for topic's problem I think @Lykurg gave the most suituble solution.
Nevertheless, I think that adding QLabel upon QPushButton is very simple and usefull thing.
At least you will make your colleagues laugh, when you tell them this method.