1. 绘制文字
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
mainwindow.cpp
#include "mainwindow.h"
#include "./ui_mainwindow.h"
#include <QGraphicsScene>
#include <QGraphicsView>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent), ui(new Ui::MainWindow)
{
ui->setupUi(this);
this->setWindowTitle("test app");
this->setWindowIcon(QIcon(":/new/prefix1/images/favicon.ico"));
// this->setWindowFlags(Qt::FramelessWindowHint);
// this->setAttribute(Qt::WA_TranslucentBackground, true);
// this->setStyleSheet("background:red");
//实例 QGraphicsScene
QGraphicsScene *scene = new QGraphicsScene;
//背景色
scene->setForegroundBrush(QColor(0, 255, 255, 127));
//字体设置
QFont font("黑体",60);
//添加文字
scene->addSimpleText("图形图像",font);
//网格
//scene->setForegroundBrush(QBrush(Qt::lightGray, Qt::CrossPattern));
//实例 QGraphicsView
QGraphicsView *view = new QGraphicsView(scene);
//显示
this->setCentralWidget(view);
}
MainWindow::~MainWindow()
{
delete ui;
}
2. 绘制线条
mainwindow.h同上。
mainwindow.cpp
#include "mainwindow.h"
#include "./ui_mainwindow.h"
#include <QGraphicsScene>
#include <QGraphicsView>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent), ui(new Ui::MainWindow)
{
ui->setupUi(this);
this->setWindowTitle("test app");
this->setWindowIcon(QIcon(":/new/prefix1/images/favicon.ico"));
// this->setWindowFlags(Qt::FramelessWindowHint);
// this->setAttribute(Qt::WA_TranslucentBackground, true);
// this->setStyleSheet("background:red");
//实例 QGraphicsScene
QGraphicsScene *scene = new QGraphicsScene;
//QPen 属性
QPen pen;
pen.setStyle(Qt::DashDotLine);
pen.setWidth(2);
pen.setBrush(Qt::black);
pen.setCapStyle(Qt::RoundCap);
pen.setJoinStyle(Qt::RoundJoin);
//绘制线条
scene->addLine(30,30,200,30,pen);
//实例 QGraphicsView
QGraphicsView *view = new QGraphicsView(scene);
//显示
this->setCentralWidget(view);
}
MainWindow::~MainWindow()
{
delete ui;
}
3. 绘制椭圆
mainwindow.h同上
mainwindow.cpp
#include "mainwindow.h"
#include "./ui_mainwindow.h"
#include <QGraphicsScene>
#include <QGraphicsView>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent), ui(new Ui::MainWindow)
{
ui->setupUi(this);
this->setWindowTitle("test app");
this->setWindowIcon(QIcon(":/new/prefix1/images/favicon.ico"));
// this->setWindowFlags(Qt::FramelessWindowHint);
// this->setAttribute(Qt::WA_TranslucentBackground, true);
// this->setStyleSheet("background:red");
//实例 QGraphicsScene
QGraphicsScene *scene = new QGraphicsScene;
//绘制椭圆
scene->addEllipse(50,50,100,120);
//实例 QGraphicsView
QGraphicsView *view = new QGraphicsView(scene);
//显示
this->setCentralWidget(view);
}
MainWindow::~MainWindow()
{
delete ui;
}
4. 显示静态图像
在mainwindow.h里加一个
protected:
void paintEvent(QPaintEvent *);
mainwindow.cpp
#include "mainwindow.h"
#include "./ui_mainwindow.h"
#include <QPainter>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent), ui(new Ui::MainWindow)
{
ui->setupUi(this);
this->setWindowTitle("test app");
this->setWindowIcon(QIcon(":/new/prefix1/images/favicon.ico"));
// this->setWindowFlags(Qt::FramelessWindowHint);
// this->setAttribute(Qt::WA_TranslucentBackground, true);
// this->setStyleSheet("background:red");
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::paintEvent(QPaintEvent *)
{
//实例 QPixmap
QPixmap image(":/new/prefix1/images/favicon.ico");
//实例 QPainter 绘制类
QPainter painter(this);
//绘制图片
painter.drawPixmap(20,20,200,257,image);
}
5.显示动态图像
mainwindow.h同1中的;
mainwindow.cpp
#include "mainwindow.h"
#include "./ui_mainwindow.h"
#include <QLabel>
#include <QTimer>
#include <QMovie>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent), ui(new Ui::MainWindow)
{
ui->setupUi(this);
this->setWindowTitle("test app");
this->setWindowIcon(QIcon(":/new/prefix1/images/favicon.ico"));
// this->setWindowFlags(Qt::FramelessWindowHint);
// this->setAttribute(Qt::WA_TranslucentBackground, true);
// this->setStyleSheet("background:red");
//实例 QLabel
QLabel *label = new QLabel(this);
label->setGeometry(QRect(50,50,88,51));
//实例 QMovie
QMovie *movie = new QMovie(":/new/prefix1/images/favicon.ico");
//3 秒后图片消失
QTimer::singleShot( 3*1000, label, SLOT(close()));
label->setMovie(movie);
movie->start();
}
MainWindow::~MainWindow()
{
delete ui;
}
6. 图片移动
改变图片的坐标即可
7. 图片翻转
利用mirrored函数即可
8. 图片缩放
利用scaled函数