Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [solved] Background image Fill & resize with window

[solved] Background image Fill & resize with window

Scheduled Pinned Locked Moved General and Desktop
7 Posts 3 Posters 12.0k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    Ducky
    wrote on 3 Dec 2014, 21:37 last edited by
    #1

    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

    Well I used to write code in Autoit and work on Windows.
    And now i am trying to write code in QT and work on Ubuntu

    1 Reply Last reply
    0
    • C Offline
      C Offline
      Chris Kawa
      Lifetime Qt Champion
      wrote on 4 Dec 2014, 01:10 last edited by
      #2

      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
      

      }
      @

      1 Reply Last reply
      0
      • D Offline
        D Offline
        Ducky
        wrote on 4 Dec 2014, 08:25 last edited by
        #3

        Hi Thanx Chris,

        That make more scenes.
        I am stille getting a error.

        no 'void MainWindow::resizeEvent(QResizeEvent*)' member function declared in class 'MainWindow' .

        but that is probably due to i am still learning on how to Code in QT .

        Well I used to write code in Autoit and work on Windows.
        And now i am trying to write code in QT and work on Ubuntu

        1 Reply Last reply
        0
        • C Offline
          C Offline
          Chris Kawa
          Lifetime Qt Champion
          wrote on 4 Dec 2014, 10:58 last edited by
          #4

          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.

          1 Reply Last reply
          0
          • D Offline
            D Offline
            Ducky
            wrote on 4 Dec 2014, 17:10 last edited by
            #5

            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 pic

            QMainWindow::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_OBJECT

            public:
            explicit MainWindow(QWidget *parent = 0);
            ~MainWindow();

            private:
            Ui::MainWindow ui;
            void resizeEvent(QResizeEvent
            evt); // declare The members
            };

            #endif // MAINWINDOW_H@

            Well I used to write code in Autoit and work on Windows.
            And now i am trying to write code in QT and work on Ubuntu

            1 Reply Last reply
            0
            • D Offline
              D Offline
              Ducky
              wrote on 4 Dec 2014, 17:12 last edited by
              #6

              Thax again

              Well I used to write code in Autoit and work on Windows.
              And now i am trying to write code in QT and work on Ubuntu

              1 Reply Last reply
              0
              • S Offline
                S Offline
                SGaist
                Lifetime Qt Champion
                wrote on 4 Dec 2014, 21:46 last edited by
                #7

                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

                Interested in AI ? www.idiap.ch
                Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                1 Reply Last reply
                0

                3/7

                4 Dec 2014, 08:25

                • Login

                • Login or register to search.
                3 out of 7
                • First post
                  3/7
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved