![](/img/trans.png)
[英]Weird behaviour when compiling CppUTest tests for application using Qt and Boost
[英]Weird bug in Qt application
在我的应用程序中,我重新实现了QGraphicsView
检查mouseReleaseEvent()
,然后在鼠标所处的位置告诉该项目以处理事件。
我的视图的QGraphicsItem
由其他两个QGraphicsItems
,我检查是否正在单击两个(或释放按钮)这两个之一,并处理相应的事件。
在我的小部件的构造函数中,我使用与项目检测到发布时相同的方法,将其中一项设置为默认选中。
当我调试时,我发现对于LabelItem
,select不会从构造函数中被调用(并且当我第一次启动该应用程序时,结果是清楚的)。 但是,当我单击项目时,应用程序终止。 我看到我正在进入选择功能,但没有离开它。 所以问题就在这里。
这很奇怪,因为选择功能只是单行设置器。
void LabelItem::select()
{
selected = true;
}
这是mouseReleaseEvent
;
void LayerView::mouseReleaseEvent(QMouseEvent *event)
{
LayerItem *l;
if(event->button() == Qt::LeftButton)
{
l = (LayerItem *) itemAt(event->pos());
if(l->inLabel(event->pos()))
{ //No problem upto this point, if label is clicked on
l->setSelection(true); //in setSelection, I call select() or unselect() of LabelItem,
//which is a child of LayerItem, and the problem is there.
//In the constructor for my main widget, I use setSelection
//for the bottom most LayerItem, and have no issues.
emit selected(l->getId());
}
else if(l->inCheckBox(event->pos()))
{
bool t = l->toggleCheckState();
emit toggled(l->getId(), t);
}
}
}
当我在函数中注释掉该行时,没有任何错误。 我尚未调试其他QGraphicsItem
CheckBoxItem,但该应用程序也因其事件而终止。 我认为问题可能是相关的,因此我现在专注于select
。
我完全不知道是什么原因引起的,以及为什么发生这种情况。 从我过去的经验来看,我很确定这很简单,但我愚蠢地没想到,但是我不知道要做什么。
帮助将不胜感激。
如果LabelItem
是在顶部LayerItem
, itemAt
将最有可能返回LabelItem
,因为它是鼠标下的最上面的项目。 除非使用l->setAcceptedMouseButtons(0)
将LabelItem
设置为不接受任何鼠标按钮。
尝试使用qgraphicsitem_cast
测试项目的类型。 每个派生类都必须重新定义QGraphicsItem::type()
才能为强制转换函数返回不同的值,以便能够识别类型。
您还可以通过重新定义它们的QGraphicsItem::mouseReleaseEvent()
方法来处理项目本身中的点击,这将消除对恶意对象的需要,但是您必须删除函数LayerView::mouseReleaseEvent()
或至少调用基本函数类实现QGraphicsView::mouseReleaseEvent()
,以允许项目接收事件。
我已经看到了这些奇怪的行为:主要是二进制不兼容-c ++方面看起来是正确的,并且崩溃根本没有意义。 如您所述:在您的代码中,“ selected”变量不能成为原因。 您是否更改了声明并忘记了重新编译所有链接的对象。 只需清理并重新编译所有目标文件即可。 在99%的案件中为我工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.