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. Rotating QPixmap rendered in a Delegate
QtWS25 Last Chance

Rotating QPixmap rendered in a Delegate

Scheduled Pinned Locked Moved General and Desktop
8 Posts 2 Posters 2.5k 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.
  • L Offline
    L Offline
    linuxkid
    wrote on last edited by
    #1

    Hey all, I'm trying to develop a rotating QPixmap that is rendered in a delegate like so:
    [code]
    void DelegadoListView1::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
    {
    //pintor = painter;
    if (option.state & QStyle::State_Selected)
    {
    painter->fillRect(option.rect, option.palette.color(QPalette::Highlight));
    }
    QString texto = index.data(Qt::DisplayRole).toString();
    QRect r = option.rect.adjusted(2,2,-2,-2);
    icono1.paint(painter,r,Qt::AlignVCenter|Qt::AlignLeft);
    r = r.adjusted(r.height()+20,0,0,0);
    painter->drawText(r.left(),r.top(),r.width(),r.height(),Qt::AlignVCenter|Qt::AlignLeft|Qt::TextWordWrap,texto,&r);

    }
    [/code]

    icono1 is a QIcon, and the function takes a QModelIndex whose first index is text.
    I want to rotate the QIcon, but how can I do so? I've tried tying a timer and rotating the pixmap on timeout but the image deforms,
    How should I go about it?

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi and welcome to devnet,

      You should take a look at the rotate function from QPainter

      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
      • L Offline
        L Offline
        linuxkid
        wrote on last edited by linuxkid
        #3

        I've tried but it doesn't rotate the QIcon from its center, also, how can I call the paint() event from the delegate from a QTimer timeout() event?
        Since I require it to move like if it was a gif, constantly spinning...

        EDIT: Also, the image distorts itself when rotated (not scaled, just rotated, even if in 1 degree) how can I prevent that?

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Why a QIcon ? Why not use a QPixmap ?

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

          L 1 Reply Last reply
          0
          • SGaistS SGaist

            Why a QIcon ? Why not use a QPixmap ?

            L Offline
            L Offline
            linuxkid
            wrote on last edited by
            #5

            @SGaist
            Indeed, I've tried already, also turned on atialiasing just in case but it still gets rendered awfully when rotated.
            It doesn't look like the image at all..

            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              Can you share the latest version of your code ?

              As for the transformation part, did you took a look at the Coordinate Transformations doc ?

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

              L 1 Reply Last reply
              0
              • SGaistS SGaist

                Can you share the latest version of your code ?

                As for the transformation part, did you took a look at the Coordinate Transformations doc ?

                L Offline
                L Offline
                linuxkid
                wrote on last edited by
                #7

                @SGaist
                Latest version of the code is just this:
                ´´´
                void DelegadoListView1::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
                {
                //pintor = painter;
                if (option.state & QStyle::State_Selected)
                {
                painter->fillRect(option.rect, option.palette.color(QPalette::Highlight));
                }
                painter->setRenderHint(QPainter::Antialiasing);
                QString texto = index.data(Qt::DisplayRole).toString();
                QRect r = option.rect.adjusted(2,2,-2,-2);
                if (index.data(Qt::DisplayRole).toString().contains("2"))
                {

                    QPixmap pixmap = icono1.pixmap(32,32);
                    QMatrix matriz ;
                    matriz = matriz.translate(pixmap.width()/2,pixmap.height()/2);
                    matriz.rotate(angulo);
                
                    pixmap = pixmap.transformed(matriz);
                    pixmap = pixmap.scaled(64,64);
                    QIcon icono2 = QIcon(pixmap);
                
                    icono2.paint(painter,r,Qt::AlignVCenter|Qt::AlignLeft);
                }
                else
                    icono1.paint(painter,r,Qt::AlignVCenter|Qt::AlignLeft);
                r = r.adjusted(r.height()+20,0,0,0);
                painter->drawText(r.left(),r.top(),r.width(),r.height(),Qt::AlignVCenter|Qt::AlignLeft|Qt::TextWordWrap,texto,&r);
                

                }

                QSize DelegadoListView1::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
                {
                return QSize(200,52);
                }

                void DelegadoListView1::actualizar()
                {
                QPixmap pixmap = icono2.pixmap(32,32);
                matriz = matriz.translate(pixmap.width()/2,pixmap.height()/2);
                matriz.rotate(angulo++);
                pixmap = pixmap.transformed(matriz);
                pixmap = pixmap.scaled(64,64);
                icono2 = QIcon(pixmap);
                emit actualizarWidget();
                }
                ´´´

                Right now, the Qtimer is tied to the parent widget wich upon timeout() will trigger an update(); so that it's redrawn.
                Even when it is working and it does "spin", it gets seriously deformed up until the point where it's spun 360 degrees and shows itself as the first frame

                1 Reply Last reply
                0
                • SGaistS Offline
                  SGaistS Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  Again, why use a QIcon when you can draw the pixmap directly ?

                  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

                  • Login

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