2012-02-01 5 views
1

하나의 버튼에 대해 여러 개의 단축키를 설정하는 방법을 아는 사람이 있는지 궁금합니다. 예를 들어 QPushButton을 Return 키와 Enter 키 (키보드와 숫자 패드)에 연결하려고합니다. setShortcut을위한 다중 키보드 단축키

디자이너 내가 바로 가기 필드에 넣으면

:

Return, Enter 

만이 아닌 돌아 응답을 입력합니다.

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

이것은 또한 전용 (키보드) 반환하지 (숫자 패드)를 입력에 응답 할 것 같다

은 또한 그냥 넣어 디자이너와 내 소스 코드의 반환을 설정하는 것을 시도했다.

누구나 QPushButton에 하나 이상의 단축키를 설정하는 방법을 알고 있습니까? 참고 Qt4.7을 사용하고 있습니다.

답변

1

약간의 해결 방법이 있지만 QAction을 복수로 설정하여 shortcuts on it을 설정하고 QPushButton에 연결할 수 있습니다. (비슷하게 여러 QShortcut 개체를 만들어 단추에 연결할 수 있습니다.)

1

QtCreator에서 작동하지 않으므로이 문제에 대한 2 가지 코드 솔루션을 제공합니다. 이러한 경우에 대한

1.


나는 덮어 keyPressEvent (예를 들어 메인 창 또는 당신이 원하는 곳 바로 가기 예정).

헤더 :

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으로 하나의 단추에 여러 개의 바로 가기를 추가하는 방법을 찾고있었습니다. 여기에 대한 답이 도움이되었지만, 실제로 모든 것을 함께 모으기 위해 조금 노력해야했습니다. 그래서 나는 여기에 완전한 대답을 게시하여 희망을 갖고 나를 따라 오는 다른 놈들을 도울 것이라고 생각했습니다.

나는 이것이 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 (멍청한 놈, 나는이 게시물에 사진을 직접 삽입 할 수 없습니다입니다.)

난이 도움이되기를 바랍니다.