• 7306阅读
  • 6回复

请问如何使QtextEdit  禁止使用退格键 [复制链接]

上一主题 下一主题
离线dhy8281905
 

只看楼主 倒序阅读 楼主  发表于: 2010-10-08
— 本帖被 XChinux 执行加亮操作(2010-10-21) —
如题,想让QTextEdit一直输入,不可以使用退格键,不知道怎么实现啊
离线xtfllbl

只看该作者 1楼 发表于: 2010-10-08
使用过滤器QEventFilter来处理键盘事件,屏蔽退格。
上海欢迎您
离线dhy8281905

只看该作者 2楼 发表于: 2010-10-08
你好  请问具体是怎么实现的呢?
离线xtfllbl

只看该作者 3楼 发表于: 2010-10-08
void QObject::installEventFilter ( QObject * filterObj )
Installs an event filter filterObj on this object. For example:
monitoredObj->installEventFilter(filterObj);
An event filter is an object that receives all events that are sent to this object. The filter can either stop the event or forward it to this object. The event filter filterObj receives events via its eventFilter() function. The eventFilter() function must return true if the event should be filtered, (i.e. stopped); otherwise it must return false.
If multiple event filters are installed on a single object, the filter that was installed last is activated first.
Here's a KeyPressEater class that eats the key presses of its monitored objects:
class KeyPressEater : public QObject
{
     Q_OBJECT
     ...
protected:
     bool eventFilter(QObject *obj, QEvent *event);
};
bool KeyPressEater::eventFilter(QObject *obj, QEvent *event)
{
     if (event->type() == QEvent::KeyPress) {
         QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
         qDebug("Ate key press %d", keyEvent->key());
         return true;
     } else {
         // standard event processing
         return QObject::eventFilter(obj, event);
     }
}
And here's how to install it on two widgets:
KeyPressEater *keyPressEater = new KeyPressEater(this);
QPushButton *pushButton = new QPushButton(this);
QListView *listView = new QListView(this);
pushButton->installEventFilter(keyPressEater);
listView->installEventFilter(keyPressEater);
The QShortcut class, for example, uses this technique to intercept shortcut key presses.
Warning: If you delete the receiver object in your eventFilter() function, be sure to return true. If you return false, Qt sends the event to the deleted object and the program will crash.
Note that the filtering object must be in the same thread as this object. If filterObj is in a different thread, this function does nothing. If either filterObj or this object are moved to a different thread after calling this function, the event filter will not be called until both objects have the same thread affinity again (it is not removed).
See also removeEventFilter(), eventFilter(), and event().
上海欢迎您
离线gcp543706787

只看该作者 4楼 发表于: 2010-10-08
用正则表达式不可以么?
离线downstairs

只看该作者 5楼 发表于: 2011-04-16
keypress()
喜爱编程的猫头鹰
离线roywillow

只看该作者 6楼 发表于: 2011-04-16
个人认为用事件过滤器是比较方便的方法
专业维修核潜艇,回收二手航母、二手航天飞机,大修核反应堆,拆洗导弹发动机更换机油,无人侦察机手动挡改自动,航天飞机保养换三滤,飞碟外太空年检 ,各型号导弹加装迎宾踏板,高空作业擦洗卫星表面除尘、打蜡及抛光,东风全系列巡航导弹。并提供原子对撞机。量大从优,有正规发票。
快速回复
限100 字节
 
上一个 下一个