Prevent flat QToolButton from moving when clicked
-
Thanks - but I need it to be checkable, but not move at the same time. Is this possible?
-
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.
-
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. -
@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.