QPixmap绘制图形:
#include
#include
#include
class MyWidget : public QWidget
{
public:
MyWidget(QWidget *parent = 0);
protected:
void mouseMoveEvent(QMouseEvent *event);
void paintEvent(QPaintEvent *event);
private:
int x,y;
QPixmap pixmap;
QPixmap background;
QPixmap pixImg;
QBitmap bitImg;
QImage image;
};
// 在构造函数中装入背景图
MyWidget::MyWidget(QWidget *parent)
: QWidget(parent)
{
resize(800,600);
pixmap = QPixmap(100,50);
background = QPixmap("background.jpg");
pixImg = QPixmap("tt.gif");
bitImg = QBitmap("tt.gif");
image = QImage(3, 3, QImage::Format_RGB32);
QRgb value;
value = qRgb(189, 149, 39); // 0xffbd9527
image.setPixel(1, 1, value);
value = qRgb(122, 163, 39); // 0xff7aa327
image.setPixel(0, 1, value);
image.setPixel(1, 0, value);
value = qRgb(237, 187, 51); // 0xffedba31
image.setPixel(2, 1, value);
x = -1;
y = -1;
}
// 鼠标移动时形成一个pixmap
void MyWidget::mouseMoveEvent(QMouseEvent *event)
{
x = event->x();
y = event->y();
pixmap.fill(QColor(0,255,255,127));
QPainter painter(&pixmap);
painter.setPen(QColor(255,0,0,127));
painter.drawText(20, 40, QString("%1").arg(x) + "," +
QString("%1").arg(y));
update();
}
// 绘制背景图和透明的pixmap
void MyWidget::paintEvent(QPaintEvent *event)
{
QPainter painter(this);
painter.drawPixmap(0, 0, background);
painter.drawPixmap(x, y, pixmap);
painter.drawPixmap(20, 10, pixImg);
painter.drawPixmap(20, 60, bitImg);
QRectF rc(50,50, 100, 100);
painter.drawImage( rc, image);
}
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QTextCodec::setCodecForTr(QTextCodec::codecForLocale());
MyWidget widget;
widget.setMouseTracking(true);
widget.show();
QPicture picture;
QPainter painter;
painter.begin(&picture);
painter.drawRect(10, 20, 100, 50);
painter.end();
picture.save("draw_record.pic");
return app.exec();
}
阅读(1477) | 评论(0) | 转发(0) |