qtextedit固定内容_在QTextEdit中锁定一行

如何在QTextEdit控件中锁定一行或部分内容?当光标移动到该行的特定部分时,光标会自动移动到不属于该部分的下一个位置。可以考虑通过重写QTextEdit的event()方法来处理可能改变锁定行的所有事件。
摘要由CSDN通过智能技术生成

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:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值