qtoolbutton/qpushbutton icon based on button's size
-
sorry, should've mentioned this (it's why my buttons can't use the available space). i use this code:
setMinimumSize(width, height); setIconSize(QSize(width, height));
i want to have the pictures of brush styles on the buttons. here's the current code:
void PushButton::drawPattern(int width, int height, const QString &color, const QString &fillColor, int lineWidth, int pattern) { QPixmap pixmap(width, height); pixmap.fill(Qt::transparent); QColor bgColor(color); QPainter painter(&pixmap); painter.setBrush(bgColor); painter.setPen(Qt::NoPen); // Draw the rectangle QRect pixmapRect(1, 1, width, height); painter.drawRect(pixmapRect); QBrush patternBrush; patternBrush.setColor(fillColor); patternBrush.setTexture(get_pixmap(pattern)); painter.setBrush(patternBrush); // Draw the rectangle painter.drawRect(pixmapRect); if (lineWidth) { QPen patternPen; patternPen.setCapStyle(Qt::SquareCap); patternPen.setJoinStyle(Qt::MiterJoin); // Draw the outline patternPen.setColor(lineColor); painter.setBrush(Qt::NoBrush); patternPen.setWidth(lineWidth); painter.setPen(patternPen); painter.drawRect(pixmapRect); } QIcon icon(pixmap); setIcon(icon); }
-
@user4592357
Hi
That sets only min size, not sure how that prevents from using all of the space?Ah so you want entire button to be the icon/pattern and no text and other button-ish features?
I wonder why not simply draw it directly then ? (in paint event)
you seems to have a custom, class already ? (PushButton)
You could use dynamic properties to specify
color, fillColor, lineWidth, pattern
(so you can set in Designer if you need)
and use promotion to use your custom button directly in Designer.Maybe i misunderstand the actual issue ?
Could you show image of what you have ? -
yes i want the whole button to be the pattern, no matter what size the button is.
this is what i have now: https://ibb.co/FWmbDZX -
@user4592357
ah, im an idiot :)
You said tool button.
They come with fixed SizePoliy
Just change to Preferred and they take all space. -
yes, that did. but now the "icons" aren't drawn on the buttons, but rather behind them (i could hardly see them, i saw a part of the icon behind the button).
-
@user4592357
Can you show that effect ?
It wont scale the icon, but keep the size to IconSize.
Why dont just paint it directly ? ( inherit ToolButton and implement paintEvent ) -
Hi
Like
with a small custom class.
Test project
https://www.dropbox.com/s/62flul5h8aa5shj/PatternButton.zip?dl=0How do you determine the pattern it should use ?
I just used variables inside, but if you generate the buttons you could just give
as parameters. -
oh, the problem was that i was calling
QToolButton::paintEvent(event);
in the end.
without it the patterns are being drawn.
but if i don't call it, well, my button isn't a button and cannot be pressed, etc. -
@user4592357
If you dont call QToolButton::paintEvent(event), the button
can still be pressed but it wont draw the effect.
But will send signal though.You can just do it revers
call QToolButton::paintEvent(event) in top
then draw pattern with slightly smaller rect on top of what already drawn.
(i assume you need no texts) -
@mrjj
i managed to do that but i want to remove all spaces between buttons.
i've setsetContentsMargins(0, 0, 0, 0)
andsetSpacing(0)
for the layout.
this is what i have now: https://ibb.co/qYCm8vR
can't i remove those spaces? -
@user4592357
Hi
The tool buttons seems to reserve some pixels so there will be a small space.
The lower ones are Frames and both layouts have same settings so its fair to say
its the closest you get with ToolButtons as seen in top. -
@mrjj
hi,
when i add the buttons to a layout, they take up more space (just because it's available).
how can i make the buttons be in their minimum size in that layout?p.s. this is what i have set for the button:
setMinimumSize(...); setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum);
-
@user4592357
But would would be the minimum ?
You can set setMaximumSize of them if you want to limit it to
certain size. like 64x64. else layout makes them use the extra space. -
@user4592357
im not sure i understand.
You do not want a max size for widgets but at same time they are not allowed to use
all the space ?
well yes spacers should compress to minimum -
yes maybe that's weird but that's what i need.
when i added the spacer at col 0, and then the rest of the widgets, only the first widget got added. -
@user4592357
Im not sure what you mean . Adding cant fail due to spacer.
did you set Minimum on the widgets as else they get compressed totally.with no Minimum set
-
i added spacers at both ends of hbox layout, but now the whole container widget gets larger instead of the buttons getting smaller (since there is a spacer around them)...
-
@user4592357
Hi
Its impossible to guess at with no code or pictures.
If i do the same with Horz layout i get this