![](/img/trans.png)
[英]How to draw a triangle by using QGraphicsView's QGraphicsItem class
[英]How to draw image using QGraphicsItem?
我需要使用我的子类QGraphicsItem
绘制一个Image和几行
这是我的代码(头文件) -
#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类需要像...一样运行qapplication ...
main(int argc, char* argv[])
{
QApplication a( argc, argv );
// your gui class here
return a.exec();
}
所以你需要建立一个qt-gui类,例如qgraphicsview来显示它
现在当你有一个显示内容的图形视图时,你需要一个场景来显示并将qgraphicsitem添加到场景中......
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.