Qt Designer 另一个选项卡的快捷方式 [英] Qt Designer Shortcut to another tab

查看:108
本文介绍了Qt Designer 另一个选项卡的快捷方式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I was wondering if it were possible to create my own shortcut key to a QTabWidget. So if I put an ampersand infront of the letter, that means that ALT+'letter' will display that tab; however, I want it so that CTRL+'letter' will display that tab (not ALT).

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

Is there an easy way to do this in Qt Designer? If not, is there a simple way to do it in code? QTabWidget doesn't seem to have any direct methods for setting shortcuts.

推荐答案

我不知道通过 Designer 执行此操作的方法,对此我不熟悉.你可以很容易地用 QShortcut 做到这一点虽然在代码中.

I don't know of a way to do this via the Designer, not familiar with that. You could do it with QShortcut fairly easily in code though.

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

Here's a dummy widget to illustrate that. Press Ctrl+a / Ctrl+b to switch between tabs.

#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)));
      }
};

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

这篇关于Qt Designer 另一个选项卡的快捷方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