为了配置我的应用程序,我需要这样做。我有一个重新实现了keyPressEvent方法的QLineEdit字段。
QKeyEvent *ke = ...
QString txt;
if(ke->modifiers() & Qt::ControlModifier)
txt += "Ctrl+";
if(ke->modifiers() & Qt::AltModifier)
txt += "Alt+";
if(ke->modifiers() & Qt::ShiftModifier)
txt += "Shift+";
if(ke->key() >= Qt::Key_0 && ke->key() <= Qt::Key_9)
txt += ('0' + ke->key() - Qt::Key_0);
else if(ke->key() >= Qt::Key_A && ke->key() <= Qt::Key_Z)
txt += ('A' + ke->key() - Qt::Key_A);
ui->hotkeyEdit->setText(txt);但此解决方案只能使用英文字符创建快捷方式。例如,当我使用俄语键盘布局时,此代码将显示相同的序列,但带有英文字符,放置在相同的键盘键上。
发布于 2011-07-12 21:34:38
您可能会发现非常有用的是函数QKeySequence().toString()。
下面是我用来捕获用户定义快捷键的代码的一部分。通过一些修改,它可以在你的项目中做任何你需要的事情。希望它能有所帮助(很抱歉我的标识很糟糕):
if (event->type() == QEvent::KeyPress){
QKeyEvent *keyEvent = static_cast<QKeyEvent*>(event);
int keyInt = keyEvent->key();
Qt::Key key = static_cast<Qt::Key>(keyInt);
if(key == Qt::Key_unknown){
qDebug() << "Unknown key from a macro probably";
return;
}
// the user have clicked just and only the special keys Ctrl, Shift, Alt, Meta.
if(key == Qt::Key_Control ||
key == Qt::Key_Shift ||
key == Qt::Key_Alt ||
key == Qt::Key_Meta)
{
qDebug() << "Single click of special key: Ctrl, Shift, Alt or Meta";
qDebug() << "New KeySequence:" << QKeySequence(keyInt).toString(QKeySequence::NativeText);
return;
}
// check for a combination of user clicks
Qt::KeyboardModifiers modifiers = keyEvent->modifiers();
QString keyText = keyEvent->text();
// if the keyText is empty than it's a special key like F1, F5, ...
qDebug() << "Pressed Key:" << keyText;
QList<Qt::Key> modifiersList;
if(modifiers & Qt::ShiftModifier)
keyInt += Qt::SHIFT;
if(modifiers & Qt::ControlModifier)
keyInt += Qt::CTRL;
if(modifiers & Qt::AltModifier)
keyInt += Qt::ALT;
if(modifiers & Qt::MetaModifier)
keyInt += Qt::META;
qDebug() << "New KeySequence:" << QKeySequence(keyInt).toString(QKeySequence::NativeText);
}发布于 2014-05-29 02:26:44
如果有人关心上面Tihomir Dolapchiev的解决方案的python版本。我刚刚翻译了它,它工作得很好。只是为了将来的参考而发布。
class KeySequenceEdit(QtGui.QLineEdit):
"""
This class is mainly inspired by
http://stackoverflow.com/a/6665017
"""
def __init__(self, keySequence, *args):
super(KeySequenceEdit, self).__init__(*args)
self.keySequence = keySequence
self.setKeySequence(keySequence)
def setKeySequence(self, keySequence):
self.keySequence = keySequence
self.setText(self.keySequence.toString(QtGui.QKeySequence.NativeText))
def keyPressEvent(self, e):
if e.type() == QtCore.QEvent.KeyPress:
key = e.key()
if key == QtCore.Qt.Key_unknown:
warnings.warn("Unknown key from a macro probably")
return
# the user have clicked just and only the special keys Ctrl, Shift, Alt, Meta.
if(key == QtCore.Qt.Key_Control or
key == QtCore.Qt.Key_Shift or
key == QtCore.Qt.Key_Alt or
key == QtCore.Qt.Key_Meta):
print("Single click of special key: Ctrl, Shift, Alt or Meta")
print("New KeySequence:", QtGui.QKeySequence(key).toString(QtGui.QKeySequence.NativeText))
return
# check for a combination of user clicks
modifiers = e.modifiers()
keyText = e.text()
# if the keyText is empty than it's a special key like F1, F5, ...
print("Pressed Key:", keyText)
if modifiers & QtCore.Qt.ShiftModifier:
key += QtCore.Qt.SHIFT
if modifiers & QtCore.Qt.ControlModifier:
key += QtCore.Qt.CTRL
if modifiers & QtCore.Qt.AltModifier:
key += QtCore.Qt.ALT
if modifiers & QtCore.Qt.MetaModifier:
key += QtCore.Qt.META
print("New KeySequence:", QtGui.QKeySequence(key).toString(QtGui.QKeySequence.NativeText))
self.setKeySequence(QtGui.QKeySequence(key))发布于 2011-07-11 22:01:51
您可以为操作指定键盘快捷键。
这里有一些方法可以做到。
actionName->setShortcut(QKeySequence::New); //for predefined shortcuts like new, close, open..或者您可以使用下面的代码定义您自己的快捷方式
actionName->setShortcut(QKeySequence("Ctrl+N")); // sets Ctrl + N for w.e the action does在第一种情况下,qt会自动检测并为该特定操作分配适当的快捷键。在第二种情况下,您可以选择自己想要的快捷方式,并将其键入为字符串。它会自动解析并理解它。
这就避免了将键捕获用于不必要的目的的需要。
https://stackoverflow.com/questions/6647970
复制相似问题