【问题标题】:Paint device returned engine == 0, type: 1绘制设备返回引擎 == 0,类型:1
【发布时间】:2014-12-09 12:08:43
【问题描述】:

我已经看到了同一个问题的很多答案,我已经完成了它们,但没有一个能解决我的问题,我收到了错误

QWidget::paintEngine: 不应再调用 QPainter::begin: 绘制设备返回引擎 == 0,类型:1 QPainter::end: Painter 未激活,已中止

我需要知道,什么是 type : 1,以及为什么会出现这个错误,

我的代码是

iconwidget.h

class IconWigdet : public QAbstractButton
{
Q_OBJECT
QRect *iconarea;
QPainter p;
QPixmap *icon; 
public:
explicit IconWigdet(QRect *rectangle,QPixmap *tempicon);
void paintEvent(QPaintEvent *);  
};

iconwidget.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);
}

main.cpp

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 类中执行相同操作时,它就不起作用了,

提前致谢

【问题讨论】:

  • 请不要做出使您收到的答案无效的更改。我已为您回滚,以便接受的答案再次有意义。

标签: c++ qt qwidget qpainter qabstractbutton


【解决方案1】:

您直接致电IconWigdet::paintEvent。这是不允许的。不要直接调用它,而是调用QWidget::updateQWidget::repaint

GroupWidget::GroupWidget()
{ 
    groupLayout->addWidget(icon, 0, 1, 1, 1, 0);
    this->setLayout(groupLayout);
    icon->show();
    // QPaintEvent *e;
    // icon->paintEvent(e); this is not allowed
    icon->update(); // do this instead
}

虽然我不明白你为什么要在那里打电话。只需致电Icon-&gt;show(); 就足够了。 Qt 会自动安排一个绘制事件。

【讨论】:

  • 它不工作..当我在主函数中简单地使用 Icon->show 时它正在工作(在 Qn 中提到了 main 函数)。当我在 groupwidget 类中调用它时它不起作用,实际上绘图事件没有自动调用或者从 groupwidget 调用时显示错误
  • @AkhilVSuku 你在groupwidget.h 中做了一些奇怪的事情。这是你的实际头文件吗?
  • @AkhilVSuku 您在不删除的地方也到处都有不必要的指针,因此您有一堆内存泄漏。喜欢QRect *iconareaQPixmap *icon。没有理由将它们作为指针。仅在有充分理由的情况下才使用指针,不要将它们放在任何地方。您的 IconWidget 构造函数也是如此,您可以将 const 引用传递给它:IconWigdet(const QRect &amp;rectangle, const QPixmap &amp;tempicon);
  • @AkhilVSuku 如果为IconWigdet 设置最小大小会怎样?只需将setMinimumSize(100,100); 放入构造函数中即可。
  • @AkhilVSuku 您将IconWidget 添加到将控制小部件几何形状的布局中。 setGeometry 对布局内的小部件没有任何作用。在您的情况下,它似乎缩小到0,0 的大小。但是,布局仍将遵循小部件的最小和最大尺寸。当您在 main 函数中创建 IconWidget 时,它不在布局中,因此 setGeometry 有效。
【解决方案2】:

试试这个而不是你的构造函数

GroupWidget::GroupWidget()
{ 
    groupLayout->addWidget(icon, 0, 1, 1, 1, 0);
    this->setLayout(groupLayout);
    setMinimumSize(100,100); 
    setMaximumSize(200,200); 
    icon->show();
}

这会起作用的,

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-04-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多