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. Background for images with alpha channel
Forum Updated to NodeBB v4.3 + New Features

Background for images with alpha channel

Scheduled Pinned Locked Moved General and Desktop
28 Posts 5 Posters 16.9k Views 1 Watching
  • 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.
  • S Offline
    S Offline
    samkpo
    wrote on last edited by
    #1

    I'm making an image viewer, and i have made it shows animated images, but i can't find the way to set the background for those that have an alpha channel. It's easy to make it with static images, using qpainter, but i don't know how to do it with animated images.

    by the way, i'm learning both Qt and english, so please be patient :-P

    1 Reply Last reply
    0
    • V Offline
      V Offline
      vsorokin
      wrote on last edited by
      #2

      Please, show you code, how you work with QMovie, and I don't understand you need "Alpha background" as in Title or "background for images with alpha channel" as in topic body? :)

      --
      Vasiliy

      1 Reply Last reply
      0
      • D Offline
        D Offline
        dangelog
        wrote on last edited by
        #3

        On a side note, QMovie is perfectly paintable with QPainter (you obtain the current frame with currentImage() or currentPixmap()).

        Software Engineer
        KDAB (UK) Ltd., a KDAB Group company

        1 Reply Last reply
        0
        • S Offline
          S Offline
          samkpo
          wrote on last edited by
          #4

          @QMovie movie = new QMovie (file->absoluteFilePath());
          if (!movie->isValid()) {
          qDebug()<<tr("Could not open %1 image").arg(file->absoluteFilePath());
          return;
          }

          movie->start();
          imageLabel->setMovie(movie);
          imageLabel->resize(movie->scaledSize());@

          that's the code.

          how do you suggest i use QPainter?

          1 Reply Last reply
          0
          • V Offline
            V Offline
            vsorokin
            wrote on last edited by
            #5

            imageLabel has type QLabel, right? QLabel is subclass of QWidget, QWidget is subclass of QPaintDevice.
            Use QPainter for imageLabel

            --
            Vasiliy

            1 Reply Last reply
            0
            • G Offline
              G Offline
              giesbert
              wrote on last edited by
              #6

              So, I think Vass meant to overwrite "paintEvent":http://doc.qt.nokia.com/4.7/qwidget.html#paintEvent and do the painting by yourselve instead of let QImageLabel do the painting for you.

              Nokia Certified Qt Specialist.
              Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

              1 Reply Last reply
              0
              • S Offline
                S Offline
                samkpo
                wrote on last edited by
                #7

                yes, it's a QLabel Object. Ok, i understand what you say but don't know how to use QPainter for imageLabel

                1 Reply Last reply
                0
                • G Offline
                  G Offline
                  giesbert
                  wrote on last edited by
                  #8

                  If you do the painting on your own, you overwrite paint event like the following code:

                  @
                  void myWidgetClass::paintEvent(QPaintEvent*)
                  {
                  QPainter paint(this);
                  paint.drawImage(rect(), myMove.currentImage());
                  }
                  @

                  Nokia Certified Qt Specialist.
                  Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

                  1 Reply Last reply
                  0
                  • V Offline
                    V Offline
                    vsorokin
                    wrote on last edited by
                    #9

                    Gerolf It seems to me what I with you said about different things. As I understand samkpo has movie with alpha channel, and He want set background under this movie. You said about animate background for something with use Qmovie.

                    --
                    Vasiliy

                    1 Reply Last reply
                    0
                    • S Offline
                      S Offline
                      samkpo
                      wrote on last edited by
                      #10

                      yes, that's what i want

                      1 Reply Last reply
                      0
                      • V Offline
                        V Offline
                        vsorokin
                        wrote on last edited by
                        #11

                        what exactly?

                        --
                        Vasiliy

                        1 Reply Last reply
                        0
                        • G Offline
                          G Offline
                          giesbert
                          wrote on last edited by
                          #12

                          Ok, sorry, then I misunderstood....

                          Nokia Certified Qt Specialist.
                          Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

                          1 Reply Last reply
                          0
                          • S Offline
                            S Offline
                            samkpo
                            wrote on last edited by
                            #13

                            i have a movie with alpha channel and i want to set a background under this movie

                            1 Reply Last reply
                            0
                            • G Offline
                              G Offline
                              goetz
                              wrote on last edited by
                              #14

                              This code makes the background red. It's important to call setAutoFillBackground(true) on the label, otherwise the background will not be changed!

                              @
                              QPalette pal = ui->label->palette();
                              pal.setBrush(QPalette::Window, Qt::red);
                              ui->label->setPalette(pal);
                              ui->label->setAutoFillBackground(true);
                              @

                              http://www.catb.org/~esr/faqs/smart-questions.html

                              1 Reply Last reply
                              0
                              • D Offline
                                D Offline
                                dangelog
                                wrote on last edited by
                                #15

                                You need to subclass QLabel (or simply write a new widget from scratch) and in its paintEvent paint the background and then the current QMovie frame; or you can enable autoFillBackground and change the label's background palette. :)

                                Software Engineer
                                KDAB (UK) Ltd., a KDAB Group company

                                1 Reply Last reply
                                0
                                • G Offline
                                  G Offline
                                  giesbert
                                  wrote on last edited by
                                  #16

                                  bq. Vass wrote
                                  Gerolf It seems to me what I with you said about different things. As I understand samkpo has movie with alpha channel, and He want set background under this movie. You said about animate background for something with use Qmovie.

                                  But then you can just paint the background beffore drawing the move:

                                  @
                                  void myWidgetClass::paintEvent(QPaintEvent*)
                                  {
                                  QPainter paint(this);
                                  painter.draw... // draw the background here
                                  paint.drawImage(rect(), myMove.currentImage());
                                  }
                                  @

                                  Or position a label or widget with the needed background exactly below the imageLabel. (so position one widget with setgeometry or make obne label a child of the other one). Or does QLabel not draw the movie with transparent background?

                                  Nokia Certified Qt Specialist.
                                  Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

                                  1 Reply Last reply
                                  0
                                  • G Offline
                                    G Offline
                                    goetz
                                    wrote on last edited by
                                    #17

                                    Gerolf, ok, there's always more than one way to achieve things, but setting the background palette and autoFillBackground should be enough in this case :-)

                                    http://www.catb.org/~esr/faqs/smart-questions.html

                                    1 Reply Last reply
                                    0
                                    • V Offline
                                      V Offline
                                      vsorokin
                                      wrote on last edited by
                                      #18

                                      Gerolf I don't know, I think what enough lay one widget under this. Or you are right, call set background before current frame paint.

                                      --
                                      Vasiliy

                                      1 Reply Last reply
                                      0
                                      • S Offline
                                        S Offline
                                        samkpo
                                        wrote on last edited by
                                        #19

                                        nop, the palette stuff didn't work, when i call setMovie the background is gone, and even calling setBackgroundColor from QMovie doesn't work, so i've done something that fixes my problem, but in a way that isn't the better i guess.

                                        @void ImageViewer::setGifImage()
                                        {
                                        QPixmap m = scuareBackground();
                                        //m is an square 32x32, wich is the base for the alpha background.

                                        QPixmap temp(gifSize);
                                        //create a pixmap qith the same size that the gif image

                                        QPainter q(&temp);
                                        q.drawTiledPixmap(0,0,gifSize.width(), gifSize.height(), m);
                                        q.drawPixmap(0, 0, movie->currentPixmap());
                                        q.end();

                                        imageLabel->setPixmap(temp);
                                        }
                                        @

                                        that's an slot i've made, and it's used in here:

                                        @gifSize=(QPixmap(archivo->absoluteFilePath()).size());

                                        movie->start();
                                        connect(movie, SIGNAL(frameChanged(int)), this, SLOT(setGifImage()));

                                        //imageLabel->setMovie(movie);
                                        imageLabel->resize(gifSize);@

                                        of course i have to make a lot of improvements but it's something.

                                        if you have something to add or correct do it please

                                        1 Reply Last reply
                                        0
                                        • G Offline
                                          G Offline
                                          goetz
                                          wrote on last edited by
                                          #20

                                          Setting background color via palette works for me. I don't know if you want to set only a background color or a background image. The latter would be harder to achieve.

                                          http://www.catb.org/~esr/faqs/smart-questions.html

                                          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