I created a simple 10x10 black box and added it to a QToolButton as follows:
QIcon minIcon;
minIcon.addFile("c:/tmp/black10x10.png");
minButton = new QToolButton;
minButton->setIcon(minIcon);
However, it appears on screen shifted left (image enlarged for convenience):
Some squinting in Gimp told me that grey area to the left is 56 pixels zoomed and grey area to the right is 68. This misalignment is very noticeable even without zoom - that was how I spotted it in the first place. So, how do I center this icon?
P.S. Tried using a QPushButton without text. Same effect.
It's probably a bit late now, but I stumbled across the same issue and found the following code snippet in QTs qstylesheet.cpp
case CT_ToolButton:
if (rule.hasBox() || !rule.hasNativeBorder() || !rule.baseStyleCanDraw())
sz += QSize(3, 3); // ### broken QToolButton
This would increase your even sized icon to be odd sized and therefor not centered. I'm not sure why there's an addition of 3 but the comment suggest it's a fix for something...
Unfortunately this doesn't fix the issue, it just kind of explains the source of it. But it might help someone to find a better solution than "make all your icons odd sized".
Related
I am new to C++,Qt and Visual Studio and this is my first post on Stack Overflow.
I apologize in advance if this is a repeated question, I tried searching for a similar question but couldn't find one. Let me know if this is a repeated question and I will delete it.
I am trying to create a line plot using QWidget::paintEvent(). The line plot I am drawing is actually a QPainterPath. I want to detect when the mouse hovers over my line plot and so I create a small rectangle where my mouse cursor is and detect when this rectangle intersects with my line plot using bool QPainterPath::intersects() function. The problem is that this function returns true even when my mouse is not exactly over my line plot. In the Image 1 (I am not allowed to embed images yet) my line plot is the thick black curve and the bool QPainterPath::intersects() returns true even when my cursor is over the yellow region. As per the Qt document this is because:
There is an intersection if any of the lines making up the rectangle crosses a part of the path or if any part of the rectangle overlaps with any area enclosed by the path.
There is no way to have a QPainterPath without any enclosed area as Qt only provides two types of fill for QPainterPath: Qt::OddEvenFill or Qt::WindingFill. (To be honest, I find this kind of annoying, since an open path is a series of line segments connected end-to-end, if someone wants to enclose an area they can easily connect the first and last point using either QPainterPath::lineTo() or QPainterPath::moveTo() functions)
Anyway, I decided to get smarter than Qt and drew two extra QPainterPath with pathUp being a few pixels above my line plot and pathDn being a few pixels below my line plot. Image 2 shows these 3 line plots, red one is pathUp, black one is real line plot and green one is pathDn. I thought I coould detect the intersection in the QWidget::mouseMoveEvent() by using the following code:
// cRect: Rectangle at mouse cursor position
if((pathUp.intersects(cRect) && (!pathDn.intersects(cRect))) || ((!pathUp.intersects(cRect)) && pathDn.intersects(cRect)))
{
qDebug() << "Intersects";
}
But this still produces wrong results because now the enclosed area is different, as you can see in Image 3 the green area is an enclosed area of pathDn and red area is the enclosed area of pathUp. The thick black curve is again the line plot that I want to detect my mouse hover on. This enclosed area is not affected by Qt::setFillRule of QPainterPath.
What's even more frustrating is that I tried this technique using QPolygonF instead of QPainterPath on QWidget and the results were exactly the same. I also tried QGraphicsView, there I used QGraphicsPathItem to create my line plot and then used QGraphicsScene::focusItemChanged() signal to detect when I click on my line plot. It again produced the same result of detecting the click when my cursor is over the enclosed area. I do not want to create a custom QGraphicsItem (unless I absolutely have to) just to reimplement it's hoverEnterEvent() and hoverLeaveEvent() method because of the limitations imposed on the boundingRect() of the QGraphicsItem as explained in Qt Docs:
QGraphicsScene expects all items boundingRect() and shape() to remain unchanged unless it is notified. If you want to change an item's geometry in any way, you must first call prepareGeometryChange() to allow QGraphicsScene to update its bookkeeping.
Since I making a plot in real-time the boundingRect() will change quite frequently (> 20 Hz), which will result in an extra computational burden on the software. Is there any way I can solve my problem without creating a custom QGraphicsItem?
P.S. I have been using Stack Overflow for many years whenever I got stuck. I just never made an account here because I never needed to post anything. You guys are the best and I am very happy to be a part of this community!
when I run my application under Ubuntu (Gnome/Unity) all wxBitmapButtons come with a white border (with a size of about 2..4 pixels). This border also shifts the position and extends the size of the total button by the borders size.
When I specify flag wxNO_BORDER (or wxBORDER_NONE) the border is no longer shown by default bur re-appears on mouse-over.
So my question: how can I remove this border completely? Normal wxButtons do not show this behaviour, only wxBitmapButton is affected...
wxBORDER_NONE is supposed to work, so if it doesn't, it looks like a bug in wxGTK. It's pretty strange that it does work for wxButtons as wxBitmapButton is basically the same class. However if it's really the case, you should consider just using wxButton with a bitmap instead.
I'm working on a Qt project and I'm struck on a QtDesign issue.
I've at the moment a 4x4 grid, like that:
Top Left (TL): SourceSelector
Top Right (TR): groupBox_vizualization
Bottom Left (BL): groupBox_filtrage
Bottom Right (BR): groupBox_spectre
I would like the SourceSelector to have a 100% width when I select one of the widgets in stackWidget_source. At the moment, when I select this widget I set the width of the "vizualisation" widget to 0, and hide it. I would like the SourceSelector to use 100% of the width in that case only. I order to have a grid (2)x(1 1).
But TL/BL et TR/BR always have the same width. So I can't make the TR one to use 100% as the BR has a fixed size.
Do you guys have any idea ?
Well, I solved my problem with a Grid Layout at the bottom and a HLayout at the top. Both of them placed in a VLayout.
I have a few icons in .ico files in my C++ MFC program. I use DrawIcon and DrawIconEx to draw them onto a memory bitmap and they come out with some sort of shadow-like border at the edge of the edges of the visible content. The pixels look like they are in the transparent part of the icon and in the area that I drew in as white. Windows appears to be
I have edited the .ico files with Axialis IconWorkshop, Visual Studio, and looked at them with a few online ico-to-png converters. They look fine until I draw with them. they were okay for years and something changed in the last few years with Windows 8 or Windows 10.
Any ideas how to draw these things right?
Here's how one of my icons look in an icon editor:
And then how they look wrong in my program (blown up for you to see better):
I'm looking for possible viewport-window or other device context settings that might have some effect on this stuff.
I almost posted my own answer because DrawIconEx seemed to fix the problem because I have a high DPI display. But at closer inspection, the garbage is still there, just at the proper smaller pixel count, not scaled up by the DrawIcon function.
The DrawIcon and DrawIconEx functions on a high DPI display scales up the icons that they draw. Even though they seem to be trying to draw the icon at a 1:1 size, there is still some scaling going on.
There were a few online sources of information, none of which individually was useful, that together hinted on the cause of the problem. The only possible solution that I found was to create a 40x40, and maybe a 64x64 icon within the same icon file and let the icon drawing functions get the right icon to draw.
This is not really an answer since I have not tried to use a different size icon in the ico file. I just switched my app to be unaware of high DPI displays.
Just use DrawIconEx without DI_DEFAULTSIZE flag, it's the one causing the mess. E.g. DrawIconEx(Handle, 32, 32, ArrowCur, 0, 0, 0, 0, DI_NORMAL);
When i nest in gtkmm multiple Paned-Widgets there is always a border around the whole Paned and it can get really big and ugly (as seen in the picture). Is there any way to remove the border so there is only that seperator left? And is it possible to give that seperator an own style?
I found the problem.
The problem wasnt the paned widget itself but the viewports i put in it as childs. Just set the the shadow type of the viewports to Gtk::SHADOW_NONE and no borders will be drawn at all.