码迷,mamicode.com
首页 > 其他好文 > 详细

Display Images in widget

时间:2016-04-01 22:09:52      阅读:220      评论:0      收藏:0      [点我收藏+]

标签:

在自定义的widget中显示图片。

思路:定义类MyWidget,public 继承自QWidget,然后实现 void paintEvent(QPaintEvent *)。

新建Empty qmake project,命名为DisplayImages,添加三个文件 mywidget.h  mywidget.cpp  main.cpp

mywidget.h 

技术分享
#ifndef MYWIDGET_H
#define MYWIDGET_H

#include <QWidget>
class MyWidget : public QWidget
{
public:
    MyWidget(QWidget *parent = 0);

protected:
    void paintEvent(QPaintEvent *event);
};

#endif // MYWIDGET_H
View Code

mywidget.cpp

技术分享
#include "mywidget.h"
#include <QPainter>
#include <QImage>

MyWidget::MyWidget(QWidget *parent)
    : QWidget(parent)
{
    setWindowTitle("Display Images");
}

void MyWidget::paintEvent(QPaintEvent *event)
{
    Q_UNUSED(event);
    QPainter painter(this);
    QImage image("../testQPainter2/dot.png");
    painter.drawImage(100, 100, image);
    painter.drawImage(200, 200, image);
}
View Code

main.cpp

技术分享
#include <QApplication>
#include "mywidget.h"

int main(int argc, char *argv[])
{
    QApplication app(argc, argv);
    MyWidget widget;
    widget.show();
    return app.exec();
}
View Code

 

Display Images in widget

标签:

原文地址:http://www.cnblogs.com/xuyan505/p/5346458.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!