• 最简单的显示图片方法


    QT没有直接可以显示图片的专用控件,因此在控件上显示图片有点麻烦。

    但间接显示图片的方法有很多,下面就介绍最简单的一种(支持拉伸,自适应大小)

    1. #include <QtGui>    
    2.     
    3. int main(int argc, char **argv)    
    4. {    
    5.     QApplication app(argc, argv);    
    6.     
    7.     QFrame *frame = new QFrame;    
    8.     frame->setObjectName("avatar");    
    9.     //在程序的当前目录下,有logo_cn.png这个图片文件    
    10.     QString str = QString("QFrame#avatar{border-image:url(logo_cn.png)}");    
    11.     frame->setStyleSheet(str);    
    12.     frame->show();    
    13.     
    14.     return app.exec();   
    15. }  

    http://blog.csdn.net/small_qch/article/details/6664764

    -----------------------------------------------------------------------------------

    之前我写过一个可以直接显示图片的Button: http://blog.csdn.net/aaa20090987/article/details/6789380

    当时为了方便,直接用QFrame作为它的基类,结果(布局,使用等)十分不方便,大哭

    还是老老实实地用 QAbstractButton 作为基类,再用paintEvent来画图吧

    1. //tqt.h  
    2. #ifndef TQT_H_  
    3. #define TQT_H_  
    4.   
    5. #include <QtGui>  
    6. #include <QtCore>  
    7.   
    8. class PictureButton : public QAbstractButton  
    9. {  
    10.     Q_OBJECT  
    11. private:  
    12.     QPixmap pixmap;  
    13. protected:  
    14.     virtual void paintEvent(QPaintEvent *event);  
    15.     virtual QSize sizeHint() const;  
    16. public:  
    17.     PictureButton(const QString &path, QWidget *parent=0);  
    18.     PictureButton(QWidget *parent = 0);  
    19.     ~PictureButton();  
    20.     void setPixmap(const QString &path);  
    21. };  
    22.   
    23.   
    24. class Widget : public QWidget  
    25. {  
    26.     Q_OBJECT  
    27. private:  
    28.     QLabel *label;  
    29.     PictureButton *prevButton;  
    30.     PictureButton *nextButton;  
    31.     int num;  
    32.   
    33. public:  
    34.     Widget(QWidget *parent = 0);  
    35.     ~Widget();  
    36.   
    37.     public slots:  
    38.         void ClickedPrevButton();  
    39.         void ClickedNextButton();  
    40. };  
    41.   
    42. #endif  
    43.   
    44.   
    45. //tqt.cpp  
    46. #include "tqt.h"  
    47.   
    48. PictureButton::PictureButton(const QString &path, QWidget *parent/* =0 */)  
    49. : QAbstractButton(parent)  
    50. {  
    51.     setPixmap(path);  
    52.     setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);  
    53. }  
    54.   
    55. PictureButton::PictureButton(QWidget *parent /* = 0 */)  
    56. : QAbstractButton(parent)  
    57. {  
    58.     setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);  
    59. }  
    60.   
    61.   
    62. PictureButton::~PictureButton()  
    63. {  
    64.   
    65. }  
    66.   
    67. void PictureButton::paintEvent(QPaintEvent *event)  
    68. {  
    69.     QPainter painter(this);  
    70.     painter.drawPixmap(0, 0, width(), height(), pixmap);  
    71. }  
    72.   
    73. QSize PictureButton::sizeHint() const   
    74. {  
    75.     return pixmap.size();  
    76. }  
    77.   
    78. void PictureButton::setPixmap(const QString &path)  
    79. {  
    80.     pixmap.load(path);  
    81.     update();  
    82. }  
    83.   
    84. Widget::Widget(QWidget *parent)   
    85. : QWidget(parent)  
    86. {  
    87.     num = 0;  
    88.     label = new QLabel("0", this);  
    89.     //prev.jpg和next.jpg是当前文件夹中的两张JPG图片  
    90.     prevButton = new PictureButton("prev.jpg", this);  
    91.     nextButton = new PictureButton("next.jpg", this);  
    92.   
    93.     QHBoxLayout *subLayout = new QHBoxLayout;  
    94.     QVBoxLayout *layout = new QVBoxLayout;  
    95.     subLayout->addWidget(prevButton);  
    96.     subLayout->addWidget(nextButton);  
    97.     layout->addWidget(label);  
    98.     layout->addLayout(subLayout);  
    99.     setLayout(layout);  
    100.   
    101.     setWindowTitle("Widget");  
    102.     resize(200, 200);  
    103.     connect(prevButton, SIGNAL(clicked()), this, SLOT(ClickedPrevButton()));  
    104.     connect(nextButton, SIGNAL(clicked()), this, SLOT(ClickedNextButton()));  
    105. }  
    106.   
    107. Widget::~Widget()  
    108. {  
    109.   
    110. }  
    111.   
    112. void Widget::ClickedPrevButton()  
    113. {  
    114.     num--;  
    115.     label->setText(QString::number(num));  
    116. }  
    117.   
    118. void Widget::ClickedNextButton()  
    119. {  
    120.     num++;  
    121.     label->setText(QString::number(num));  
    122. }  
    123.   
    124.   
    125. //main.cpp  
    126. #include "tqt3.h"  
    127.   
    128. int main(int argc, char *argv[])  
    129. {  
    130.     QApplication a(argc, argv);  
    131.     Widget w;  
    132.     w.show();  
    133.     return a.exec();  
    134. }  


    注意:这个程序是用来研究的(或者说是玩的),如果要在按钮上加图片的话,直接用QToolButton就可以了

    http://blog.csdn.net/small_qch/article/details/6830483

  • 相关阅读:
    Mysql数据备份命令
    git命令大全
    git 安装及普通命令
    git学习资料
    php 魔术方法 和 魔术常量
    react事件处理(绑定)
    java父类、子类构造函数调用过程
    java字符串String的intern()方法,举例说明
    java关键字static使用的“坑”(准备、初始化)
    java虚拟机
  • 原文地址:https://www.cnblogs.com/findumars/p/4851474.html
Copyright © 2020-2023  润新知