QT顯示圖片的方法有很多 大致為 label上顯示 直接畫出來 1-------------------------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'); 2----------直接畫出圖片------------------------- 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); } 最后補(bǔ)充-----------程序啟動時的圖片 QApplication app(argc, argv); QSplashScreen *splash = new QSplashScreen; splash->setPixmap(QPixmap(':/images/splash.png'));//設(shè)置圖片 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;//釋放空間,節(jié)省內(nèi)存 return app.exec(); 來自: http://hi.baidu.com/sdink/blog/item/879e7013027a7fcaa7ef3f83.html 首先你得加載一張能顯示透明的圖片,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函數(shù)是用來截取圖片的顯示區(qū)域,如果沒有該函數(shù),該圖片是被縮放的放到圖片上 用調(diào)色板加載圖片: C/C++ code QPalette p = palette(); p.setBrush(QPalette::Button, QBrush(QPixmap('toolbutton.png'))); setPalette(p); 另外實(shí)現(xiàn)按鈕的透明: C/C++ code button->setFlat(true); 還有就是用繪制事件函數(shù)了: C/C++ code QPixmap arrayImage('/home/image/array.png'); //圖片路徑 QRect arrayRect(0,0,50,50); //截取圖片區(qū)域 QPainter painter; painter.drawPixmap(QPoint(100,100),arrayImage,arrayRect); //打印圖片 |
|