Question

here's my code:

#include<QApplication>
#include<QWidget>
#include<QPaintEvent>
#include<QPainter>

int main(int argc,char**argv)
{
QApplication app(argc,argv);
QWidget*wid=new QWidget;
wid->setWindowTitle("sb");

QPainter paint;
paint.setWindow(0,0,900,900);
QRectF border(3*45-20,25,670,850);
QRectF inter(3*45,45,630,810);
paint.setPen(Qt::NoPen);
paint.setBrush(QBrush(Qt::darkMagenta,Qt::SolidPattern));
paint.drawRect(border);
paint.setBrush(QBrush(Qt::gray,Qt::SolidPattern));
paint.drawRect(inter);//
paint.setPen(QPen(Qt::darkGray,3,Qt::SolidLine,Qt::RoundCap,Qt::RoundJoin));
paint.setPen(Qt::NoPen);

paint.beginNativePainting();
wid->show();
return app.exec();
}

after qmake -project ,qmake ,make ,then it goes wrong as follows:

kl@kl-Latitude-D630:~/QTproj/t1$ ./t1 
QPainter::setWindow: Painter not active
QPainter::setPen: Painter not active
QPainter::setBrush: Painter not active
QPainter::drawRects: Painter not active
QPainter::setBrush: Painter not active
QPainter::drawRects: Painter not active
QPainter::setPen: Painter not active
QPainter::setPen: Painter not active
QPainter::beginNativePainting: Painter not active

Well ~~ How to active the QPainter only in one cpp file(I don't wanna use separate files such as paint.h ,paint.cpp and main.cpp . I know I can make it work if I create a class inherit the QWidget, but I wanna have a try how to make it work in one file)?? thanks a lot ~;-)

Was it helpful?

Solution

I think, error is here:

QPainter paint;
paint.setWindow(0,0,900,900);

Errors say, that you just don't activated Painter before painting, like trying to read a file before opening it. You initialized painter, but didn't tell him where to paint on. Check a bit of documentation - http://harmattan-dev.nokia.com/docs/library/html/qt4/qpainter.html#QPainter

just add QPainter::begin ( QPaintDevice * device ) after QPainter paint; or change that line to QPainter::QPainter ( QPaintDevice * device )

for example - QPainter p(img); Where img is QImage* in this case. Good luck

OTHER TIPS

You can use QPainter only in QWidget's paintEvent function. So write a new class that inherits from QWidget and implement it's paintEvent function.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top