读书人

练习QPainter运行时看不到图形只有

发布时间: 2012-03-09 16:54:56 作者: rapoo

练习QPainter,运行时看不到图形,只有一个灰色窗口

C/C++ code
#include<QApplication>#include<QPaintDevice>#include<QPainter>#include<QPixmap>#include<QWidget>#include<QtGui>#include<QWidget>int main(int argc,char *argv[]){    QApplication app(argc,argv);    //QWidget *wid=new QWidget();    //QPixmap *pm=new QPixmap(200,100);    QWidget *pm=new QWidget();    QPainter painter(pm);    painter.setRenderHint(QPainter::Antialiasing);    painter.setPen(QPen(Qt::black,12,Qt::DashDotLine,Qt::RoundCap));    painter.setBrush(Qt::green);    painter.drawEllipse(80,80,400,240);    pm->show();    return app.exec();}


[解决办法]
QPainter::begin: Paint device returned engine == 0, type: 1
QPainter::setRenderHint: Painter must be active to set rendering hints
QPainter::setPen: Painter not active
QPainter::setBrush: Painter not active
上面就是你的程序运行是的提示。
想要使用QPainter画图,就要继承QWidget,重写painterEvent虚函数,在里面画图。
Qt不像MFC在什么时间都可以画图,只有在painterEvent里面可以。
[解决办法]
楼上正解,如果楼主要在其他地方绘制图形,可以考虑绘制到QImage或者QPixmap上面,然后在
painterEvent里面调用QPainter的绘制图像函数。

读书人网 >QT开发

热点推荐