Home > database >  Show QWidget as focused
Show QWidget as focused

Time:07-19

I've got four QLineEdit placed inside of a QLineEdits, where I want the first the parent to look as if it is in focus when any of the containing ones is selected. Note: I don't want the focus to actually change, just the "focus frame" (the thin blue border) to appear on the parent LineEdit.

I've tried to draw a rect, but while it works on Windows I'm running into issues of the drawn rectangle not looking like a proper rectangle on ex. Linux, where it is supposed to be rounded. Is there a way to fix this OR, if possible, just make it draw itself as focused despite focus not being on it?

Here's my attempt at drawing a custom rect, but haven't been able to make it successfully mirror the OS style properly.

            if (childHasFocus) {
            QPainter painter(this);

            QLineEdit textBox;
            QColor color = textBox.palette().color(QPalette::Highlight);

            painter.setPen(color);
            QRect rect;
            rect.setTopLeft(QPoint(0,0));
            rect.setWidth(this->width() - 1);
            rect.setHeight(this->height() - 1);
            painter.drawRect(rect);
        }

EDIT: Added an image of the desired look. Note that I'm trying to get it to look like other LineEdits focusframe independent of OS, so hardcoding a blue rectangle won't work due to ex. Linux having a rounded focusframe.

Desired look:

CodePudding user response:

QlineEdit class is also a qwidget, use the setFocus method

https://doc.qt.io/qt-6/qwidget.html#setFocus

CodePudding user response:

Here is a setStyleSheet way to draw a rect .Maybe it doesn't fit your idea but it can solve your needs .

//draw  rect
lineEdit->setStyleSheet("QLineEdit{"
                                "border-style: outset; "
                                "border-width: 1px; "
                                "border-color: #1E90FF;"
                                "}");
//cancel
lineEdit->setStyleSheet("QLineEdit{""}");

If your lineedit already has a stylesheet it will be overwritten.

  • Related