Reputation: 115
When I set inputMask
to "99999" , run program, and mouse clicking to QLineEdit
, it filled with 5 spaces, so I have to delete the spaces first, and only then put the value I need.
I've tried to set cursor position to 0 ,but it doesn't work. Also tried to set text to empty string, same result.
ui->engineCapacity_lineEdit->setInputMask("99999");
ui->engineCapacity_lineEdit->setCursorPosition(0);
ui->engineCapacity_lineEdit->setText("");
It suppose to put cursor to the beginning of lineEdit
, instead it's on 5th character of the line
Upvotes: 1
Views: 1799
Reputation: 243965
By default QLineEdit
will set the cursor based on the position of the click, and in the case that the number of characters is restricted as in your case and if you press outside the valid sector then the cursor will be placed on the right edge of that sector.
If you want that to happen then you must implement that logic in the mousePressEvent
:
class LineEdit: public QLineEdit
{
public:
using QLineEdit::QLineEdit;
protected:
void mousePressEvent(QMouseEvent *event)
{
QLineEdit::mousePressEvent(event);
setCursorPosition(0);
}
};
If you want to use that QLineEdit
in your .ui then you must promote it for this you can check the following publications:
Another way to implement the same logic is using an event filter:
*.h
class YourClass: public Foo{
// ...
public:
bool eventFilter(QObject *obj, QEvent *event);
};
*.cpp
// constructor
ui->setupUi(this);
ui->engineCapacity_lineEdit->installEventFilter(this);
// ...
bool YourClass::eventFilter(QObject *obj, QEvent *event){
if(obj == ui->engineCapacity_lineEdit && event->type() == QEvent::MouseButtonPress)
{
ui->engineCapacity_lineEdit->setCursorPosition(0);
}
return Foo::eventFilter(obj, event);
}
Upvotes: 1