2013-03-27 37 views
5

我正在使用Qt Creator爲mineseeper遊戲創建一個gui。 我怎樣才能知道用右鍵單擊的QpushButton?爲遊戲中的國旗。 換句話說,哪個信號用於右擊?Qt rightclick QPushButton

+0

您可能需要重寫'event()'並自己發出信號。 – rainer 2013-03-27 12:15:16

+0

爲什麼有人-1這個問題?是的,可能會有更多的努力,但問題是好的。感謝穆罕默德提供了一個很好的答案。 – zeFree 2013-04-09 22:44:53

回答

16

用篩選器創建您自己的按鈕,位於mousePressEvent插槽。

qrightclickbutton.h

#ifndef QRIGHTCLICKBUTTON_H 
#define QRIGHTCLICKBUTTON_H 

#include <QPushButton> 
#include <QMouseEvent> 

class QRightClickButton : public QPushButton 
{ 
    Q_OBJECT 

public: 
    explicit QRightClickButton(QWidget *parent = 0); 

private slots: 
    void mousePressEvent(QMouseEvent *e); 

signals: 
    void rightClicked(); 

public slots: 

}; 

#endif // QRIGHTCLICKBUTTON_H 

qrightclickbutton.cpp

#include "qrightclickbutton.h" 

QRightClickButton::QRightClickButton(QWidget *parent) : 
    QPushButton(parent) 
{ 
} 

void QRightClickButton::mousePressEvent(QMouseEvent *e) 
{ 
    if(e->button()==Qt::RightButton) 
     emit rightClicked(); 
} 

現在象這樣連接

QRightClickButton *button = new QRightClickButton(this); 
ui->gridLayout->addWidget(button); 
connect(button, SIGNAL(rightClicked()), this, SLOT(onRightClicked())); 

創建MainWindow.cpp插槽。

void MainWindow::onRightClicked() 
{ 
    qDebug() << "User right clicked me"; 
} 

它適合我!

+0

如果有人想連接()到通用clicked()信號和自定義rightClicked()信號 - 有必要在重載的'mousePressEvent(QMouseEvent * e)末尾調用'QPushButton :: mousePressEvent(e) )' – Krzysiek 2016-04-24 15:51:51

5

我認爲QPushButton是內部實現的,只能聽左鍵單擊。但是您可以輕鬆擴展QPushButton並重新實現讓我們說鼠標釋放事件,並在按下鼠標右鍵時執行您的操作。發出定製rightClicked()信號,例如:

signals: 
    void rightClicked(); 

protected: 
    void mouseReleaseEvent(QMouseEvent *e) { 
     if (e->button() == Qt::RightButton) emit rightClicked(); 
     else if (e->button() == Qt::LeftButton) emit clicked(); 
    } 

...或者你可以創建一個轉發的MouseEvent指針,以便你可以做的按鈕之外相同的檢查被點擊的信號過載。

signals: 
    void clicked(QMouseEvent *); 

protected: 
    void mouseReleaseEvent(QMouseEvent *e) { 
     emit clicked(e); 
    } 

然後,你做你的按鈕的clicked(QMouseEvent *)信號連接並進行相應處理槽的檢查。

+0

我正在使用'QPushButton',並且沒有'button()'。我該怎麼辦? – KhoC 2013-03-27 12:40:10

+0

@KhoC什麼? 'button()'是'QMouseEvent'的一個方法。 – cmannett85 2013-03-27 12:58:08

+0

所以我不知道如何使用'mouseReleaseEvent'。爲什麼在'mouseReleaseEvent'中調用'rightClicked()'? – KhoC 2013-03-27 13:01:28

2

我只是寫了這個小幫手適配器作任何現有按鈕右鍵點擊,不需要它的子類:

class CRightClickEnabler : public QObject 
{ 
public: 
    CRightClickEnabler(QAbstractButton * button): QObject(button), _button(button) { 
     button->installEventFilter(this); 
    }; 

protected: 
    inline bool eventFilter(QObject *watched, QEvent *event) override { 
     if (event->type() == QEvent::MouseButtonPress) 
     { 
      auto mouseEvent = (QMouseEvent*)event; 
      if (mouseEvent->button() == Qt::RightButton) 
       _button->click(); 
     } 
     return false; 
    } 

private: 
    QAbstractButton* _button; 
}; 

用法:

connect(ui->pushButton, &QPushButton::clicked, [](){qDebug() << "Button clicked";}); 
new CRightClickEnabler(ui->pushButton); 

從現在起,clicked信號將通過右鍵單擊以及左鍵單擊來觸發。沒有必要刪除這個對象 - 它使用ui->pushButton作爲父對象,並且在父對象被銷燬時將被Qt自動刪除。

很顯然,如果需要,您可以編寫2行代碼(字面上)來聲明新信號,並在右擊時發出該信號,而不是clicked