问题描述
我使用 QGraphicsScene
小部件并显示一些点 QGraphicsRectItem
。
I'm using a QGraphicsScene
widget and showing upon it some points as QGraphicsRectItem
.
这意味着在显示时调用大量的 new
+ addItem()
和 removeItem()
+ 删除
以清除未使用的点。
This means calling lots of new
+ addItem()
when showing up, and removeItem()
+ delete
to get rid of unused points.
当然,对于性能问题,我实现了我自己的新
和 delete
Of course, for performance issues, I've implemented my own new
and delete
operators, which basically recycle pre-allocated memory chunks.
除了 Qt 类之外,这些操作符对任何东西都很有效(我的意思是 QObject 派生类)。
Those operators works very well with anything, except for Qt classes (I mean QObject derived classes). The error raised is different every time but always occurs during an internal call after all my slot functions have been executed with no errors.
我也尝试过一些实例的 QWidget
添加到某些布局等。
I also tried them with some instances of QWidget
added to some layouts etc.
问题是, Qt 它自己的 new
并删除运算符
,我完全跳过定义我自己的
So the question is, does Qt allready have its own new
and delete operators
, which I am totally skipping defining my own ones?
推荐答案
检查Qt的头部来确定这样的自定义运算符是否存在是微不足道的。他们不会。
It's trivial enough to inspect Qt's headers to ascertain whether such custom operators exist. They don't.
QGraphicsRectItem
不会从 QObject
,所以这不是一个问题。因为Qt广泛地使用PIMPL语言,并且 QGraphicsItem
在堆上分配了它的PIMPL,所以你没有从中获得那么多的节省。
The QGraphicsRectItem
does not derive from QObject
, so that's not an issue anyway. You're not getting as much savings from that as you think you are, since Qt uses the PIMPL idiom extensively, and QGraphicsItem
is allocating its PIMPL on the heap.
您还需要展示您的运算符的实现 - 我知道,你不能保持正确的对齐。您要定位哪些编译器?什么版本的Qt?
You'd also need to show the implementation of your operator - for all I know, you're not maintaining proper alignment. What compiler(s) are you targeting? What version of Qt?
要做你想做的,你必须修改Qt。声明并实现:
To do what you intend, you must modify Qt. Declare and implement:
void* QGraphicsItem::operator new (size_t sz)
void* QGraphicsItem::operator new[] (std::size_t count)
void QGraphicsItem::operator delete (void* ptr, std::size_t sz)
void QGraphicsItem::operator delete[] (void* ptr, std::size_t sz)
void* QGraphicsItemPrivate::operator new (size_t sz)
void QGraphicsItemPrivate::operator delete (void* ptr, std::size_t sz)
确保您的分配器正确对齐存储。
Make sure that your allocator properly aligns the storage.
这篇关于Qt是否已经有自己的新的和删除操作符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!