【原】Qt:QKeySequenceEdit只接受单一快捷键
Qt:QKeySequenceEdit只接受单一快捷键
参考:Limit QKeySequence/QKeySequenceEdit to only one shortcut https://stackoverflow.com/questions/30006562/limit-qkeysequence-qkeysequenceedit-to-only-one-shortcut
以上帖子的解决方案,需要做一些小改动。不多说,直接贴代码了。
//QKeySequenceEdit.h
#include <QKeySequenceEdit>
class QtExtKeySequenceEdit : public QKeySequenceEdit
{
public:
QtExtKeySequenceEdit(QWidget *parent);
~QtExtKeySequenceEdit();
protected:
virtual void keyPressEvent(QKeyEvent* pEvent);
};
//QtExtKeySequenceEdit.cpp
#include "QtExtKeySequenceEdit.h"
QtExtKeySequenceEdit::QtExtKeySequenceEdit(QWidget *parent)
: QKeySequenceEdit(parent)
{
}
QtExtKeySequenceEdit::~QtExtKeySequenceEdit()
{
}
void QtExtKeySequenceEdit::keyPressEvent(QKeyEvent* pEvent)
{
__super::keyPressEvent(pEvent);
QKeySequence keySeq = keySequence();
if (keySeq.count()<=0)
{
return;
}
int key = keySeq[0];
if (key==Qt::Key_Backspace || key==Qt::Key_Delete)
{
key = 0;
}
setKeySequence(key);
}
其中,int key = keySeq[0]; 更准确的写法应该是 int key = keySeq[keySeq.count()-1]; ,但实际两句的效果一样,原因在于setKeySequence函数的内部实现。
以上代码,实现两个功能:单一快捷键,按Delete或Backspace键实现清空快捷键的功能。
转载请注明来源,谢谢。
有偿解决C++编程问题,承接项目定制开发;寻一份全职或兼职Windows C++开发工作。联系邮箱:[email protected]。