QApplication::mouseButtons是线程安全和延迟安全的
How thread-safe and latency-safe is QApplication::mouseButtons?
当您从模型获取鼠标信号到您的插槽时,传递的参数是QModelIndex。
QModelIndex没有告诉您按下了哪个按钮。因此,我们可以求助于QApplication::mouseButtons。但是QApplication::mouseButtons是当前按下的按钮,而不是模型经历的点击。
我的思想实验是说,在按下右键后,底层视图将信号发送给我的小部件,但就在我的小部件的插槽接收到信号之前,发生了虚假的左键点击。因此,在收到QModelIndex时调用QApplication::mouseButtons会错误地将被单击的行与鼠标左键而不是右键关联起来。这种情况的可能性有多大?
当您查看Qt甚至QML时,在接收到QModelIndex时,需要大量的代码技巧来实现适当的鼠标按钮信息。诺基亚正在努力推动鼠标按钮不可知论,这是一项政策吗?
我不认为这是一个非常可能的场景,但它可能会发生。
一个"简单"的方法来确定哪个按钮被点击是子类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
信号并发出一个新信号,该信号除了模型索引外还将包含按钮。// 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
做类似的事情。
相关文章:
- 从不同线程使用int64的不同字节安全吗
- 将数组作为参数传递给函数安全吗?作为第三方职能部门,可以探索他们想要的之外的其他元素
- 虚拟决赛作为安全
- 获取日期异步信号安全吗?如果在信号处理程序中使用,它会导致死锁吗
- 如何仅为一个函数添加延迟
- 如何将元素添加到数组的线程安全函数?
- C++中的线程安全删除
- 通过网络、跨平台传递std::变体是否安全
- 在std::thread中,joinable()然后join()线程安全吗
- 使用std::istream::peek()总是安全的吗
- 从值小于256的uint16到uint8的Endian安全转换
- 以在Qt中的IF语句中设置时间延迟
- 在c++队列中使用pop和visit实现线程安全
- 在类型和包装器之间reinterpret_cast是否安全<Type>?
- 混合延迟 dll 加载和手动调用 LoadLibraryA 是否安全?
- C++11:对延迟初始化进行安全的双重检查锁定.可能的
- 线程安全、无数据争用、无延迟的共享容器 (circular_buffer)
- 使用静态成员实例的 C++ 中的线程安全单一实例(无延迟实例化)
- QApplication::mouseButtons是线程安全和延迟安全的
- 延迟初始化缓存..我如何使它线程安全