QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = painterdemo
TEMPLATE = app
SOURCES += main.cpp\
widget.cpp
HEADERS += widget.h
RESOURCES += \
log/log.qrc
资源文件:
<RCC> <qresource prefix="/log"> <file>designer.png</file> <file>fileprint.png</file> <file>find_disabled.png</file> <file>find_normal.png</file> <file>log.qrc</file> <file>monkey_off_16x16.png</file> <file>monkey_off_32x32.png</file> <file>monkey_off_64x64.png</file> <file>monkey_off_128x128.png</file> <file>monkey_on_16x16.png</file> <file>monkey_on_32x32.png</file> <file>monkey_on_64x64.png</file> <file>monkey_on_128x128.png</file> <file>qt_extended_16x16.png</file> <file>qt_extended_32x32.png</file> <file>qt_extended_48x48.png</file> <file>qt4logo.png</file> <file>rotateleft.png</file> <file>rotateright.png</file> <file>zoomin.png</file> <file>zoomout.png</file> </qresource></RCC>
void Widget::paintEvent(QPaintEvent *)
{
QPainter painter(this);
QPixmap pixmap(":/log/qt4logo.png");
QBitmap bitmap(":/log/qt4logo.png");
painter.drawPixmap(10, 10, 250, 125, pixmap);
painter.drawPixmap(270, 10, 250, 125, bitmap);
QPixmap whitePixmap(":/log/monkey_off_16x16.png");
QBitmap whiteBitmap(":/log/monkey_off_16x16.png");
painter.drawPixmap(10, 140, 250, 125, whitePixmap);
painter.drawPixmap(270, 140, 250, 125, whiteBitmap);
}