I created a BackgroundWidget class that does what you want:
#ifndef BACKGROUNDWIDGET_H
#define BACKGROUNDWIDGET_H
#include <QWidget>
#include <QPixmap>
#include <QString>
#include <QSize>
#include <QPaintEvent>
class BackgroundWidget
: public QWidget{
Q_OBJECT
public:
~BackgroundWidget();
void setPicture(const QString& bgImagePath);
signals:
void backgroundPainted();
protected:
private:
};
#endif // BACKGROUNDWIDGET_H
#ifndef BACKGROUNDWIDGET_H
#define BACKGROUNDWIDGET_H
#include <QWidget>
#include <QPixmap>
#include <QString>
#include <QSize>
#include <QPaintEvent>
class BackgroundWidget: public QWidget
{
Q_OBJECT
public:
BackgroundWidget(QWidget* parent, QString bgImagePath);
~BackgroundWidget();
void setPicture(const QString& bgImagePath);
void setPixmap(QPixmap* bgImage);
QSize sizeHint() const;
signals:
void backgroundPainted();
protected:
void paintEvent(QPaintEvent *event);
private:
QPixmap Image;
QBrush Brush;
};
#endif // BACKGROUNDWIDGET_H
To copy to clipboard, switch view to plain text mode
First Ctor arg is your widget (QMainWindow for example), second arg is the path to the picture, possibly stored as resource.
Here is the implementation:
#include "BackgroundWidget.h"
#include <QWidget>
#include <QPixmap>
#include <QString>
#include <QSize>
#include <QPaintEvent>
#include <QPainter>
BackgroundWidget
::BackgroundWidget(QWidget *parent,
QString bgImagePath
): QWidget(parent
), Image
(), Brush
(){
parentWidget()->stackUnder(this);
setPicture(bgImagePath);
lower();
}
BackgroundWidget::~BackgroundWidget()
{
}
QSize BackgroundWidget
::sizeHint() const {
return parentWidget()->visibleRegion().boundingRect().size();
}
void BackgroundWidget::setPicture(const QString& bgImagePath)
{
}
void BackgroundWidget
::setPixmap(QPixmap* bgImage
) {
if (! bgImage)
return;
}
{
resize(parentWidget()->visibleRegion().boundingRect().size());
painter.setBrush(Brush);
int x = (width() - Image.width()) / 2;
int y = (height() - Image.height()) / 2;
painter.drawPixmap(x, y, Image.width(), Image.height(), Image);
emit backgroundPainted();
}
#include "BackgroundWidget.h"
#include <QWidget>
#include <QPixmap>
#include <QString>
#include <QSize>
#include <QPaintEvent>
#include <QPainter>
BackgroundWidget::BackgroundWidget(QWidget *parent, QString bgImagePath): QWidget(parent), Image(), Brush()
{
parentWidget()->stackUnder(this);
setPicture(bgImagePath);
setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
lower();
Brush = QBrush(Image);
}
BackgroundWidget::~BackgroundWidget()
{
}
QSize BackgroundWidget::sizeHint() const
{
return parentWidget()->visibleRegion().boundingRect().size();
}
void BackgroundWidget::setPicture(const QString& bgImagePath)
{
Image = QPixmap(bgImagePath);
}
void BackgroundWidget::setPixmap(QPixmap* bgImage)
{
if (! bgImage)
return;
Image = QPixmap(*bgImage);
}
void BackgroundWidget::paintEvent(QPaintEvent *event)
{
resize(parentWidget()->visibleRegion().boundingRect().size());
QPainter painter(this);
painter.setBrush(Brush);
int x = (width() - Image.width()) / 2;
int y = (height() - Image.height()) / 2;
painter.drawPixmap(x, y, Image.width(), Image.height(), Image);
emit backgroundPainted();
}
To copy to clipboard, switch view to plain text mode
See attachment for complete code
rvgrouik
Bookmarks