qt旋转图片,实现加载窗口

头文件

#pragma once

#include <QWidget>
#include <QTime>

class QPixmap;
class QTimer;

class MyRoundWidget : public QWidget
{
	Q_OBJECT

public:
	MyRoundWidget(QWidget *parent, int radius = 128);
	~MyRoundWidget();

	void showRount(int x, int y, const QSize &size, QString msg = QString());

protected:
	virtual void paintEvent(QPaintEvent *event) override;

private slots:
	void my_timer_timeout();

private:
	QPixmap *_pixmap;
	QTimer *_timer;
	QTime _startTime;

	QString _darwText;
	int _imageRotate;
	int _radius;
};

源文件:

#include "myroundwidget.h"

#include <QApplication>
#include <QPainter>
#include <QPixmap>
#include <QTimer>
#include <QFontMetrics>

MyRoundWidget::MyRoundWidget(QWidget *parent, int radius)
	: QWidget(parent), _imageRotate(0), _radius(radius)
{
	_pixmap = new QPixmap(":/images/rotating.png");

	_timer = new QTimer(this);
	connect(_timer, SIGNAL(timeout()), this, SLOT(my_timer_timeout()));
}

MyRoundWidget::~MyRoundWidget()
{
	delete _pixmap;
}

void MyRoundWidget::showRount(int x, int y, const QSize &size, QString msg)
{
	this->move(x, y);
	this->setFixedSize(size);

	_startTime = QTime::currentTime();
	_darwText = msg;

	_timer->start(20);
	this->show();
}

void MyRoundWidget::my_timer_timeout()
{
	_imageRotate += 10;
	if (_imageRotate >= 360)
		_imageRotate = 0;

	this->update();
	qApp->processEvents();
}

void MyRoundWidget::paintEvent(QPaintEvent *event)
{
	QPainter painter(this);
	painter.fillRect(this->rect(), QColor(98, 98, 98, 98));

	int pw = _radius;
	int ph = _radius;

	int cx = this->width() / 2;
	int cy = this->height() / 2;

	int sx = (this->width() - pw) / 2;
	int sy = (this->height() - ph) / 2;

	if (!_darwText.isEmpty())
	{
		QString msg = _darwText + QString("[%1s]").arg(_startTime.secsTo(QTime::currentTime()));
		QFontMetrics fm(font());
		int w = fm.width(msg);
		int h = fm.height();
		painter.drawText(QRectF(cx - w / 2.0, cy - h / 2.0 + ph / 2.0 + 20, w, h), msg);
	}

	painter.translate(cx, cy);
	painter.rotate(_imageRotate);
	painter.translate(-cx, -cy);

	painter.setRenderHint(QPainter::SmoothPixmapTransform, true);
	painter.drawPixmap(sx, sy, pw, ph, *_pixmap);

	return QWidget::paintEvent(event);
}

头文件:

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QPixmap>
#include <QTimer>
#include <QTime>

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();
   // void showRount(int x, int y, const QSize &size, QString msg = QString());

protected:
   virtual void paintEvent(QPaintEvent *event) override;

protected slots:
    //void my_timer_timeout();

private:
    Ui::MainWindow *ui;
     QPixmap *_pixmap;
     QTimer *_timer;
  /*  QTime _startTime;

    QString _darwText;
    int _imageRotate;
    int _radius;*/
};

#endif // MAINWINDOW_H

源文件

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QApplication>
#include <QPainter>
#include <QPixmap>
#include <QTimer>
#include <QFontMetrics>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    //ui->comboBox->setStyleSheet("QComboBox::drop-down { image:url(:/new/prefix1/xljt.png)}");
   _pixmap=new QPixmap(":/new/prefix1/jz.png");
   _timer=new QTimer(this);
   _timer->start(10);
   this->setWindowFlags(Qt::FramelessWindowHint |Qt::WindowStaysOnTopHint);
   this->setAttribute(Qt::WA_TranslucentBackground);//支持alpha通道!!!
   this->setAttribute(Qt::WA_NoSystemBackground);//不自动绘制背景
   connect(_timer,SIGNAL(timeout()),this,SLOT(update()));

}

MainWindow::~MainWindow()
{
    //delete _pixmap;
    delete ui;
}

