qt上麦图yy上麦名片怎么弄弄

Qt中使图片像素变小
  本文主要介绍了在Qt中使图片像素变小。对于编程人员来说,编程那是小事,大概因为笔者不是图形程序员的缘故,对这个小技巧十分之陌生,我想大家可能也有很大一部分人还不知道这个技巧,特此memo,希望对大家有用。
  以下的代码是将一个600*400的图片弄成400*580显示出来。
  QPixmap&pixmap(":/set_background600x400.png");
  QPixmap&fitpixmap=pixmap.scaled(400,560,&Qt::KeepAspectRatio);
  label-&setPixmap(fitpixmap);&
  我是参照网上的这句QPixmap fitPixmap = pixmap.scaled(width(),height(),
Qt::KeepAspectRatio);
  我的例子:
  #ifndef&TEXT_H &
  #define&TEXT_H &
  #include&
  #include&
  #include&
  #include&
  //#include&&&
  class&magic:public&QWidget
  public: &
  magic(QWidget&*parent=0);
  private: &
  QLabel&* &
  //void&paintEvent(QPaintEvent&*);
  #endif&//&TEXT_H
  #include&"text.h" &
  magic::magic(QWidget&*parent):QWidget(parent)
  setGeometry(20,20,800,600); &
  label=new&QLabel(this); &
  label-&setGeometry(QRect(10,&10,&400,&580));
  QPixmap&pixmap(":/set_background600x400.png");
  QPixmap&fitpixmap=pixmap.scaled(500,&500).scaled(400,560,&Qt::IgnoreAspectRatio,&Qt::SmoothTransformation);
  label-&setPixmap(fitpixmap); &
  int&main(int&argc,char&**argv)
  QApplication&app(argc,argv);
  magic&M; &
  M.show(); &
  return&app.exec(); &
  还有网上的另外一个也有参考价值。
  在Qt Labs Blog里提到了一个快速缩小大图片的方法, 而且作者说是“几乎所有图形程序员都知道的trick”。
大概因为笔者不是图形程序员的缘故, 对这个小技巧十分之陌生, 我想大家可能也有很大一部分人还不知道这个技巧, 特此memo,
希望对大家有用。
  QImage提供了缩放图片的函数 scaled,
并且可以用参数指定”快速缩放(FastTransformation)”还是”平滑缩放(SmoothTransformation)”,
使用还是很方便的。 但是如果你用缩放函数去做大图片的缩略图可能会发现”快速缩放”得到的图片质量不佳,
而”平滑缩放”质量很好但速度欠佳, 特别是原图非常大的时候smoothscale简直就是个噩梦阿。 这里就可以使用被称为“Cheat
Scaling”的缩小图片的技巧了, 那就是先使用”快速缩放”得到一个中等大小的图片以获得较快的缩放速度,
再使用”平滑缩放”缩小至需要的大小以获得较好的图片质量。
  如下代码:
  QImage&result&=&img.scaled(800,&600).scaled(200,&150,&Qt::IgnoreAspectRatio,&Qt::SmoothTransformation);&
  一个公认比较好的方法是,先缩至缩略图4倍大小, 再进一步平滑缩放。 按照原作者的测试, 该算法甚至比“快速缩放”还要略快,
却能获得和“平滑缩放”极其接近的最终结果。
  小结:在Qt中使图片像素变小这篇内容介绍完了,对于编程熟练的你,是否想熟练编程一样熟悉它喽,那么希望本文章能够帮到你的忙。
