Qt应用程序中的奇怪错误
Weird bug in Qt application
在我的应用程序中,我重新实现了QGraphicsView
检查mouseReleaseEvent()
,然后在鼠标所在的位置告诉项目来处理事件。
我的视图QGraphicsItem
由另外两个QGraphicsItems
组成,我检查正在单击两者中的哪一个(或者更确切地说是释放按钮),并处理相应的事件。
在我的 Widget 的构造函数中,我将其中一个项目设置为默认选中,使用与项目检测到发布时使用的相同方法。
当我调试时,我发现对于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
,因为它是鼠标下最顶层的项目。除非LabelItem
设置为不接受任何带有 l->setAcceptedMouseButtons(0)
的鼠标按钮。
尝试使用qgraphicsitem_cast
来测试项目的类型。每个派生类都必须重新定义QGraphicsItem::type()
,以便强制转换函数能够标识该类型。
您还可以通过重新定义其QGraphicsItem::mouseReleaseEvent()
方法来处理项目本身中的单击,这将消除对邪恶强制转换的需求,但您必须删除函数LayerView::mouseReleaseEvent()
或至少调用基类实现,QGraphicsView::mouseReleaseEvent()
,以允许项目接收事件。
我看到了这些奇怪的行为: 这主要是二进制不兼容 - c ++方面看起来是正确的,崩溃只是没有意义。如您所说:在您的代码中,"选定"变量不能是原因。您是否更改了声明并忘记重新编译所有链接的对象。只需清理并重新编译所有对象文件即可。在 99% 的情况下为我工作。
- 示例外壳应用程序显示的 V8 "segmentation fault (core dumped)"错误
- C++应用程序 MySQL odbc 数据库连接错误:在引发"otl_tmpl_exception<>"实例后终止调用
- Visual Studio中的wxwidgets应用程序给出错误"LNK2019 unresolved external symbol"
- 调用 java 的回调() 时应用程序崩溃.由于 detatchThread 而获得运行时错误
- 设置我的应用程序 API 感知并防止系统使其模糊和错误定位
- 错误: 无效使用非静态数据成员"应用程序::应用程序构造函数"
- QT C++防止关闭应用程序时出现运行时错误
- JNI 在应用程序中检测到错误:在为 innerclass 调用 NewObject 时使用了无效的 jobject
- 如何使用CLion和MSVC编译器工具链编译wxWidgets Hello World应用程序而没有错误?
- 运行 libpng 应用程序时出现运行时错误
- .exe应用程序在windows10中创建新模块时抛出错误,但在windows7中工作正常
- 由于选项卡顺序的原因,鼠标光标总是得到错误的hwnd-MFC应用程序
- C++ 错误的分配应用程序失败
- Android (ART) 崩溃并显示错误 JNI 在应用程序中检测到错误:jstring 是无效的本地引用
- 使用提升过程编译简单的应用程序会生成错误
- Qt 应用程序中与Qt版本相关的运行时错误
- Windows 8上的.exe文件中的MSVCR90.dll错误 - 应用程序无法正确启动(0xc000007b)
- VS2012错误:应用程序无法正确启动(0xc000007b)
- Visual Studio 2012 错误:应用程序无法正确启动 (0xc0000007b)
- QObject::connect()错误.应用程序崩溃.无构建错误