[solved] Background image Fill & resize with window
-
hi all,
Yes i am very new hear so bear whit me.
I am trying to get a background image to fill the whole gui and keep it in the gui window even when you make it bigger.
But i look via Google on the subject and only fount dead ends or only half explain t code's .
so ma-by some can give me a punch in correct direction on how to do it.
because the code that i have now ill only give me a tiled appearance when i re-size the GUI.@#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QPainter>MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);QPixmap bkgnd("/home/ducky/pic/somepic.jpg"); bkgnd = bkgnd.scaled(this->size(), Qt::IgnoreAspectRatio); QPalette palette; palette.setBrush(QPalette::Background, bkgnd); this->setPalette(palette);
}
MainWindow::~MainWindow()
{
delete ui;
}@Thanx in advansed.
Bart
-
Hi, welcome to devnet.
This code is executed once so it only gets the "current" size of the window at the beginning of the program. You need to put that in the overridden resizeEvent method:
@
void MainWindow::resizeEvent(QResizeEvent *evt)
{
QPixmap bkgnd("/home/ducky/pic/somepic.jpg");
bkgnd = bkgnd.scaled(size(), Qt::IgnoreAspectRatio);
QPalette p = palette(); //copy current, not create new
p.setBrush(QPalette::Background, bkgnd);
setPalette(p);QMainWindow::resizeEvent(evt); //call base implementation
}
@ -
bq. no ‘void MainWindow::resizeEvent(QResizeEvent*)’ member function declared in class ‘MainWindow’ .
This tells you exactly what's wrong. It's c++. You need to declare your members in the header file:
@
//mainwindow.h
class Mainwindow : public QMainWindow {
... //the other stuff
protected:
void resizeEvent(QResizeEvent* evt) override;
};
@
If you're not using a c++11 compiler don't use the "override" keyword there. -
Yes that did the trick.
you solved something that i was struggling to understand for about 4 day's.
and now i get how the two are connected
so to have a recap of what i did.
my mainwindow.cpp looks like
@#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QPainter>MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);QPixmap bkgnd("/home/ducky/pic/somepic.jpg"); //Load pic bkgnd = bkgnd.scaled(this->size(), Qt::IgnoreAspectRatio); //set scale of pic to match the window QPalette palette; palette.setBrush(QPalette::Background, bkgnd);//set the pic to the background this->setPalette(palette); //show the background pic
}
void MainWindow::resizeEvent(QResizeEvent *evt)
{
QPixmap bkgnd("/home/ducky/pic/somepic.jpg");//Load pic
bkgnd = bkgnd.scaled(size(), Qt::IgnoreAspectRatio);//set scale of pic to match the window
QPalette p = palette(); //copy current, not create new
p.setBrush(QPalette::Background, bkgnd);//set the pic to the background
setPalette(p);//show the background picQMainWindow::resizeEvent(evt); //call base implementation
}
MainWindow::~MainWindow()
{
delete ui;
}@
and my mainwindows.h looks like
@#ifndef MAINWINDOW_H
#define MAINWINDOW_H#include <QMainWindow>
namespace Ui {
class MainWindow;
}class MainWindow : public QMainWindow
{
Q_OBJECTpublic:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();private:
Ui::MainWindow ui;
void resizeEvent(QResizeEvent evt); // declare The members
};#endif // MAINWINDOW_H@
-
Hi,
To add to Chris Kawa, you can use Q_DECL_OVERRIDE with Qt 5 so if you are using c++11 you have the benefit of override