2011-11-20 52 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