如何使用QGraphicsItem绘制图像?
How to draw image using QGraphicsItem?
我需要使用我的子类化QGraphicsItem
绘制一个图像和几条线
这是我的代码(头文件)-
#ifndef LED_H
#define LED_H
#include <QtGui>
#include <QGraphicsItem>
class LED : public QGraphicsItem
{
public:
explicit LED();
QRectF boundingRect() const;
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option,
QWidget *widget);
private:
static QPixmap *led_on; //<--Problem
};
#endif // LED_H
注意 -LED
将被添加到QGraphicsScene
现在我不知道如何处理它(使用 QGraphicsItem 绘制图像),但决定使用一个static QPixmap
,它应该由LED
类的所有实例共享。
并在 cpp 文件中添加了这个>
QPixmap* LED::led_on = new QPixmap(":/path");
但是我在构建和运行时收到此错误-
QPixmap: Cannot create a QPixmap when no GUI is being used
QPixmap: Must construct a QApplication before a QPaintDevice
The program has unexpectedly finished.
请告诉我该怎么做。(我是Qt的新手) 我应该改用QImage
还是其他东西?
正如错误所暗示的那样,您必须在创建 QApplication 后创建 QPixmap。 显然,你的所作所为会导致相反的情况发生。这个问题有很多解决方案,但这个解决方案很干净: 创建初始化 QPixmap 的 LED 类的静态成员:
void LED::initializePixmap() // static
{
led_on = new QPixmap(":/path");
}
现在像这样设计你的主函数:
int main(int argc, char *argv[])
{
QApplication a(argc, argv); // first construct the QApplication
LED::initializePixmap(); // now it is safe to initialize the QPixmap
MainWindow w; // or whatever you're using...
w.show();
return a.exec();
}
这是一个问题,因为 qt 的 gui 类需要一个运行 qapp 就像在...
main(int argc, char* argv[])
{
QApplication a( argc, argv );
// your gui class here
return a.exec();
}
所以你需要构建一个Qt-GUI类,例如一个qGraphicsView来显示它
现在,当您有一个图形视图来显示内容时,您需要一个场景来显示并将 QGraphicsItem 添加到场景中......
相关文章:
- 如何在 direct2d 中使用随其窗口重新缩放的效果绘制图像
- 获取位于安卓工作室中可绘制的图像的绝对路径,用于NDK项目
- Opencv,在图像中的对象周围绘制一个框
- 为什么在开始绘制获取的交换链图像时不需要内存障碍?
- OPENCV C .在for循环中在不同的图像像素上绘制一个圆圈(应在每个循环运行时将图像打开)
- 如何在圆圈内绘制图像
- 在绘制信号上保留绘图区域"图像"
- 如何使用GLADE和GTKMM C 重新绘制GTK ::图像组件
- 过剩无法绘制 2D 图像
- 尝试递归地重新绘制所有图像像素
- 从C 中的文件中读取图像,然后传递到Libharu的绘制方法
- SDL 不绘制图像
- 如何使用QGraphicsItem绘制图像?
- 绘制太大的图像无法适应Qimage
- 图像绘制使用NDK显示轮廓
- 打开不同的图像文件,并使用GDI 库在主应用程序窗口的背景上绘制它们
- 在灰度 (B/W) 图像上绘制彩色点
- 使用图像指针绘制图像
- QPixmap将绘制的正方形的内容复制到另一个图像中
- OpenCV将透明图像绘制到另一个图像上