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

QFlipWidget -- what did I break?



  • I need a way to flip an image to reveal another image on its back -- like flipping a playing card -- for a Qt application I'm writing.

    I found some example code under the name QFlipWidget (source available here), which provides exactly the effect I want.

    However, I don't need or want all the several layers of nested widgets and layouts and whatnot, so I decided to modify the code by stripping it down to just what I need - removing layouts, QGraphicsProxyWidgets, the RoundedRect class entirely, etc. Since I'm looking for just a pair of images (from jpg files or whatever), I figured I didn't need anything more than a couple of QGraphicsPixmapItems to put in the QGraphicsScene and it should work the same.

    After making these changes, my images were side-by-side and the axis of rotation was the line of intersection between the two images, instead of "stacked" on top of each other as they appear to be in QFlipWidget. No problem, I thought, so I set the images' offset so they occupy the same space, but now I have the problem that the front image stays on top always -- after being flipped I merely see the front imaged reverse. During the animation I can see a portion of the back image so I know it's there, just not being rendered over the front image.

    I assume that some part of the old code caused the visibility flag to become false around the halfway point of the animation, but for the life of me I can't figure out where that is in the code (or in inherited classes) or if that's even the correct mechanism.

    So, I have two questions:

    In the original QFlipWidget, how is the visibility of the item which is on the not-presently-displayed side handled? That is, what causes the back image to come around on the second half of the rotation transform, instead of just flipping the front image around?

    How can I achieve this effect absent whatever mechanism I stripped in the process of removing the extraneous stuff?

    As far as I can see, this is the only substantive/relevant portion of changed code (it's from the constructor of the FlipWidget class):

    //--Front Widget
    QString frontPath = "/path/to/image1.jpg";
    m_frontPixmap = new QPixmap(frontPath);
    m_frontItem = new QGraphicsPixmapItem(QPixmap(frontPath));

    //Create the scene and add the items
    m_scene = new QGraphicsScene(-300,0,600,600, this);
    this->setScene(m_scene);
    m_scene->addItem(m_frontItem);

    //--BackWidget
    QString backPath = "/path/to/image2.jpg";
    m_backPixmap = new QPixmap(backPath);
    m_backItem = new QGraphicsPixmapItem(QPixmap(backPath));
    m_backItem->setTransform(QTransform().rotate(180, Qt::YAxis));
    m_backItem->setParentItem(m_frontItem);
    m_backItem->setOffset(-300,0);

    //--Widget size
    this->setMaximumWidth(598);


  • Qt Champions 2017

    Difficult to answer this question as this issue is not directly related to Qf. If somebody has used the flip widget will help. Otherwise you may check with author QFlipWidget itself.



  • Hi,
    @marshaul said in QFlipWidget -- what did I break?:

    I assume that some part of the old code caused the visibility flag to become false around the halfway point of the animation

    In "RoundRectItem.cpp" file at line 38, there is a logic that hides the backward widget.

        if( m_proxyWidget && m_proxyWidget->isVisible() ) 
            {
                m_proxyWidget->hide();
                m_proxyWidget->setGeometry( rect() );
            }
            return;
    

    So I think you may create a class that is similar to RoundRectItem but only for checking and setting the visibility and geometry.



  • @CKurdu Oh, how did I miss that?

    Good catch, I guess I wasn't paying enough attention to RoundRectItem, which I should have because it was one of the things I wanted to strip.

    Thank you very much. I'd like to say that I posted this on stackexchange and had the question promptly closed by a bunch of CSS wranglers based on some non-sequitur justification. So thanks for actually helping! :)



  • @marshaul
    You are welcome. If the answer really solves the problem, you can mark as solved from the topic tools.


Log in to reply