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. Diskplaying a background image in main window.
Forum Update on Monday, May 27th 2025

Diskplaying a background image in main window.

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 3 Posters 9.1k 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
    DanJenk
    wrote on last edited by
    #1

    Hi nguys

    Very new to all this qt and programming but I am starting to learn slowly.

    I have an issue setting a .jpg picture as the back ground in the main window on qt creator , here is the code

    #include "mainwindow.h"
    #include "ui_mainwindow.h"

    MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
    {
    setStyleSheet("MainWindow {background-image:url(c:/Users/danny/Downloads/Tank-Konbackground.jpg)}");

    }

    MainWindow::~MainWindow()
    {
    delete ui;
    }

    I have already used Qpixmap and had the background working however I could not add text or wodgets over the top.

    After some more reading I came across the above example but when I run the code no image appears

    Can anybody help me out

    Danny

    J.HilkJ 1 Reply Last reply
    0
    • D DanJenk

      Hi nguys

      Very new to all this qt and programming but I am starting to learn slowly.

      I have an issue setting a .jpg picture as the back ground in the main window on qt creator , here is the code

      #include "mainwindow.h"
      #include "ui_mainwindow.h"

      MainWindow::MainWindow(QWidget *parent)
      : QMainWindow(parent)
      , ui(new Ui::MainWindow)
      {
      setStyleSheet("MainWindow {background-image:url(c:/Users/danny/Downloads/Tank-Konbackground.jpg)}");

      }

      MainWindow::~MainWindow()
      {
      delete ui;
      }

      I have already used Qpixmap and had the background working however I could not add text or wodgets over the top.

      After some more reading I came across the above example but when I run the code no image appears

      Can anybody help me out

      Danny

      J.HilkJ Offline
      J.HilkJ Offline
      J.Hilk
      Moderators
      wrote on last edited by
      #2

      hi @DanJenk and welcome,

      try it with the following

      "QMainWindow {background-image:url(c:/Users/danny/Downloads/Tank-Konbackground.jpg);}"
      

      Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


      Q: What's that?
      A: It's blue light.
      Q: What does it do?
      A: It turns blue.

      1 Reply Last reply
      1
      • mrjjM Offline
        mrjjM Offline
        mrjj
        Lifetime Qt Champion
        wrote on last edited by
        #3

        Hi
        You can using a resource file instead of direct path.
        Works much better later on other pc :)

        Add the res file
        alt text

        Add the image
        alt text

        Take the path
        alt text

        use res path in stylesheet
        alt text

        D 1 Reply Last reply
        3
        • mrjjM mrjj

          Hi
          You can using a resource file instead of direct path.
          Works much better later on other pc :)

          Add the res file
          alt text

          Add the image
          alt text

          Take the path
          alt text

          use res path in stylesheet
          alt text

          D Offline
          D Offline
          DanJenk
          wrote on last edited by
          #4

          @mrjj thankyou very much the image is now showing however its far too big to pick out all the detail how would I resize ity and shrink it down ?

          I plan to use the ui on a 10 inch touchscreen 1280x800

          mrjjM 1 Reply Last reply
          0
          • D DanJenk

            @mrjj thankyou very much the image is now showing however its far too big to pick out all the detail how would I resize ity and shrink it down ?

            I plan to use the ui on a 10 inch touchscreen 1280x800

            mrjjM Offline
            mrjjM Offline
            mrjj
            Lifetime Qt Champion
            wrote on last edited by
            #5

            @DanJenk
            Hi
            I would use an image app and scale it down it to the needed res.
            (1280x800) and see how that looks.

            D 1 Reply Last reply
            1
            • mrjjM mrjj

              @DanJenk
              Hi
              I would use an image app and scale it down it to the needed res.
              (1280x800) and see how that looks.

              D Offline
              D Offline
              DanJenk
              wrote on last edited by
              #6

              @mrjj Thanks again I had resized it in gimp however I forgot to add the new file to resources problemsolved

              J.HilkJ 1 Reply Last reply
              0
              • D DanJenk

                @mrjj Thanks again I had resized it in gimp however I forgot to add the new file to resources problemsolved

                J.HilkJ Offline
                J.HilkJ Offline
                J.Hilk
                Moderators
                wrote on last edited by
                #7

                @DanJenk
                keep in mind, when you modify your resource files, (e.g adding a new picture or editing an existing one)

                make sure to make a clean rebuild of your project, clean -> run qmake -> build, otherwise the changes my not be applied/ files not found


                Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                Q: What's that?
                A: It's blue light.
                Q: What does it do?
                A: It turns blue.

                1 Reply Last reply
                3

                • Login

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