4

我想知道是否可以创建自己的 QTabWidget 快捷键。因此,如果我在字母前面加上一个 & 符号,这意味着 ALT+'letter' 将显示该选项卡;但是,我想要它以便 CTRL+'letter' 将显示该选项卡(而不是 ALT)。

在 Qt Designer 中是否有一种简单的方法可以做到这一点?如果没有,是否有一种简单的方法可以在代码中做到这一点?QTabWidget 似乎没有任何设置快捷方式的直接方法。

4

1 回答 1

4

我不知道通过设计器执行此操作的方法,对此不熟悉。QShortcut不过,您可以在代码中相当容易地做到这一点。

这是一个虚拟小部件来说明这一点。按Ctrl+a/Ctrl+b在选项卡之间切换。

#include <QtGui>

class W: public QWidget
{
    Q_OBJECT

    public:
      W(QWidget *parent=0): QWidget(parent)
      {
        // Create a dummy tab widget thing
        QTabWidget *tw = new QTabWidget(this);
        QLabel *l1 = new QLabel("hello");
        QLabel *l2 = new QLabel("world");
        tw->addTab(l1, "one");
        tw->addTab(l2, "two");
        QHBoxLayout *l = new QHBoxLayout;
        l->addWidget(tw);
        setLayout(l);

        // Setup a signal mapper to avoid creating custom slots for each tab
        QSignalMapper *m = new QSignalMapper(this);

        // Setup the shortcut for the first tab
        QShortcut *s1 = new QShortcut(QKeySequence("Ctrl+a"), this);
        connect(s1, SIGNAL(activated()), m, SLOT(map()));
        m->setMapping(s1, 0);

        // Setup the shortcut for the second tab
        QShortcut *s2 = new QShortcut(QKeySequence("Ctrl+b"), this);
        connect(s2, SIGNAL(activated()), m, SLOT(map()));
        m->setMapping(s2, 1);

        // Wire the signal mapper to the tab widget index change slot
        connect(m, SIGNAL(mapped(int)), tw, SLOT(setCurrentIndex(int)));
      }
};

这并不是作为小部件布局最佳实践的示例......只是为了说明将快捷方式序列连接到选项卡更改的一种方法。

于 2012-04-15T07:41:44.333 回答