qt之图形图像操作

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函数


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
QT是一个跨平台的图形用户界面开发框架,可以用于实现各种应用程序,包括图形图像编辑器。 首先,我们可以使用QT的图形视图框架来构建图形图像编辑器的界面。可以使用QT自带的控件来实现工具栏、菜单栏、绘图区域等界面元素。通过QT的信号槽机制,可以实现用户对界面的操作响应,例如点击菜单项或者按钮时触发相应的功能。 其次,为了实现图形图像编辑的功能,需要基于QT提供的绘图API进行开发。通过使用QT的绘图函数,可以实现在绘图区域上绘制各种图形,如直线、矩形、圆形等。可以通过监听鼠标事件、键盘事件等来捕捉用户的操作,例如拖动鼠标绘制直线或者移动图形。 此外,图形图像编辑器还可以提供一些额外的功能,如选择、变换、编辑等。可以通过使用QT的图形视图框架来实现图形的选择、移动、缩放等操作。可以使用QT的图像处理函数,如旋转、裁剪、滤镜等来实现图像的编辑功能。 最后,为了提升用户体验,可以结合QT的其他功能,如撤销/重做功能、多文档支持、拖放操作等。这些功能可以通过使用QT的相关类库或者自定义类来进行开发。 总而言之,QT提供了丰富的工具和功能,可以方便地实现图形图像编辑器。通过使用QT的图形视图框架、绘图API和其他相关功能,可以创建一个功能完善的图形图像编辑器,满足用户的需求。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值