简体   繁体   English

如何在Qt中实现快捷输入框

[英]How to implement shortcut input box in Qt

In software like qtcreator you can see things like this: 在像qtcreator这样的软件中,您可以看到以下内容:

在此处输入图片说明

Basically some box which when clicked ask you to press some keyboard combination in order to record a shortcut. 基本上是一些单击时要求您按下某种键盘组合以记录快捷方式的框。

How can I create something like that in Qt? 如何在Qt中创建类似的内容? I was able so far to implement this only using a regular text box in which user had to type the combination themselves and if it wasn't correct message box appeared, but it would be much more simple if users didn't have to type things like "ctrl + f2" but instead click these keys. 到目前为止,我只能使用常规文本框来实现此目的,在常规文本框中,用户必须自己键入组合,并且如果显示的消息框不正确,则如果用户不必键入内容,它将更加简单。就像“ Ctrl + F2”一样,而是单击这些键。

Is there any Qt widget for this? 是否有任何Qt小部件?

Use QKeySequenceEdit , available since Qt 5.2. 使用QKeySequenceEdit (从Qt 5.2开始可用)。 It allows you to record shortcut key like in Qt Designer. 它允许您像在Qt Designer中一样记录快捷键。

In case you need the widget for Qt 4.x, I have implemented one previously. 如果您需要Qt 4.x的小部件,我以前已经实现了。 Three key parts are: 三个关键部分是:

  1. read user input 读取用户输入
  2. convert it to human readable string 将其转换为人类可读的字符串
  3. create QKeySequence using the string 使用字符串创建QKeySequence

The widget records multiple shortcuts, like in Designer. 小部件记录了多个快捷方式,例如在Designer中。 Shortcuts can be cleared via Delete or Backspace. 快捷键可以通过Delete或Backspace清除。

#define MAX_SHORTCUTS 3

QString ShortcutLineEdit::keyEventToString(QKeyEvent *e)
{
    int keyInt = e->key();
    QString seqStr = QKeySequence(e->key()).toString();

    if (seqStr.isEmpty() ||
        keyInt == Qt::Key_Control ||
        keyInt == Qt::Key_Alt || keyInt == Qt::Key_AltGr ||
        keyInt == Qt::Key_Meta ||
        keyInt == Qt::Key_Shift)
    {
        return "";
    }

    QStringList sequenceStr;
    if (e->modifiers() & Qt::ControlModifier)
        sequenceStr << "Ctrl";
    if (e->modifiers() & Qt::AltModifier)
        sequenceStr << "Alt";
    if (e->modifiers() & Qt::ShiftModifier)
        sequenceStr << "Shift";
    if (e->modifiers() & Qt::MetaModifier)
        sequenceStr << "Meta";

    return sequenceStr.join("+") + (sequenceStr.isEmpty() ? "" : "+") + seqStr;
}


void ShortcutLineEdit::keyPressEvent(QKeyEvent *e)
{
    QString text =text();
    int keyInt = e->key();
    bool modifiers = e->modifiers() & (Qt::ControlModifier | Qt::ShiftModifier | Qt::AltModifier | Qt::MetaModifier);

    if (!modifiers && (keyInt == Qt::Key_Delete || keyInt == Qt::Key_Backspace)) {
        setText("");
        return;
    }

    QString sequenceStr = keyEventToString(e);
    if (sequenceStr == "") {
        QLineEdit::keyPressEvent(e);
        return;
    }

    if (text.split(", ").size() >= MAX_SHORTCUTS)
        text = "";

    if (!text.isEmpty())
        text += ", ";

    setText(text + sequenceStr);
}

void ShortcutLineEdit::apply()
{
    QList<QKeySequence> sequenceList;
    QStringList sequenceStrList = text().split(", ");
    foreach (QString str, sequenceStrList)
        sequenceList << QKeySequence(str);

    // use sequenceList somehow
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM