在Qt C+中定义带有多个字母的快捷方式+; 使用Qt C++代码定义快捷方式: QShortcut *shortcut = new QShortcut(QKeySequence("Ctrl+Shift+S"), this); QObject::connect(shortcut, &QShortcut::activated, [=]{qDebug()<<"Example";}); QShortcut*shortcut=新的QShortcut(QKeySequence(“Ctrl+Shift+S”),此选项); QObject::connect(快捷方式,&QShortcut::activated,[=]{qDebug()
AFAIK,在Qt C+中定义带有多个字母的快捷方式+; 使用Qt C++代码定义快捷方式: QShortcut *shortcut = new QShortcut(QKeySequence("Ctrl+Shift+S"), this); QObject::connect(shortcut, &QShortcut::activated, [=]{qDebug()<<"Example";}); QShortcut*shortcut=新的QShortcut(QKeySequence(“Ctrl+Shift+S”),此选项); QObject::connect(快捷方式,&QShortcut::activated,[=]{qDebug(),c++,qt,C++,Qt,AFAIK,QShortcut当前不支持所描述的功能 解决方法是自己安装事件过滤器 #include <QCoreApplication> #include <QKeyEvent> #include <QVector> class QMultipleKeysShortcut : public QObject { Q_OBJECT public: explicit inline QMultipleKeysShortcut(const QVe
QShortcut
当前不支持所描述的功能
解决方法是自己安装事件过滤器
#include <QCoreApplication>
#include <QKeyEvent>
#include <QVector>
class QMultipleKeysShortcut : public QObject
{
Q_OBJECT
public:
explicit inline QMultipleKeysShortcut(const QVector<int> &Keys, QObject *pParent) :
QObject{pParent}, _Keys{Keys}
{
pParent->installEventFilter(this);
}
Q_SIGNALS:
void activated();
private:
QVector<int> _Keys;
QVector<int> _PressedKeys;
inline bool eventFilter(QObject *pWatched, QEvent *pEvent) override
{
if (pEvent->type() == QEvent::KeyPress)
{
if (_PressedKeys.size() < _Keys.size())
{
int PressedKey = ((QKeyEvent*)pEvent)->key();
if (_Keys.at(_PressedKeys.size()) == PressedKey) {
_PressedKeys.append(PressedKey);
}
}
if (_PressedKeys.size() == _Keys.size()) {
emit activated();
}
}
else if (pEvent->type() == QEvent::KeyRelease)
{
int ReleasedKey = ((QKeyEvent*)pEvent)->key();
int Index = _PressedKeys.indexOf(ReleasedKey);
if (Index != -1) {
_PressedKeys.remove(Index, _PressedKeys.size() - Index);
}
}
return QObject::eventFilter(pWatched, pEvent);
}
};
#包括
#包括
#包括
类QMultipleKeysShortcut:公共QObject
{
Q_对象
公众:
显式内联QMultipleKeysShortcut(常量QVector&Keys,QObject*pParent):
QObject{pParent},_Keys{Keys}
{
pParent->installEventFilter(此);
}
Q_信号:
无效激活();
私人:
QVector_键;
QVector _按的键;
内联布尔事件过滤器(QObject*pWatched,QEvent*pEvent)覆盖
{
如果(pEvent->type()==QEvent::KeyPress)
{
如果(_PressedKeys.size()<_Keys.size())
{
int PressedKey=((QKeyEvent*)pEvent)->key();
if(_Keys.at(_PressedKeys.size())==PressedKey){
_按按键。追加(按按键);
}
}
如果(_PressedKeys.size()==_Keys.size()){
发射激活();
}
}
else if(pEvent->type()==QEvent::KeyRelease)
{
int ReleasedKey=((QKeyEvent*)pEvent)->key();
int Index=_PressedKeys.indexOf(ReleasedKey);
如果(索引!=-1){
_按按键。删除(索引,_PressedKeys.size()-Index);
}
}
返回QObject::eventFilter(pWatched,pEvent);
}
};
使用方法:
QMultipleKeysShortcut *pMultipleKeysShortcut = new QMultipleKeysShortcut{{Qt::Key_Control, Qt::Key_Shift, Qt::Key_S, Qt::Key_D, Qt::Key_F}, this};
connect(pMultipleKeysShortcut, &QMultipleKeysShortcut::activated, [=] {qDebug() << "Example"; });
qmultiplekeyshortcut*pmmultiplekeyshortcut=newqmultiplekeyshortcut{{{Qt::Key_控件,Qt::Key_Shift,Qt::Key_S,Qt::Key_D,Qt::Key_F},this};
连接(pMultipleKeysShortcut,&QMultipleKeysShortcut::activated,[=]{qDebug()