Unsolved How to handle when an QLineEdit widget is selected and no longer selected
-
I tried subclassing it but im confused about how to read when its selected and no longer selected:
class LineEdit : public QLineEdit { Q_OBJECT public: LineEdit(QWidget* parent = 0) : QLineEdit(parent) { } public slots: void clicked() { qDebug() << "clicked?"; } signals: void clicked() { qDebug() << "?clicked"; } };
-
@Karine said in How to handle when an QLineEdit widget is selected and no longer selected:
when its selected and no longer selected:
There is no such attribute 'selected'. Only if a widget has focus or not.
-
@Karine said in How to handle when an QLineEdit widget is selected and no longer selected:
when its selected and no longer selected
Sounds like Schrodinger widget :-)
Sorry, could not resist :-) -
@jsulm
The code is right. Just it needs to be run on a quantum computer.... -
How fun you'll are, I'm referring to when a line edit is current selected (has focus, the caret is blinking on it) and when no longer.
-
@Karine
But what is your question? You read aQLineEdit
's text vialineEdit->text()
. It does not matter whether theQLineEdit
is selected or has focus or is not.BTW, you cannot have two methods with the same name (
clicked()
) as signals, slots or anything else in a class (ignoring overloads, which is not the case here). -
But what is your question?
I'm asking how to detect when a line edit is currently focused (caret on it) and when it is no longer.
It does not matter whether the QLineEdit is selected or has focus or is not.
I'm applying a blur effect into the line edit widget when its focused, similar to this gif:
I don't think it's possible to apply blur using Stylesheet?
// When the edit is focused (caret on it): QGraphicsDropShadowEffect * dse = new QGraphicsDropShadowEffect(); dse->setBlurRadius(60); dse->setOffset(0); dse->setColor(0x007eff); ui.Line_Edit->setGraphicsEffect(dse); // When it's not longer focused dse->deleteLate();
-
@Karine said in How to handle when an QLineEdit widget is selected and no longer selected:
I'm asking how to detect when a line edit is currently focused (caret on it) and when it is no longer.
And you simply ignored my answer which also includes links:
There is no such attribute 'selected'. Only if a widget has focus or not.
-
@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?