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. set background image does not work
Forum Updated to NodeBB v4.3 + New Features

set background image does not work

Scheduled Pinned Locked Moved Solved General and Desktop
8 Posts 4 Posters 7.8k 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.
  • Q Offline
    Q Offline
    qtpi
    wrote on last edited by
    #1

    hi folks.

    so i have image.png in my source file. and the following code does not work ...what did i do wrong?

    this->setStyleSheet("QWidget{background-image: url(:/image.png);}");

    Ni.SumiN raven-worxR 2 Replies Last reply
    0
    • RatzzR Offline
      RatzzR Offline
      Ratzz
      wrote on last edited by Ratzz
      #2

      @qtpi
      have you tried

      this->setStyleSheet("background-image: url(:/image.png);");
      

      http://stackoverflow.com/questions/4458201/unable-to-set-the-background-image-in-qt-stylesheet
      http://stackoverflow.com/questions/22109342/background-image-in-qt-stylesheet-doesnt-work

      --Alles ist gut.

      1 Reply Last reply
      1
      • Q qtpi

        hi folks.

        so i have image.png in my source file. and the following code does not work ...what did i do wrong?

        this->setStyleSheet("QWidget{background-image: url(:/image.png);}");

        Ni.SumiN Offline
        Ni.SumiN Offline
        Ni.Sumi
        wrote on last edited by
        #3

        @qtpi
        this->setStyleSheet("QWidget{background-image: url(:/image.png);}");
        Works fine.
        For Test I did this, and worked from me.
        MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this); this->setStyleSheet("QWidget{background-image: url(:images/image1.png);}");; }

        Check the path , Size and exact name of the image. In my case , I have "image1.png" in the "images" folder. But if you are using QWidget mean you are setting it to all widgets under "this".

        And Importantly, after adding images to the resource file, please don't forget to run again the "Run qmake" , build and run.

        1 Reply Last reply
        2
        • Q qtpi

          hi folks.

          so i have image.png in my source file. and the following code does not work ...what did i do wrong?

          this->setStyleSheet("QWidget{background-image: url(:/image.png);}");

          raven-worxR Offline
          raven-worxR Offline
          raven-worx
          Moderators
          wrote on last edited by
          #4

          @qtpi
          If the path is correct then the question is, what type of widget are you setting the stylesheet on?

          --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
          If you have a question please use the forum so others can benefit from the solution in the future

          Q 1 Reply Last reply
          2
          • raven-worxR raven-worx

            @qtpi
            If the path is correct then the question is, what type of widget are you setting the stylesheet on?

            Q Offline
            Q Offline
            qtpi
            wrote on last edited by
            #5

            @raven-worx
            simply a normal sub-classed qwidget. I subclassed the qwidget twice.
            seems still not working.

            but when I tried to simply run this code on a empty qwidget, it seems to work. I suppose something is wrong with my sub-classed widget.

            raven-worxR 1 Reply Last reply
            0
            • Q qtpi

              @raven-worx
              simply a normal sub-classed qwidget. I subclassed the qwidget twice.
              seems still not working.

              but when I tried to simply run this code on a empty qwidget, it seems to work. I suppose something is wrong with my sub-classed widget.

              raven-worxR Offline
              raven-worxR Offline
              raven-worx
              Moderators
              wrote on last edited by
              #6

              @qtpi
              Qt docs say about QWidget:

              Supports only the background, background-clip and background-origin properties.
              If you subclass from QWidget, you need to provide a paintEvent for your custom QWidget as below:
              
              void CustomWidget::paintEvent(QPaintEvent *)
              {
                  QStyleOption opt;
                  opt.init(this);
                  QPainter p(this);
                  style()->drawPrimitive(QStyle::PE_Widget, &opt, &p, this);
              }
              
              The above code is a no-operation if there is no stylesheet set.
              Warning: Make sure you define the Q_OBJECT macro for your custom widget.
              

              --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
              If you have a question please use the forum so others can benefit from the solution in the future

              Q 1 Reply Last reply
              2
              • raven-worxR raven-worx

                @qtpi
                Qt docs say about QWidget:

                Supports only the background, background-clip and background-origin properties.
                If you subclass from QWidget, you need to provide a paintEvent for your custom QWidget as below:
                
                void CustomWidget::paintEvent(QPaintEvent *)
                {
                    QStyleOption opt;
                    opt.init(this);
                    QPainter p(this);
                    style()->drawPrimitive(QStyle::PE_Widget, &opt, &p, this);
                }
                
                The above code is a no-operation if there is no stylesheet set.
                Warning: Make sure you define the Q_OBJECT macro for your custom widget.
                
                Q Offline
                Q Offline
                qtpi
                wrote on last edited by
                #7

                @raven-worx O.o I read the code, still did not understand why it works. But it did work.
                Thanks for the help!

                raven-worxR 1 Reply Last reply
                0
                • Q qtpi

                  @raven-worx O.o I read the code, still did not understand why it works. But it did work.
                  Thanks for the help!

                  raven-worxR Offline
                  raven-worxR Offline
                  raven-worx
                  Moderators
                  wrote on last edited by
                  #8

                  @qtpi said:

                  I read the code, still did not understand why it works.

                  why? The style draws the widget's "basics".
                  For other widgets this isn't needed, because they are more complex and thus are drawn via the style anyway.

                  --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
                  If you have a question please use the forum so others can benefit from the solution in the future

                  1 Reply Last reply
                  0

                  • Login

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