How to select the Values from a QLineEdit using the Mouse Press Event?
-
I'm a new bie to Qt. Plz Help Me to solve my doubt. I have a LineEdit With a Text in it. And if i click somewhere in the LineEdit, the text value should be selected and if i enter any other characters in it, the old selected value has to be erased and the new one should be placed in the LineEdit.
I Checked for the LineEdit Signal Clicked, but it is not there for a LineEdit. Then i came to know that i have to subclass my LineEdit and using the Focus Event i can get it. But here i don't want to Subclass my LineEdit. Can anyone please tell me how to select the entire text of a line edit using mouse click without subclassing the QLineEdit.
-
Hello!!
Maybe I didn't understand your problem, but in my last project, I use QLineEdit and when I double click on it, the text is automatically selected, one double click for the nearest word and 2 double click for the whole line.
After that, if I enter any character, the selected text is erased. -
You have to subclass QLineEdit and reimplement a (single) click event that calls selectAll
@class MyLineEdit : public QLineEdit
{
Q_OBJECT
public:
explicit MyLineEdit(QWidget *parent = 0);protected:
void mouseReleaseEvent(QMouseEvent *);};@
and in the cpp...
@void MyLineEdit::mouseReleaseEvent(QMouseEvent *)
{
selectAll();
}
@you can do it with the press event too, but since you can press on the line edit and move away from it and then release outside, I think it is better to use the release event to make sure the user wants to do a complete click on the line edit
-
[quote author="Belencina1" date="1358171882"]
I use QLineEdit and when I double click on it, the text is automatically selected, one double click for the nearest word and 2 double click for the whole line.
After that, if I enter any character, the selected text is erased.[/quote]How did u did that? Did u sub classed your Line Edit. Or you tried with any other ways?
-
[quote author="utcenter" date="1358175843"] you can do it with the press event too, but since you can press on the line edit and move away from it and then release outside, I think it is better to use the release event to make sure the user wants to do a complete click on the line edit[/quote]
Yup, I tried using Event Filters. In that i tried,
@if(keyEvent->key() == QEvent::MouseButtonPress)
{
qDebug() << "Line Edit Value Selected"; // Just tried using a qDebug. But it didn't works.
}@But i couldn't get the proper solution. Can u plz tell me where am going wrong,
-
I think keyEvent is only concerned with with keyboard events, otherwise you should see your message.
QEvent has a mouse button press, but QKeyEvent doesn't, so they cannot be equal as you test.
The QLineEdit, as a subclass of QWidget, has a mousePress and mouseRelease event, it does not have clicked() signal.
So the way to achieve what you want is the method I showed you above. If you want, you can replace the Release with a Press event, it is still guaranteed to work.
-
[quote author="Joy Rider" date="1358194278"]
Yup, I tried using Event Filters. In that i tried,@if(keyEvent->key() == QEvent::MouseButtonPress)
{
qDebug() << "Line Edit Value Selected"; // Just tried using a qDebug. But it didn't works.
}@But i couldn't get the proper solution. Can u plz tell me where am going wrong,
[/quote]Using Event Filters, It will Work Fine. Only thing you have to do is, a small change in your if condition to if(event->type == QEvent::MouseButtonRelease)
Here's the code for selecting the QLineEdit Text using event filter.
@if(event->type() == QEvent::MouseButtonRelease)
{
q_LineEdit->selectAll();
}
@Thanks & Regards...
-
[quote author="Belencina1" date="1358171882"]
I use QLineEdit and when I double click on it, the text is automatically selected, one double click for the nearest word and 2 double click for the whole line.
After that, if I enter any character, the selected text is erased.[/quote]That's Right.... :-)
Thanks & Regards
-
bq. If you are not subclassing QLineEdit then make sure that you use lineEdit->installEventFilter(this) and then in the eventFilter() function of your qwidget/qmainwindow you check for the target if (target == lineEdit ) then perform the valid operations.
P.S : not required for the current topic
-
[quote author="Joy Rider" date="1358193837"]
[quote author="Belencina1" date="1358171882"]
I use QLineEdit and when I double click on it, the text is automatically selected, one double click for the nearest word and 2 double click for the whole line.
After that, if I enter any character, the selected text is erased.[/quote]How did u did that? Did u sub classed your Line Edit. Or you tried with any other ways?
[/quote]
It worked for me this way automatically, without subclassing or using any option.
-
[quote author="Joy Rider" date="1358170755"]I'm a new bie to Qt. Plz Help Me to solve my doubt. I have a LineEdit With a Text in it. And if i click somewhere in the LineEdit, the text value should be selected and if i enter any other characters in it, the old selected value has to be erased and the new one should be placed in the LineEdit.
I Checked for the LineEdit Signal Clicked, but it is not there for a LineEdit. Then i came to know that i have to subclass my LineEdit and using the Focus Event i can get it. But here i don't want to Subclass my LineEdit. Can anyone please tell me how to select the entire text of a line edit using mouse click without subclassing the QLineEdit. [/quote]
You dont need to subclass anything , The double click text selection property is by default available with the lineEdit. What steps are you following ?