Unsolved How to handle when an QLineEdit widget is selected and no longer selected
-
@Karine
Yon can know if you have the focus with:if(QApplication::focusWidget()==this) { // has focus }
There's focusWidget in QWidget as well:
Returns the last child of this widget that setFocus had been called on.
Not sure what's really mean. Have a test if it's return your own widget.
Or try: parentWidget().focusWidget() -
@Christian-Ehrlicher sorry i didn't see your answer because the jokes caught of my attention.
I tried this:
class LineEdit : public QLineEdit { Q_OBJECT public: LineEdit(QWidget* parent = 0) : QLineEdit(parent) { } QGraphicsDropShadowEffect* dse; void focusInEvent(QFocusEvent* event) { dse = new QGraphicsDropShadowEffect(); dse->setBlurRadius(60); dse->setOffset(0); dse->setColor(0x007eff); this->setGraphicsEffect(dse); qDebug() << "focus"; QLineEdit::focusInEvent(event); } void focusOutEvent(QFocusEvent* event) { qDebug() << "lost focus"; dse->deleteLater(); this->update(); this->repaint(); QLineEdit::focusOutEvent(event); } };
But after the widget lost focus it did not remove the blur effect, any idea why?
When i minimize/restore my GUI, then the blur effect is removed. -
@Karine
record the state in a member variable and do the drawing in paintEvent() -
@mpergand could you give an example? im not sure how to do this inside of the paint event
-
class LineEdit : public QLineEdit { Q_OBJECT public: LineEdit(QWidget* parent = 0) : QLineEdit(parent) { blurEffect = new QGraphicsBlurEffect(this); setGraphicsEffect(blurEffect); blurEffect->setEnabled(false); setText("Hello"); } private: void focusInEvent(QFocusEvent* event) { blurEffect->setEnabled(false); } void focusOutEvent(QFocusEvent* event) { blurEffect->setEnabled(true); } /* void paintEvent(QPaintEvent* ev) { QLineEdit::paintEvent(ev); }*/ QGraphicsBlurEffect * blurEffect; };
-
Since the blur effect is outside of the widget's boundaries, the parent needs a redraw
parentWidget()->update();
this->update() and repaint() are not needed. I would also go with the solution from @mpergand to not re-create the effect every time but simply enable/disable it.
/edit: with @mpergand's solution there is also no extra redraw of the parent needed. I'll take a look on it why this is needed when the effect is destroyed. Looks like a small bug to me.
-
Using the code post by @mpergand when the effect is applied to the widget the caret disappears, does this also happen on your side?
class LineEdit : public QLineEdit { Q_OBJECT public: QGraphicsDropShadowEffect* dse; LineEdit(QWidget* parent = 0) : QLineEdit(parent) { dse = new QGraphicsDropShadowEffect(this); setGraphicsEffect(dse); dse->setBlurRadius(60); dse->setOffset(0); dse->setColor(0x007eff); dse->setEnabled(false); } void focusInEvent(QFocusEvent* event) { dse->setEnabled(true); QLineEdit::focusInEvent(event); } void focusOutEvent(QFocusEvent* event) { dse->setEnabled(false); QLineEdit::focusOutEvent(event); } };
-
Created a bug report for the problem when an enabled graphics effect gets deleted: https://bugreports.qt.io/browse/QTBUG-109165
On linux with Qt5.15 I can see the cursor even when the effect is applied.
-
@Christian-Ehrlicher i think the caret is disappearing because of the stylesheet, could you try with this:
QLineEdit { background-color: rgba(0, 0, 0, 190); border-radius: 12px; border: 1px solid rgb(22, 22, 22); padding-left: 10px; font-size: 12px; font-weight: 900; color: rgb(154, 154, 154); } QLineEdit:hover { color: white; }
-
@Karine said in How to handle when an QLineEdit widget is selected and no longer selected:
background-color: rgba(0, 0, 0, 190);
Removing the alpha from the background, the caret is now visible, could it also be a bug?