我有一個自定義類,它繼承自QGraphicsRectItem(基本上QGraphicsItem)。一開始我有屬性以及相應的 getter 和 setter。一切都很好,直到我從場景中獲取專案并嘗試呼叫 setter(或 getter):屬性不再存在。所以我對從場景中檢索到的元素進行了轉換,正如預期的那樣......程式崩潰了(因為元素不再存在......我想)。為了解決這個問題,我使用data了基類提供的屬性(QGraphicsItem) 我認為因為它是一個已經實作的屬性,即使在轉換之后我也不應該有任何問題。然而問題仍然存在。為什么以及如何修復它?我腦子里還有其他解決方案,但我想了解為什么這是一個代表我的問題的簡化代碼
我的專案.h
#ifndef MYITEM_H
#define MYITEM_H
#include <QGraphicsRectItem>
#include <QGraphicsTextItem>
#include <QObject>
#include <QDebug>
class MyItem :public QObject, public QGraphicsRectItem
{
Q_OBJECT
public:
enum {Type = UserType 200};
MyItem();
QString getTestProperty() const;
void setTestProperty(QString p_newValue);
private:
QGraphicsTextItem* test_txt;
};
#endif // MYITEM_H
我的專案.cpp
#include "myitem.h"
MyItem::MyItem(): QGraphicsRectItem(0,0,100,100)
{
setFlag(QGraphicsItem::ItemIsMovable);
test_txt = new QGraphicsTextItem(this);
setData(0,QVariant("hi there"));
}
void MyItem::setTestProperty(QString p_newValue) {
setData(0,p_newValue);
qDebug()<<data(0).toString()<<endl;
test_txt->setPlainText(data(0).toString());
}
QString MyItem::getTestProperty() const {
return data(0).toString();
}
主視窗.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QGraphicsScene>
#include <QGraphicsView>
#include <QtDebug>
#include <myitem.h>
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
private:
QGraphicsScene* m_scene;
QGraphicsView* m_view;
};
#endif // MAINWINDOW_H
主視窗檔案
#include "mainwindow.h"
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
setMinimumSize(800,600);
m_scene = new QGraphicsScene;
m_view = new QGraphicsView;
m_view->setScene(m_scene);
MyItem* item1 = new MyItem();
m_scene->addItem(item1);
setCentralWidget(m_view);
item1->setTestProperty("hello there :)");
MyItem* supposedPointerToItem1 = new MyItem;
supposedPointerToItem1 = qgraphicsitem_cast<MyItem*> (m_scene->items().first());
supposedPointerToItem1->setTestProperty("Another test that may fail");//Failed
}
MainWindow::~MainWindow()
{
}
最后這是我得到的結果的截圖
The program ended suddenly

uj5u.com熱心網友回復:
能失敗的都會失敗。
所以最好檢查一下。QGraphicsScene::items() 回傳所有專案,因此它也將回傳 QGraphicsTextItem,這就是您的情況:第一個專案不是 MyItem 而是 QGraphicsTextItem。
另一方面,您不必要地創建了第二個 MyItem,我也不認為需要使用 Q_OBJECT 或從 QObject 繼承。
#ifndef MYITEM_H
#define MYITEM_H
#include <QGraphicsRectItem>
class QGraphicsTextItem;
class MyItem :public QGraphicsRectItem
{
public:
enum {Type = QGraphicsItem::UserType 200};
MyItem(QGraphicsItem *parent = nullptr);
QString getTestProperty() const;
void setTestProperty(const QString &p_newValue);
int type() const override;
private:
QGraphicsTextItem* test_txt;
};
#endif // MYITEM_H
#include "myitem.h"
#include <QDebug>
MyItem::MyItem(QGraphicsItem *parent): QGraphicsRectItem(0,0,100,100, parent)
{
setFlag(QGraphicsTextItem::ItemIsMovable);
test_txt = new QGraphicsTextItem(this);
setData(0,QVariant("hi there"));
}
void MyItem::setTestProperty(const QString & p_newValue) {
setData(0,p_newValue);
qDebug()<<data(0).toString();;
test_txt->setPlainText(data(0).toString());
}
QString MyItem::getTestProperty() const {
return data(0).toString();
}
int MyItem::type() const
{
return Type;
}
#include "mainwindow.h"
#include "myitem.h"
#include <QGraphicsScene>
#include <QGraphicsView>
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent)
{
setMinimumSize(800,600);
m_scene = new QGraphicsScene;
m_view = new QGraphicsView;
m_view->setScene(m_scene);
MyItem* item1 = new MyItem();
m_scene->addItem(item1);
setCentralWidget(m_view);
item1->setTestProperty("hello there :)");
QList<QGraphicsItem*> items = m_scene->items();
for(QGraphicsItem *item : qAsConst(items)){
if(MyItem* myitem = qgraphicsitem_cast<MyItem *>(item)){
myitem->setTestProperty("Another test that may fail");
}
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/313889.html
標籤:qt 碰撞 qgraphicsitem
下一篇:使用兩個定時器Qt
