![](/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.