我已经看到同一个问题的许多答案,我已经完成了它们,但没有一个解决了我的问题,我收到了错误
QWidget::paintEngine: 不应再被称为 QPainter::begin: 绘图设备返回引擎 == 0,类型:1 QPainter::end: Painter 未激活,已中止
我需要知道,什么是类型:1,以及为什么显示此错误,
我的代码是
图标小部件.h
class IconWigdet : public QAbstractButton
{
Q_OBJECT
QRect *iconarea;
QPainter p;
QPixmap *icon;
public:
explicit IconWigdet(QRect *rectangle,QPixmap *tempicon);
void paintEvent(QPaintEvent *);
};
图标小部件.cpp
IconWigdet::IconWigdet(QRect *rectangle,QPixmap *tempicon)
{
iconarea = new QRect();
*iconarea = *rectangle ;
icon = new QPixmap(*tempicon);
this->setGeometry(0,0,iconarea->width(),iconarea->height()+20);
}
void IconWigdet::paintEvent(QPaintEvent *)
{
qDebug() << " PaintEvent ";
p.begin(this);
p.drawText(iconarea->x()+ 10,iconarea->height()+10, "name");
p.drawPixmap ( *iconarea,*icon );
p.end();
}
groupwidget.h
class GroupWidget: public QWidget
{
Q_OBJECT
QGridLayout *groupLayout = new QGridLayout ;
QRect *rect = new QRect( 0, 0, 100, 100);
QPixmap *pimap = new QPixmap("../widgeticon/icons/ball.png");
IconWigdet *icon = new IconWigdet(rect,pimap);
public:
GroupWidget();
};
groupwidget.cpp
GroupWidget::GroupWidget()
{
groupLayout->addWidget(icon, 0, 1, 1, 1, 0);
this->setLayout(groupLayout);
icon->show();
QPaintEvent *e;
icon->paintEvent(e);
}
主文件
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
GroupWidget *Gw = new GroupWidget;
Gw->show();
return a.exec();
}
如果主函数更改为,iconwidget 类将完美运行
int main(int argc, char *argv[])
{
QApplication a(argc, argv);;
QRect *rect = new QRect( 0, 0, 100, 100);
QPixmap *pimap = new QPixmap("../widgeticon/icons/ball.png");
IconWigdet *Iw = new IconWigdet(rect,pimap);
Iw->show();
return a.exec();
}
这意味着,如果我们在 main 函数中使用 iconwidget 类对象,它正在工作,但是当我们在 groupwidget 类中执行相同操作时,它就不起作用了,
提前致谢