Focus Rectangle paintEvent
-
Hi! I'm trying to make my application draw a focus rectangle (the blue border around ex. QLineEdits). The painting itself works, but the style of the drawn focusRect doesn't match at all. What I get instead is a dotted line, akin to the ones you see in a QTableWidget when selecting an item. How do I access and draw it using the style used by default of other widgets in the application?
FocusFrame::FocusFrame(QWidget *parent) : QFrame(parent) { }
void FocusFrame::paintEvent(QPaintEvent *) { QPainter painter(this); QStyleOptionFocusRect option; option.initFrom(this); //I thought this would give it the default appearance of focus rectangles style()->drawPrimitive(QStyle::PE_FrameFocusRect, &option, &painter, this); }
Thanks in advance!
-
Hi,
Wouldn't QRubberBand do what you want ?
-
It kind of works, though I can't find any way to fix the blue background. Also, will the QRubberBand style always match the focusFrame style independent of OS?
Desired look:
Current (with option.backgroundColor = yellow just to show the issue more clearly):
Rubberband attempt:
-
You can set the shape of the QRubberBand to line. That said, which OS are you running ?
Usually it is in charge of handling the focus rect around the widget that you are currently editing or about to activate with the entrer key in the case of a button. -
Tried setting it to line, but the background still gets filled and appearance doesn't differ from rectangle at all.
I'm currently using Windows, but the aim of this is to get my "custom" focus rectangle to mirror the appearance of the ones of QLineEdit on at least Windows, Mac and Linux. If there was a way to ask Qt for the focus style and use that to draw, that'd be perfect - but maybe there's an easier solution?
To clarify: I've got 4 QLineEdits and when focusing any of them, I want "frame" to show as focused (without moving focus from the current QLineEdit.
layout = new QHBoxLayout(frame); layout->addWidget(textIP1); layout->addWidget(labelDot1); layout->addWidget(textIP2); layout->addWidget(labelDot2); layout->addWidget(textIP3); layout->addWidget(labelDot3); layout->addWidget(textIP4);
If there's some kind of way to get frame to show itself as focused without manually drawing a focus frame, that'd solve the issue as well.
-
A simpler way would be to use a style sheet:
QLineEdit:focus { border: 2px solid #000080; }
Note that not all desktop environment uses focus rectangle by default so your application style may go against the platform guidelines which your users might not appreciate.
-
@SGaist said in Focus Rectangle paintEvent:
A simpler way would be to use a style sheet:
QLineEdit:focus { border: 2px solid #000080; }
Note that not all desktop environment uses focus rectangle by default so your application style may go against the platform guidelines which your users might not appreciate.
Stylesheet sadly wouldn't work due to it applying to textIP1-4. It would also have the issue of not following the style of the desktop environment.
Is there no way to properly make the custom widget follow the styling independently of environment style? My main goal is to make "frame" indistinguishable from a regular QLineEdit.
-
Did you consider using a proxy style ?