原文链接:
已投稿到:
以上网友发言只代表其个人观点,不代表新浪网的观点或立场。QT如何取图片的一部分,让其它部分透明?
[问题点数:100分]
QT如何取图片的一部分,让其它部分透明?
[问题点数:100分]
不显示删除回复
显示所有回复
显示星级回复
显示得分回复
只显示楼主
匿名用户不能发表回复!|
每天回帖即可获得10分可用分!小技巧:
你还可以输入10000个字符
(Ctrl+Enter)
请遵守CSDN,不得违反国家法律法规。
转载文章请注明出自“CSDN(www.csdn.net)”。如是商业用途请联系原作者。QT creator 如何给主窗口添加背景图片,并且上面的控件能够覆盖背景图片?_百度知道
QT creator 如何给主窗口添加背景图片,并且上面的控件能够覆盖背景图片?
出现重叠。怎样才能使mainwindow有背景图片而且不影响控件,我使用stylesheet给mainwindow添加背景图片后,谢谢各位了。,所有的控件都有背景图片,
提问者采纳
使用stylesheet的时候要让其余的控件不继承这个规则,详细做法请查看 qss 规则,jpg&quot,,,setBrush(QPalette,palette,QPixmap pixmap(&quot,&#92,background&#92,,QPalette palette=this-&gt,QBrush(pixmap)),),this-&gt,&#92,&#92,E,palette(),&#92,,在makefile的构造函数里这么写,这个图片路径写自己的,making&#92,Background,setPalette(palette),sonico,
我在父控件设置:border-image: url(://Resources/title.png);在子控件设置:border-image: \*url();搞定了。谢谢你的帮助。
提问者评价
太感谢了,真心有用
其他类似问题
背景图片的相关知识
等待您来回答
您可能关注的推广回答者:
下载知道APP
随时随地咨询
出门在外也不愁#include&&QApplication&#include&&QtGui&int&main(int&argc,&char&*argv[]){&&&&QApplication&app(argc,argv);&&&&&&&&QFrame&*frame&=&new&QF&&&&frame-&resize(400,700);&&& QPixmap pixmap("images/frame.png");&&&&QPalette&&&&&& palette.setBrush(frame-&backgroundRole(),QBrush(pixmap));&&&&frame-&setPalette(palette);&&& frame-&setMask(pixmap.mask());& //可以将图片中透明部分显示为透明的& & frame-&setAutoFillBackground(true);&&&&frame-&show();&&&&return&app.exec();}
注意图片路径怎么表示,我的图片放在该工程下的images文件夹中。存在问题:图片可以显示出来,但是图片大小不能和frame大小一致,显示效果不好,具体怎样调整大小,以后再补充,效果如下(设置了透明的,好像很漂亮~透明部分将我的桌面显示出来了~_~):2.setStyleSheet方法(非常好用的方法)
#include&&QApplication&#include&&QtGui&int&main(int&argc,&char&*argv[]){&&&&QApplication&app(argc,argv);&&&&QFrame&*frame&=&new&QF&&&&frame-&setObjectName("myframe");&&&&frame-&resize(400,700);&&& frame-&setStyleSheet("QFrame#myframe{border-image:url(images/frame.png)}"&);&&& frame-&show();&&&&return&app.exec();}
效果如下:注意:很漂亮的效果吧~~注意代码中红线的部分噢,设置ObjectName后,才能保证setStyleSheet只作用在我们的frame上,不影响其子控件的背景设置。之所以用border-image而不用background-image,还是上面的问题,用background-image不能保证图片大小和控件大小一致,图片不能完全显示,这个以后再补充了,现在还没有找到方法。3.paintEvent事件方法
//myframe.h文件#ifndef&MYFRAME_H#define&MYFRAME_H#include&&QWidget&#include&&QtGui&class&MyFrame&:&public&QWidget{public:&&&&MyFrame();&&&&void&paintEvent(QPaintEvent&*event);};#endif&//&MYFRAME_H//myframe.cpp文件#include&"myframe.h"MyFrame::MyFrame(){}void&MyFrame::paintEvent(QPaintEvent&*event){&&&&QPainter&painter(this);&&&&painter.drawPixmap(0,0,400,700,QPixmap("images/frame.png"));}//main.cpp文件#include&&QApplication&#include&&QtGui&#include&"myframe.h"int&main(int&argc,&char&*argv[]){&&&&QApplication&app(argc,argv);&&&&&&&&MyFrame&*frame&=&new&MyF&&&&frame-&resize(400,700);&&&&frame-&show();&&&&return&app.exec();}
效果如下:注:跟前面一样的效果吧,与前面的差别就是这个背景图片不随着窗口的大小而变化,因为它的固定大小被设置成(400,700)了。重写QWidget的paintEvent事件,当控件发生重绘事件,比如show()时,系统就会自动调用paintEvent函数。好了,上面是三种设置背景图片的方法,下面我要说一个设置QPushButton的背景图片的方法,用的是setIcon方法(其实QPushButton设置背景图片也可以用前面三种方法的,不过现在这种Icon方法的看起来也不错)
#include&&QApplication&#include&&QtGui&int&main(int&argc,&char&*argv[]){&&&&QApplication&app(argc,argv);&&&&QFrame&*frame&=&new&QF&&&&QPushButton&*&button0&=&new&QPushButton(frame);&&&&QPushButton&*&button1&=&new&QPushButton(frame);&&&&QPushButton&*&button2&=&new&QPushButton(frame);&&&&QPushButton&*&button3&=&new&QPushButton(frame);&&&&QPushButton&*&button4&=&new&QPushButton(frame);&&&&QPushButton&*&button5&=&new&QPushButton(frame);&&&&frame-&setObjectName("myframe");&&&&frame-&resize(400,700);&&&&frame-&setStyleSheet("QFrame#myframe{border-image:url(images/frame.png)}"&);&&&&button0-&setGeometry(60,150,68,68);&&&&button1-&setGeometry(160,150,68,68);&&&&button2-&setGeometry(260,150,68,68);&&&&button3-&setGeometry(60,280,68,68);&&&&button4-&setGeometry(160,280,68,68);&&&&button5-&setGeometry(260,280,68,68);&&&&QIcon&&&&&QPixmap&pixmap0("images/SMS.png");&&&&icon.addPixmap(pixmap0);&&&&button0-&setIcon(icon);&&&&button0-&setIconSize(QSize(68,68));&&&&button0-&setFixedSize(pixmap0.size());&&&&button0-&setMask(pixmap0.mask());&&&&QPixmap&pixmap1("images/EMail.png");&&&&icon.addPixmap(pixmap1);&&&&button1-&setIcon(icon);&&&&button1-&setIconSize(QSize(68,68));&&&&button1-&setFixedSize(pixmap1.size());&&&&button1-&setMask(pixmap1.mask());&&&&QPixmap&pixmap2("images/Contacts.png");&&&&icon.addPixmap(pixmap2);&&&&button2-&setIcon(icon);&&&&button2-&setIconSize(QSize(68,68));&&&&button2-&setFixedSize(pixmap2.size());&&&&button2-&setMask(pixmap2.mask());&&&&QPixmap&pixmap3("images/Calendar.png");&&&&icon.addPixmap(pixmap3);&&&&button3-&setIcon(icon);&&&&button3-&setIconSize(QSize(68,68));&&&&button3-&setFixedSize(pixmap3.size());&&&&button3-&setMask(pixmap3.mask());&&&&QPixmap&pixmap4("images/GoogleVoice.png");&&&&icon.addPixmap(pixmap4);&&&&button4-&setIcon(icon);&&&&button4-&setIconSize(QSize(68,68));&&&&button4-&setFixedSize(pixmap4.size());&&&&button4-&setMask(pixmap4.mask());&&&&QPixmap&pixmap5("images/AndroidMarket.png");&&&&icon.addPixmap(pixmap5);&&&&button5-&setIcon(icon);&&&&button5-&setIconSize(QSize(68,68));&&&&button5-&setFixedSize(pixmap5.size());&&&&button5-&setMask(pixmap5.mask());&&&&frame-&show();&&&&return&app.exec();}
效果如下:注:图标效果不错吧~_~好了,今天就写到这里,以后有新的内容再补充。补充,这样就可以让图片跟窗口一样大小了。
int&main(int&argc,&char&*argv[]){&&&&QApplication&app(argc,argv);&&&&&&&&QFrame&*frame&=&new&QF&&&&frame-&resize(400,700);&&&&QImage&image1;&&&&image1.load("images/frame1.jpg");&&&&QImage&image2&=&image1.scaled(400,700);&&&&QPalette&&&&&&&palette.setBrush(frame-&backgroundRole(),QBrush(image2));&&&&frame-&setPalette(palette);&&&&frame-&setMask(pixmap.mask());&&//可以将图片中透明部分显示为透明的&&&&frame-&setAutoFillBackground(true);&&&&frame-&show();&&&&return&app.exec();}
哥们你这个模拟器是symbian的还是android的?看到你的贴图我都困惑了
@孔雀I am a girl.No symbian or android.Just Linux and QT(Qt creator).The phone frame is just a picture, not a simulator.
@qianqian
very good !! I like it!
可以留个 QQ一起交流吗?我q的是 。
4、设置字体大小、粗体、斜体、颜色、背景的时候要注意:setFont可以设置,但是前提是没有用setStyleSheet函数  否则 setFont不起作用,同时还可以用html脚步&font&&/font&这样设置。  StyleSheet方法:textEdit-&setStyleSheet(&font-size:18&&background-color:&   &border-style:&&font-style:&&font-weight:&&color:white&);   setFont方法: QF  font.setFamily(&Bitstream Charter&);  font.setPixelSize(16);  font.setBold(true);   font.setItalic(true);  textEdit-&setFont(font);  html方法:textEdit-&setText(&&font color=red&abc&/font&&)  
果断被忽悠了,竟然是个图,不是个模拟器?
@pacco
请你看清楚行么,我从来没说这是模拟器,我说了是qt添加背景图片的代码,而且我代码都贴出来了,你看得懂就该知道是怎么回事,看不懂不要在这乱说我忽悠谁,大哥,拜托你看清楚了再说话,我学习记笔记不算忽悠谁吧。
很喜欢你的博客,简明得体,美观大方。以后小弟会多多参阅学习的。
是在QVFB下还是直接环境下
我换了张图片试了一下程序,出现了QFrame和frame没有声明的问题,不知道怎么回事呢
QPushButton 这里为什么不用layout呢?
QPushButton为什么不用layout呢?
QFrame是不是也可以用layout呢?没试过,有空在琢磨琢磨。。
你好,我用3个button添加背景图,然而第三个button的图标老是显示不对,是第二个的图标,请问怎么改呀?
随笔分类(28)
随笔档案(28)
图形学网站
阅读排行榜
评论排行榜qt上麦图片怎么弄
qt上麦图片怎么弄
不区分大小写匿名
QQ餐厅进不了,农场和牧场能进,这是什么原因?
山治真的变成人妖了么?后来怎么样了?
跪求Easonchen2013年巡回演唱会在深圳是什么时候?!!
等待您来回答
QQ堂领域专家

我要回帖

更多关于 qt label显示图片 的文章

 

随机推荐