void MainWindow::paintEvent(QPaintEvent *e)
{
static int i =0;//定义旋转度数
i++;

QPainter paint(this);
paint.translate(this->width()/2,this->height()/2);//设置原点为窗口中心
paint.rotate(i);//坐标旋转
paint.drawPixmap(-_pixmap->height()/2,-_pixmap->width()/2,*_pixmap);//经分析,应使图片中心,对应原点
if(i >= 360)
i = 0;//旋转度数检测
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: Qt Image Viewer是基于Qt框架实现图片浏览器。Qt是跨平台的C++应用程序开发框架,它提供了丰富的图形界面控件和功能库,适合用于创建各种类型的应用程序。 在Qt Image Viewer中,我们可以使用QImage类来加载和处理图片。QImage类提供了许多用于处理图片的函数,例如加载图片、缩放、旋转等等。 在实现图片浏览器的时候,我们可以使用QFileDialog类来打开图片文件。用户可以通过该类的getOpenFileName函数选择要打开的图片文件,并将文件路径传递给QImage类进行加载。 为了显示图片,我们可以使用QLabel类作为图片的容器。在QLabel中,我们可以使用setPixmap函数将QImage对象转换为QPixmap,并使用setScaledContents函数将图片自适应地放置在容器中。 为了实现浏览多张图片的功能,我们可以在窗口中添加按钮或者菜单栏来供用户选择前一张或者后一张图片。当用户点击这些按钮时,我们可以在加载图片之前判断是否已经加载了一张图片,如果已经加载,则根据用户的操作加载前一张或者后一张图片。 此外,我们还可以添加一些其他的功能,如缩放图片旋转图片、保存图片等等。这些功能可以通过在窗口中添加一些按钮或者菜单项来实现,并与对应的QImage函数进行连接。 总的来说,Qt Image Viewer通过使用Qt框架提供的函数和控件,可以实现一个简单但功能齐全的图片浏览器。用户可以方便地打开、查看和操作图片。 ### 回答2: Qt是一款功能强大的跨平台应用程序开发框架,它提供了丰富的类库和工具,可用于开发各种类型的应用程序,包括图片浏览器。 在Qt中,可以使用QImage来加载和显示图片。首先,我们可以通过QFileDialog类选择要显示的图片文件,然后使用QImage加载该文件。加载完成后,可以将QImage转换为QPixmap,以便在窗口中显示。我们可以使用QLabel或QGraphicsView来显示QPixmap,从而实现图片浏览器的界面。 为了能够浏览不同图片,我们可以使用QPushButton或QAction添加上一张和下一张图片的功能。当点击这些按钮时,可以切换当前显示的图片。另外,我们还可以使用QSlider或QScrollBar添加缩放功能,以实现放大和缩小图片的效果。 此外,为了方便用户对图片进行查看和编辑,我们可以将图片浏览器与其他功能结合起来。例如,我们可以添加图像旋转、翻转、裁剪等操作的功能按钮,以及添加保存图片的功能按钮。 最后,为了提高用户体验,我们还可以添加一些便捷的功能,如拖拽图片窗口中自动加载,支持多种图片格式,添加全屏显示的功能等。 综上所述,使用Qt的QImage和QPixmap,以及QLabel、QGraphicsView、QPushButton、QAction、QSlider等类,我们可以很方便地实现一个简单但功能完善的图片浏览器。通过选取和加载图片文件,并提供方便的浏览和编辑操作,可以满足用户对图片浏览器的基本需求。 ### 回答3: 在使用Qt实现图片浏览器时,我们可以使用Qt的QImage和QPixmap类来处理和显示图片。 首先,我们需要创建一个主窗口,在主窗口中添加一个QGraphicsView作为显示图片的画布。然后,我们可以通过QFileDialog来打开一个图片文件,获取图片的路径。 接下来,我们可以使用QImage类来读取图片的数据,并将其转换为QPixmap格式以便显示在QGraphicsView中。我们可以通过QGraphicsScene来管理QGraphicsView中的显示内容,将QPixmap添加到QGraphicsScene中,并将场景关联到QGraphicsView上。 为了实现图片的浏览功能,我们可以在主窗口中添加两个按钮,一个是“上一张”,一个是“下一张”。点击这些按钮时,我们可以通过改变图片的路径并重新加载图片实现图片的切换。 此外,我们还可以添加其他功能,例如放大缩小图片旋转图片等,可以使用QTransform类来实现这些功能。 最后,我们还可以使用QScrollBar来实现图片的滚动效果,当图片的大小超过QGraphicsView的大小时,可以通过滚动条来查看完整的图片内容。 总结一下,使用Qt的QImage和QPixmap类结合QGraphicsView和QGraphicsScene,我们可以方便地实现一个简单的图片浏览器,并添加功能如图片切换、放大缩小、旋转和滚动等。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值