What I think you have here is an insert cursor. There are three possibilities:
- You click right of center and the cursor is placed right of the existing character. The max length stops you typing another character.
- You click left of center and the cursor is placed before the existing character. The max length will stop you inserting another character.
- You click and it selects the character which will be erased when you type another character.
If you use a QLineEdit subclass that selects all text when focus moves into the widget then you should get the overwriting behaviour (3) by default.
#ifndef OVERWRITINGEDIT_H
#define OVERWRITINGEDIT_H
#include <QLineEdit>
{
Q_OBJECT
public:
explicit OverwritingEdit
(QWidget *parent
= nullptr
);
signals:
// QWidget interface
protected:
};
#endif // OVERWRITINGEDIT_H
#ifndef OVERWRITINGEDIT_H
#define OVERWRITINGEDIT_H
#include <QLineEdit>
class OverwritingEdit : public QLineEdit
{
Q_OBJECT
public:
explicit OverwritingEdit(QWidget *parent = nullptr);
signals:
// QWidget interface
protected:
void focusInEvent(QFocusEvent *event);
};
#endif // OVERWRITINGEDIT_H
To copy to clipboard, switch view to plain text mode
#include "overwritingedit.h"
OverwritingEdit
::OverwritingEdit(QWidget *parent
){
}
{
// Select all text
selectAll();
}
#include "overwritingedit.h"
OverwritingEdit::OverwritingEdit(QWidget *parent)
: QLineEdit(parent)
{
}
void OverwritingEdit::focusInEvent(QFocusEvent *event)
{
QLineEdit::focusInEvent(event);
// Select all text
selectAll();
}
To copy to clipboard, switch view to plain text mode
This will still not handle, for example, pressing 5 then 4 and expecting to get 4. Pressing 5 leaves the insert cursor after the 5 and you are back into scenario (1) above. Perhaps you could select all in a slot attached to textChanged() or perhaps you are better off with a completely custom widget.
Bookmarks