2008年6月18日 星期三

QPainter & QPixmap

QPainter用來繪圖 而QPixmap可以得到jpg圖檔
以下為範例程式碼
需注意繪圖部分必須寫在 paintEvent 裏面
-----------------------------------------------------------------

#include "qapplication.h"
#include "qwidget.h"
#include "qpainter.h"
#include "qpixmap.h"
class mywindow:public QWidget
{
public:
mywindow();
private:
void paintEvent(QPaintEvent* event);
QPainter *paint;
};

void mywindow::paintEvent(QPaintEvent* event){
QPixmap pm("bg1.jpg");
paint= new QPainter;
paint->begin(this);
paint->drawPixmap(0,0,pm);
paint->end();
}

mywindow::mywindow()
{ setGeometry(0,0,600,600); // (initial location in window x,y),(width, height)

}
int main(int argc, char *argv[])
{
QApplication app(argc, argv);

mywindow window;
window.show();

return app.exec();
}

沒有留言: