How can I lock a line(or a part of a line) in a "QTextEdit" control? I can do this: when I move the cursor position at that part of the line, the cursor will be moved automatically to the next first position that not belongs to that part of the line. Maybe you have antoher idea.
Thank you!
解决方案
I think the best way to achieve that is to subclass QTextEdit and reimplement the event() method to handle all events that might change a locked line. Something like this:
class MyTextEdit : public QTextEdit
{
Q_OBJECT
public:
bool event(QEvent *event)
{
switch (event->type()) {
case QEvent::KeyPress:
case QEvent::EventThatMayChangeALine2:
case QEvent::EventThatMayChangeALine3:
if (tryingToModifyLockedLine(event)) {
return true; // true means "event was processed"
}
}
return QTextEdit::event(event); // Otherwise delegate to QTextEdit
}
};
Besides QEvent::KeyPress there might be some other events that can change your text. For instance QEvent::Drop
For more information on events see: