2013-03-25 2 views
0

여기 내 코드입니다 : 다음과 같이 qmake를 -project 후하나의 cpp 파일에서만 QPainter를 어떻게 사용합니까?

#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(); 
} 

, qmake를 만들, 그것은 잘못 : 음

[email protected]:~/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 

~~ 방법합니다 (QPainter가 하나 개의 CPP 파일에 활성 내가 paint.h, paint.cpp 및 main.cpp 같은 별도의 파일을 사용하고 싶지 않아요. 내가 QWidget을 상속 클래스를 만드는 경우 작동하도록 만들 수 있지만, 그것을 작동하게하는 방법을 시도하고 싶습니다. 하나의 파일) ?? 덕분에 많은 ~ ;-)

+0

QWidget에 페인트를 적용하려면 서브 클래스를 만들어서 paintEvent를 재정의해야합니다. –

답변

4

내가 생각하는, 오류가 여기에 있습니다 :

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

오류 말, 당신은 단지을 열기 전에 파일을 읽으려고처럼, 그림 그리기 전에 화가를 활성화하지 않습니다. 화가를 초기화했지만 페인트 할 위치를 알려주지 않았습니다. 문서의 비트 확인 - http://harmattan-dev.nokia.com/docs/library/html/qt4/qpainter.html#QPainter

단지 예를 들어 QPainter::QPainter (QPaintDevice * device)

에 그 라인을 QPainter paint;QPainter::begin (QPaintDevice * device)를 추가하거나 변경 - QPainter p(img);을 IMG이 경우 QImage *입니다. 행운을 빌어 요

+1

이 더 정확하려면 화가 개체가 * 뭔가 위에 페인트 *됩니다. 그는 페인트 표면을 세우지 않고 화가를 조작하기 시작합니다. – UmNyobe

0

QPainterQWidgetpaintEvent 기능에서만 사용할 수 있습니다. QWidget에서 상속받은 새로운 클래스를 작성하고 paintEvent 함수를 구현하십시오.

+1

완전히 잘못되었으므로 QPainter를 사용할 수 있습니다. QPaintDevice는 http://harmattan-dev.nokia.com/docs/library/html/qt4/qpaintdevice.html#details입니다. – Shf

관련 문제