QT c++绘制点 qpainter qgraphicscene
大家好,我是 qt 新手,在绘制单个点时遇到困难。
我有一个大的qMainWindow,它最终调用QgraphicsScene,在它的内部我需要绘制一个点,一个小像素,这就是我想要的,我想使用QPainter,但我在实例化它时遇到了麻烦。有什么想法吗?
Hell-o all, Im new to qt and I am having trouble drawing one single point.
I have a big qMainWindow that eventually calls a QgraphicsScene and inside of it I need to draw a single point, one little pixel, that is all I want, I want to use a QPainter but Im having trouble instantiating one. Any ideas??
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您想要做的示例代码在这里会有很大帮助。
QPainter使用:
构造指向画布对象的 QPainter 对象,然后调用
painter_obj.drawPoint(x,y);
。请注意,QPainter 需要在堆栈上创建,而不是堆上,以便对象的析构函数可以启动实际绘图。文档中的示例:
QGraphicsScene 使用:
通常,您使用 QGraphicsScene 来同时管理在视图周围浮动的大量对象。对于一个简单的绘图小部件来说,这太过分了。 iirc,QGraphicsScene 不是供 QPainter 绘画的有效画布。
相反,您创建适当类型的 QGraphicsItem(或子类),并重写 Paint 方法。当您的 QGrpahicsItem 添加到场景中时,库将向您传递一个 QPainter 对象,用于在适当的时候处理您的绘图。
Sample code of what you're trying to do would help alot here.
QPainter use:
Construct your QPainter object pointing at a canvas object, and then call
painter_obj.drawPoint(x,y);
. Note that the QPainter needs to be created on the stack, not the heap, so that the destructor of the object can kick off the actual drawing.The example from the docs:
QGraphicsScene use:
Usually, you use a QGraphicsScene to manage a large number of objects floating around a view at the same time. This is overkill for a simple drawing widget. QGraphicsScene is not, iirc, a valid canvas for a QPainter to paint on.
Instead, you create a QGraphicsItem (or subclass) of the appropriate type, and override the paint method. When your QGrpahicsItem is added to the Scene, the library will pass you a QPainter object to use to handle your drawing when appropriate.