QT显示图片的四种方法

  • 格式:pdf
  • 大小:72.49 KB
  • 文档页数:3

QT显⽰图⽚的四种⽅法

相关⽂章

QT显⽰图⽚的⽅法有很多

⼤致为

label上显⽰

直接画出来

容器显⽰

1---------------显⽰gif图⽚(label上显⽰)

在QT中要显⽰GIF图⽚,不能通过单单的添加部件来完成.

还需要⼿动的编写程序.

⼯具:QT Creator

新建⼀个⼯程,我们先在designer中,添加⼀个QLabel部件.

如下图:

将QLabel拉成适当⼤⼩.

在类cpp函数中添加如下程序:

#include "widget.h"

#include "ui_widget.h"

#include

#include

Widget::Widget(QWidget *parent) :

QWidget(parent),

ui(new Ui::Widget)

{

ui->setupUi(this);

QMovie *movie =new QMovie("D:/Project/Qt/testclass/2.gif");

ui->label->setMovie(movie);

movie->start();

}

Widget::~Widget()

{

delete ui;

}

如下图:

这⾥要注意QMovie中的路径名:"D:/Project/Qt/testclass/2.gif" 这⾥的路径斜杠和WINDOWS下是相反的.WINDOWS下默认是反斜杠.

编译,运⾏就没有问题,就会看到GIF⽂件在播放了.

如下图:当⽂档GIF图⽚显⽰:

#include

#include

#include

int main(int argc,char*argv[])

{

QApplication app(argc,argv);

QLabel *label =new QLabel();

QMovie *movie =new QMovie("D:/Project/Qt/firstQT/2.gif");

label->setMovie(movie);

movie->start();

label->show();

return app.exec();

}

2-------------------------label上显⽰图⽚------------------

把你的label.png放到⼯程⽬录顶层,直接QPixmap pixmap("label.png");

ui->title_label->setPixmap(pixmap);

ui->title_label->show();

---

可以直接:label->setPixmap(QPixmap("./pic.jpg"));

或者:QImage *image= new QImage("./pic.jpg");

label->setPixmap(QPixmap::fromImage(image));

再或者在中途换某个图像的话: QImage *image= new QImage("./pic1.jpg");

label->setPixmap(QPixmap::fromImage(image));

...........

image->load("./pic2.jpg");

3----------直接画出图⽚-------------------------

voidlogindlg::paintEvent(QPaintEvent*)

{

QPainterpainter(this);

QPixmappix;

pix.load("D:/QT/login/login/images/delta.png");

painter.drawPixmap(0,0,100,33,pix);

//painter.drawLine(0,0,100,100);

}

4-----------程序启动时的图⽚

QApplication app(argc, argv);

QSplashScreen *splash = new QSplashScreen;

splash->setPixmap(QPixmap(":/images/splash.png"));//设置图⽚

splash->show();//显⽰图⽚

Qt::Alignment topRight = Qt::AlignRight | Qt::AlignTop;

splash->showMessage(QObject::tr("Setting up the main window..."),topRight, Qt::white);//显⽰信息

MainWindow mainWin;

splash->showMessage(QObject::tr("Loading modules..."),

topRight, Qt::white); //显⽰信息

loadModules();

splash->showMessage(QObject::tr("Establishing connections..."),

topRight, Qt::white); //显⽰信息

establishConnections();

mainWin.show();

splash->finish(&mainWin);//图⽚⼀直显⽰到mainWin加载完成

delete splash;//释放空间,节省内存

return app.exec();

来⾃:

⾸先你得加载⼀张能显⽰透明的图⽚,jpg格式肯定是不⾏的,⼀般都是png

还有不同的部件加载图⽚的⽅法也不太相同,⽐如:QLabel加载图⽚:

C/C++ code

QString strPath=imagePath.value(day); //图⽚路径

QPixmap pix(strPath);

dayLabel->setPixmap(pix);

QPushButton加载图⽚:

C/C++ code

button->setIcon(QIcon("toolbutton.png"));

button->setIconSize(QSize(48, 48));

其中setIconSize函数是⽤来截取图⽚的显⽰区域,如果没有该函数,该图⽚是被缩放的放到图⽚上

⽤调⾊板加载图⽚:C/C++ code

QPalette p = palette();

p.setBrush(QPalette::Button, QBrush(QPixmap("toolbutton.png")));

setPalette(p);

另外实现按钮的透明:C/C++ code

button->setFlat(true);

还有就是⽤绘制事件函数了:C/C++ code

QPixmap arrayImage("/home/image/array.png"); //图⽚路径

QRect arrayRect(0,0,50,50); //截取图⽚区域

QPainter painter;

painter.drawPixmap(QPoint(100,100),arrayImage,arrayRect); //打印图⽚

//=============================================================================================================================

备注::

1>这⾥要注意QMovie中的路径名:"D:/Project/Qt/testclass/2.gif" 这⾥的路径斜杠和WINDOWS下是相反的.WINDOWS下默认是反斜杠.