QT顯示圖片 .轉


QT顯示圖片的方法有很多

大致為

label上顯示

直接畫出來

容器顯示

 

1---------------顯示gif圖片(label上顯示)

http://archive.cnblogs.com/a/2053701/

 

在QT中要顯示GIF圖片,不能通過單單的添加部件來完成.

還需要手動的編寫程序.

工具:QT Creator

新建一個工程,我們先在designer中,添加一個QLabel部件.

如下圖:


將QLabel拉成適當大小.

在類cpp函數中添加如下程序:

#include " widget.h "
#include
" ui_widget.h "
#include
< QLabel >
#include
< QMovie >

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 < QtGui / QApplication >
#include
< QLabel >
#include
< QMovie >

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); //打印圖片


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM