PDA

View Full Version : Hide cursor of QLineEdit



Alundra
2nd June 2014, 05:09
Hi,
Is it possible to hide the cursor of QLineEdit and show it when needed ?
It's because I have a widget I would enable edit only when one click on it, readOnly works but it has problem with cursor when changing.
Thanks

Rajesh.Rathod
2nd June 2014, 11:19
You need to derive class from QLineEdit and in the following situation you should make the control read only.
1. focusOutEvent.
2. returnPressed signal.

& on mouse click you can remove the read only status ...I think that is the behaviour you want...

wysota
2nd June 2014, 11:22
Hi,
Is it possible to hide the cursor of QLineEdit and show it when needed ?
It's because I have a widget I would enable edit only when one click on it, readOnly works but it has problem with cursor when changing.
Thanks

What problems does "readOnly" have with the cursor?

Alundra
2nd June 2014, 19:10
https://bugreports.qt-project.org/browse/QTBUG-37686
this problem, it's my bug report.

wysota
3rd June 2014, 10:46
If you want the widget to have keyboard focus after being double-clicked then set focus on it there. I don't see any Qt bug there. I also don't understand what you are trying to do. You don't have to manipulate "readOnly" property to make a widget gain or lose focus.

Alundra
3rd June 2014, 14:30
The problem is the cursor is not visible and using setFocus() that doesn't show it too, you can try the code I have write in the BugReport, that show the problem.
To have the cursor visible, when double click (on the sample of the bug report), you have to use keyboard, only when you use keyboard the cursor is visible again.

wysota
3rd June 2014, 15:22
The problem is the cursor is not visible and using setFocus() that doesn't show it too, you can try the code I have write in the BugReport, that show the problem.
To have the cursor visible, when double click (on the sample of the bug report), you have to use keyboard, only when you use keyboard the cursor is visible again.

As I said your test report is broken, at least when using Qt4.

I don't have any problems with the cursor using the following code:


#include <QtGui>

class LineEdit : public QLineEdit {
Q_OBJECT
public:
LineEdit(QWidget *parent = 0) : QLineEdit(parent) { setReadOnly(true); }
protected:
void mouseDoubleClickEvent(QMouseEvent *) {
setReadOnly(false);
setFocus();
activateWindow();
}
};

#include "main.moc"

int main(int argc, char **argv) {
QApplication app(argc, argv);
LineEdit le;
le.show();
le.setText("ABC");
return app.exec();
}

Still, the real question is why do you want to use "readOnly" here? You don't need to mark a widget read-only to not see the cursor. Just focus-out of the widget and/or don't accept focus in situations when you don't want to. Maybe you just need to change the focus policy (e.g. to Qt::ClickFocus)?

Alundra
3rd June 2014, 15:59
Using Qt4 the problem is not there :
http://www.qtcentre.org/threads/58482-QLineEdit-cursor-not-vissible-or-blocked?p=260658#post260658
Qt::ClickFocus is maybe the good way to go here, I will try it.