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

Qt5 - CPP - How to resize a QScrollList



  • I have the following problem.

    I have scroll list of widgets which is looking like this (if it's expended on X way too much)
    enter image description here

    If I will scale it on Y-axis it's going to look like this.

    enter image description here

    I'm trying to make those blue widgets to keep their aspect ratio, doesn't matter the size of the container. In order to do that I've reimplemented the resizeEvent like this:

    void PolygonButtonWidget::resizeEvent(QResizeEvent *event) {
        auto ratioW = 8;
        auto ratioH = 3;
        float thisAspectRatio = (float) event->size().width() / event->size().height();
        if (thisAspectRatio < ratioW / ratioH) {
            auto w = event->size().height() * ratioW / ratioH;
            auto h = event->size().height();
            qDebug() << hasHeightForWidth() << " " << w << " " << h;
    
            this->resize(w, h);
            if (m_nrButtons != 0) {
                this->move((w + 20) * m_nrButtons, this->y());
            }
        }
        QWidget::resizeEvent(event);
    }
    

    By doing this my list is going to look something like this

    enter image description here

    or like this

    enter image description here

    How can I fix this? For some reason, some of my widgets are going to disappear, what should be my approach in order to fix this issue?



  • @Vildnex said in Qt5 - CPP - How to resize a QScrollList:
    I don't know about the answer you are seeking, but while you are waiting for someone else to answer:

    auto ratioW = 8;
    auto ratioH = 3;
    float thisAspectRatio = (float) event->size().width() / event->size().height();
    if (thisAspectRatio < ratioW / ratioH) {
    

    That ratioW / ratioH looks like an integer division to me?


Log in to reply