2012-04-15 20 views
4

我想知道是否有可能創建我自己的快捷鍵到QTabWidget。所以如果我在字母的前面加一個&符號,這意味着ALT +'字母'將顯示該標籤;不過,我希望它能夠讓CTRL +'letter'顯示該選項卡(而不是ALT)。Qt設計器快捷鍵到另一個標籤

在Qt Designer中有這麼簡單的方法嗎?如果沒有,是否有一種簡單的方法可以在代碼中完成? QTabWidget似乎沒有任何設置快捷方式的直接方法。

回答

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

這並不意味着作爲小部件佈局的最佳實踐的例子......只是爲了說明連接一個快捷鍵序列標籤變化的一種方式。

相關問題