是否可以限制QKeySequence
在QKeySequenceEdit
中仅显示一个快捷方式?目前,它最多支持4个快捷方式。我的应用程序仅支持一种快捷方式的键序列,例如Ctrl+A
或Ctrl+C
,而不是例如Ctrl+A, D
或Ctrl+C, X, Z
。
是否可以将QKeySequence
或QKeySequenceEdit
限制为一个键序列?
最佳答案
解决了它,不是最好的解决方案,而是快速的解决方案。。。如果您想进一步定制,我想必须自己构建。
customkeysequenceedit.h:
#ifndef CUSTOMKEYSEQUENCEEDIT_H
#define CUSTOMKEYSEQUENCEEDIT_H
#include <QKeySequenceEdit>
class QKeyEvent;
class CustomKeySequenceEdit : public QKeySequenceEdit
{
Q_OBJECT
public:
explicit CustomKeySequenceEdit(QWidget *parent = 0);
~CustomKeySequenceEdit();
protected:
void keyPressEvent(QKeyEvent *pEvent);
};
#endif // CUSTOMKEYSEQUENCEEDIT_H
customkeysequenceedit.cpp:
#include "customkeysequenceedit.h"
#include <QKeyEvent>
CustomKeySequenceEdit::CustomKeySequenceEdit(QWidget *parent) : QKeySequenceEdit(parent) { }
CustomKeySequenceEdit::~CustomKeySequenceEdit() { }
void CustomKeySequenceEdit::keyPressEvent(QKeyEvent *pEvent)
{
QKeySequenceEdit::keyPressEvent(pEvent);
QKeySequence seq(QKeySequence::fromString(keySequence().toString().split(", ").first()));
setKeySequence(seq);
}