2011-11-20 98 views
0

當你得到一個模型鼠標信號到你的插槽中,傳遞的參數是一個QModelIndex。QApplication :: mouseButtons的線程安全和延遲安全性如何?

QModelIndex不會告訴你按下哪個按鈕。所以,我們可以求助於QApplication :: mouseButtons。但QApplication :: mouseButtons是當前的按鈕按下,而不是當模型經歷了點擊。

我的思想實驗說,當按下右按鈕後,下面的視圖將信號發送到我的小部件,但我的widget的時隙中接收到信號,就在發生虛假左鍵點擊。因此,在收到QModelIndex時調用QApplication :: mouseButtons會錯誤地將正在點擊的行與鼠標左鍵而不是右鍵關聯起來。這種情況有多可能?

當你看到Qt和甚至QML,它需要大量的代碼雜技實現對收到QModelIndex的正確鼠標按鈕信息。諾基亞是在努力促進鼠標按鈕不可知論的政策嗎?

回答

3

我不認爲這是一個非常可能的方案,但它可能發生。

一個「簡單」的辦法,以確保有關所單擊按鈕是子類QTableView(或者你正在使用的視圖,並重新實現mouseReleaseEvent

void mouseReleaseEvent(QMouseEvent * event) 
{ 
    // store the button that was clicked 
    mButton = event->button(); 
    // Now call the parent's event 
    QTableView::mouseReleaseEvent(event); 
} 

默認情況下,mouseReleaseEvent發出clicked信號如果視圖的項目按

如果用戶按下鼠標小部件內,然後鬆開鼠標按鈕之前,拖動鼠標 到另一個位置,您 小部件接收發布事件。如果正在按下某個項目,該功能將發出 clicked()信號。

訣竅是捕捉clicked信號中派生類和發射一個新的信號,該信號除模型索引將包含按鈕,以及。

// Define your new signal in the header 
signals: 
    void clicked(QModelIndex, Qt::MouseButton); 

// and a slot that will emit it 
private slots: 
    void clickedSlot(QModelIndex); 

// In the constructor of your derived class connect the default clicked with a slot 
connect(this, SIGNAL(clicked(QModelIndex), this, SLOT(clickedSlot(QModelIndex))); 

// Now the slot just emits the new clicked signal with the button that was pressed 
void clickedSlot(QModelIndex i) 
{ 
    emit clicked(i, mButton); 
} 

如果你需要pressed信號,以及你可以做的mousePressEvent類似的東西。

+0

謝謝你 - 這是真正真正的輝煌。我現在可以放棄使用QApplication :: mouseButtons。 –

+0

歡迎... – pnezis