Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Diskplaying a background image in main window.



  • 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


  • Moderators

    hi @DanJenk and welcome,

    try it with the following

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

  • Lifetime Qt Champion

    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



  • @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


  • Lifetime Qt Champion

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



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


  • Moderators

    @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


Log in to reply