QT没有直接可以显示图片的专用控件,因此在控件上显示图片有点麻烦。
但间接显示图片的方法有很多,下面就介绍最简单的一种(支持拉伸,自适应大小)
- #include <QtGui>
- int main(int argc, char **argv)
- {
- QApplication app(argc, argv);
- QFrame *frame = new QFrame;
- frame->setObjectName("avatar");
- //在程序的当前目录下,有logo_cn.png这个图片文件
- QString str = QString("QFrame#avatar{border-image:url(logo_cn.png)}");
- frame->setStyleSheet(str);
- frame->show();
- return app.exec();
- }
http://blog.csdn.net/small_qch/article/details/6664764
-----------------------------------------------------------------------------------
之前我写过一个可以直接显示图片的Button: http://blog.csdn.net/aaa20090987/article/details/6789380
当时为了方便,直接用QFrame作为它的基类,结果(布局,使用等)十分不方便,
还是老老实实地用 QAbstractButton 作为基类,再用paintEvent来画图吧
- //tqt.h
- #ifndef TQT_H_
- #define TQT_H_
- #include <QtGui>
- #include <QtCore>
- class PictureButton : public QAbstractButton
- {
- Q_OBJECT
- private:
- QPixmap pixmap;
- protected:
- virtual void paintEvent(QPaintEvent *event);
- virtual QSize sizeHint() const;
- public:
- PictureButton(const QString &path, QWidget *parent=0);
- PictureButton(QWidget *parent = 0);
- ~PictureButton();
- void setPixmap(const QString &path);
- };
- class Widget : public QWidget
- {
- Q_OBJECT
- private:
- QLabel *label;
- PictureButton *prevButton;
- PictureButton *nextButton;
- int num;
- public:
- Widget(QWidget *parent = 0);
- ~Widget();
- public slots:
- void ClickedPrevButton();
- void ClickedNextButton();
- };
- #endif
- //tqt.cpp
- #include "tqt.h"
- PictureButton::PictureButton(const QString &path, QWidget *parent/* =0 */)
- : QAbstractButton(parent)
- {
- setPixmap(path);
- setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
- }
- PictureButton::PictureButton(QWidget *parent /* = 0 */)
- : QAbstractButton(parent)
- {
- setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
- }
- PictureButton::~PictureButton()
- {
- }
- void PictureButton::paintEvent(QPaintEvent *event)
- {
- QPainter painter(this);
- painter.drawPixmap(0, 0, width(), height(), pixmap);
- }
- QSize PictureButton::sizeHint() const
- {
- return pixmap.size();
- }
- void PictureButton::setPixmap(const QString &path)
- {
- pixmap.load(path);
- update();
- }
- Widget::Widget(QWidget *parent)
- : QWidget(parent)
- {
- num = 0;
- label = new QLabel("0", this);
- //prev.jpg和next.jpg是当前文件夹中的两张JPG图片
- prevButton = new PictureButton("prev.jpg", this);
- nextButton = new PictureButton("next.jpg", this);
- QHBoxLayout *subLayout = new QHBoxLayout;
- QVBoxLayout *layout = new QVBoxLayout;
- subLayout->addWidget(prevButton);
- subLayout->addWidget(nextButton);
- layout->addWidget(label);
- layout->addLayout(subLayout);
- setLayout(layout);
- setWindowTitle("Widget");
- resize(200, 200);
- connect(prevButton, SIGNAL(clicked()), this, SLOT(ClickedPrevButton()));
- connect(nextButton, SIGNAL(clicked()), this, SLOT(ClickedNextButton()));
- }
- Widget::~Widget()
- {
- }
- void Widget::ClickedPrevButton()
- {
- num--;
- label->setText(QString::number(num));
- }
- void Widget::ClickedNextButton()
- {
- num++;
- label->setText(QString::number(num));
- }
- //main.cpp
- #include "tqt3.h"
- int main(int argc, char *argv[])
- {
- QApplication a(argc, argv);
- Widget w;
- w.show();
- return a.exec();
- }
注意:这个程序是用来研究的(或者说是玩的),如果要在按钮上加图片的话,直接用QToolButton就可以了
http://blog.csdn.net/small_qch/article/details/6830483