【问题标题】:Qt Designer Shortcut to another tabQt Designer 到另一个选项卡的快捷方式
【发布时间】:2012-04-26 23:06:31
【问题描述】:

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

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

【问题讨论】:

    标签: qt qt4 qt-designer


    【解决方案1】:

    我不知道通过设计器执行此操作的方法,对此不熟悉。不过,您可以在代码中相当轻松地使用 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)));
          }
    };
    

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-02-15
      • 1970-01-01
      相关资源
      最近更新 更多