2012-02-01 51 views
1

1つのボタンに複数のショートカットを設定する方法が分かっていたのですが、たとえば、ReturnキーとEnterキー(キーボードと数字パッド)にリンクするQPushButtonがあります。setShortcut用の複数のキーボードショートカット

デザイナーに私は、ショートカットフィールドに置く場合:

Return, Enter 

のみ入力応答ではなく、戻ります。

私もちょうどデザイナーに戻るを設定し、自分のソースコードに私が入れて試してみました:

ui.searchButton->setShortcut(tr("Enter")); 

これだけ(テンキー)(キーボード)を返していない入力に応答するようです。

誰かがQPushButtonに複数のショートカットを設定する方法を知っていますか?私はQt4.7を使用しています。

答えて

1

QActionを複数設定してshortcuts on itに設定し、それをQPushButtonに接続することができます。 (同様に、複数のQShortcutオブジェクトを作成してボタンに接続することもできます)。

1

私はQtCreatorで動作しませんので、ここではこの問題に対して2つのコード解決策があります。私はkeyPressEvent(例えば、あなたのメインウィンドウのか、どこにショートカットになりたい)を上書きするような場合のために


1.

ヘッダー:

protected: 
    virtual void keyPressEvent(QKeyEvent* e); 

出典:

void main_window::keyPressEvent(QKeyEvent* e) 
{ 
    switch(e->key()) 
    { 
    case Qt::Key_Enter: 
    case Qt::Key_Return: 
     // do what you want, for example: 
     QMessageBox::information(this, 
      "Success", 
      "Let me guess, you pressed the return key or the enter key."); 
     break; 
    default: 
     ; 
    } 

    QMainWindow::keyPressEvent(e); 
} 

2.
私は複数のQShortcut ojectsを作成して接続することも可能だと思います。 必要なショートカットをすべて作成し、そのショートカットを受け取るオブジェクトのスロットにactivated -Signalを接続してください。

1

qt noobとして、私は1つのボタンに複数のショートカットを追加する方法を探していました。ここの答えは参考になりましたが、実際にすべての作品を一緒にまとめるために少しでも苦労しなければなりませんでした。だから私はここで完全な答えを投稿し、うまくいけば、私の後に来る他のnoobsを助けると思った。

これはPyQtで書かれていることをお詫びしますが、私はそれがアイデアを伝えると信じています。ここで

# Create and setup a "Find Next" button 
find_next_btn = QtGui.QPushButton("  Find &Next") 
# setupButton is a small custom method to streamline setting up many buttons. See below. 
setupButton(find_next_btn, 150, "Icons/arrow_right_cr.png", 30, 20, "RTL") 
find_next_btn.setToolTip("Search DOWN the tree") 
find_next_btn.clicked.connect(find_next) 
# find_next is the method executed when the button is pressed 

# Create an action for the additional shortcuts. Alt+N is already set 
# by "&" in "Find &Next" 
find_next_ret_act = QtGui.QAction(self, triggered=find_next_btn.animateClick) 
find_next_ret_act.setShortcut(QtGui.QKeySequence("Return")) 

find_next_enter_act = QtGui.QAction(self, triggered=find_next_btn.animateClick) 
find_next_enter_act.setShortcut(QtGui.QKeySequence("Enter")) 

# Now add (connect) these actions to the push button 
find_next_btn.addActions([find_next_ret_act, find_next_enter_act]) 


# A method to streamline setting up multiple buttons 
def setupButton(button, btn_w, image=None, icon_w=None, icon_h=None, layout_dir=None): 
    button.setFixedWidth(btn_w) 
    if image != None:    
     icon = QtGui.QIcon() 
     icon.addPixmap(QtGui.QPixmap(image)) 
     button.setIcon(icon) 
    if icon_w != None: 
     button.setIconSize(QtCore.QSize(icon_w, icon_h)) 
    if layout_dir == "RTL": 
     find_next_btn.setLayoutDirection(QtCore.Qt.RightToLeft) 

は、結果のボタンです:http://i.stack.imgur.com/tb5Mh.png(noobのように、私はポストに直接画像を埋め込むことが許されておりません。)

私はこれが便利です願っています。

関連する問題