分享

QT显示图片 共两大类方法

 cjavahtml 2016-04-27
  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);
}




  最后补充-----------程序启动时的图片

  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();

  来自: 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函数是用来截取图片的显示区域,如果没有该函数,该图片是被缩放的放到图片上

  用调色板加载图片:

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

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多