void IFaceWClass::paintEvent(QPaintEvent *event) { painter = new QPainter(this); background = new QPixmap("D:\\ear.png"); painter->drawPixmap(0, 0, background->width(), background->height(), *background); }
palette = new QPalette(); palette->setBrush(this->backgroundRole(), QBrush(QPixmap("D:\\ear.png"))); this->setPalette(*palette);
C++ (Qt)#include <QApplication>#include <QWidget>#include <QPainter>#include <QPaintEvent> class MyWidget : public QWidget{public: MyWidget( const QString& fn, QWidget* p = 0 ) : QWidget( p ) { bg.load( fn ); } protected: void paintEvent( QPaintEvent* e ) { if( bg.isNull() ) { QWidget::paintEvent( e ); return; } QPainter p( this ); qreal xs = qreal( width() ) / qreal( bg.width() ); qreal ys = qreal( height() ) / qreal( bg.height() ); if( xs != 1 || ys != 1 ) { QImage img; int cx = qRound( e->rect().x() / xs ); int cy = qRound( e->rect().y() / ys ); int cw = qRound( e->rect().width() / xs ); int ch = qRound( e->rect().height() / ys ); cw = qMin( cw, bg.width() - cx ); ch = qMin( ch, bg.height() - cy ); img = bg.copy( cx, cy, cw, ch ); cw = qRound( cw * xs ); ch = qRound( ch * ys ); img = img.scaled( cw, ch, Qt::IgnoreAspectRatio, /*Qt::SmoothTransformation :*/ Qt::FastTransformation ); p.drawImage( e->rect().topLeft(), img ); } else { p.drawImage( e->rect().topLeft(), bg, QRect( e->rect().x(), e->rect().y(), qMin( e->rect().width(), bg.width() ), qMin( e->rect().height(), bg.height() ) ) ); } } QImage bg;}; int main( int argc, char* argv[] ){ QApplication a( argc, argv ); MyWidget w( argc > 1 ? argv[1] : "" ); w.show(); return a.exec();}
C++ (Qt)#include <QApplication>#include <QWidget>#include <QPainter>#include <QPaintEvent> class MyWidget : public QWidget{public: MyWidget( const QString& fn, QWidget* p = 0 ) : QWidget( p ) { bg.load( fn ); br.setTextureImage( bg ); } protected: void paintEvent( QPaintEvent* e ) { QPainter p( this ); br.setMatrix( QMatrix( qreal( width() ) / qreal( bg.width() ), 0, 0, qreal( height() ) / qreal( bg.height() ), 0, 0 ) ); p.setPen( Qt::NoPen ); p.setBrush( br ); p.drawRect( e->rect() ); } QImage bg; QBrush br;}; int main( int argc, char* argv[] ){ QApplication a( argc, argv ); MyWidget w( argc > 1 ? argv[1] : "" ); w.show(); return a.exec();}