zoukankan      html  css  js  c++  java
  • 最简单的显示图片方法

    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

  • 相关阅读:
    LeetCode15 3Sum
    LeetCode10 Regular Expression Matching
    LeetCode20 Valid Parentheses
    LeetCode21 Merge Two Sorted Lists
    LeetCode13 Roman to Integer
    LeetCode12 Integer to Roman
    LeetCode11 Container With Most Water
    LeetCode19 Remove Nth Node From End of List
    LeetCode14 Longest Common Prefix
    LeetCode9 Palindrome Number
  • 原文地址:https://www.cnblogs.com/findumars/p/4851474.html
Copyright © 2011-2022 走看看