Qt FlowLayout升级版

@[toc]

简述

最近因为工作需要,在flowlayout流布局上改进了下。专业词语不知道怎么称呼,暂且称为紧凑型布局吧。看看效果图

效果图

1.gif

核心代码

int itemSize = itemList.size();
    for (int i = 0; i < itemSize; i++) {
        QWidget *wid = itemList[i]->widget();
        int spaceX = horizontalSpacing();
        if (spaceX == -1)
            spaceX = wid->style()->layoutSpacing(
                QSizePolicy::PushButton, QSizePolicy::PushButton, Qt::Horizontal);
        int spaceY = verticalSpacing();
        if (spaceY == -1)
            spaceY = wid->style()->layoutSpacing(
                QSizePolicy::PushButton, QSizePolicy::PushButton, Qt::Vertical);
//! [10]
//! [11]
//!

        if(itemList[i]->sizeHint().height() == 0) continue;
        int nextX = x + itemList[i]->sizeHint().width() + spaceX;
        int nextY = y;
        if(x > effectiveRect.x()){
            nextX = x;
            nextY = y + itemList[i]->sizeHint().height() + spaceY;

            if(nextY - spaceY >= preY + lineHeight && lineHeight > 0 && lineWidth > 0){
                x = x + lineWidth + spaceX;
                y = preY;
                nextX = x;
                nextY = y + itemList[i]->sizeHint().height() + spaceY;
                lineWidth = 0;
            }
            lineWidth = qMax(lineWidth, itemList[i]->sizeHint().width());
        }

        if (nextX + itemList[i]->sizeHint().width() > effectiveRect.right() && lineHeight > 0) {
            x = effectiveRect.x();
            y = preY + lineHeight + spaceY;
            preY = y;
            nextX = x + itemList[i]->sizeHint().width() + spaceX;
            nextY = y;
            lineHeight = 0;
            lineWidth = 0;
        }

        if (!testOnly)
        {
            itemList[i]->setGeometry(QRect(QPoint(x, y), itemList[i]->sizeHint()));
        }

        lineHeight = qMax(lineHeight, itemList[i]->sizeHint().height());
        x = nextX;
        y = nextY;
    }

QQ群

Qt交流大会 853086607 (收费群,用于后期升级群费用) 在这里插入图片描述

结尾

不定期上传新作品,解答群中作品相关问题。相关外,能解答则解答。欢迎大家一起探索Qt世界!相关工程,可以联系博主雨田哥:3246214072


标签:Qt C++ Python



开发PC客户端,服务于金融行业 37091473(付费咨询)
沪ICP备14050191号 Copyright © 多多指教社区(群:312125701)
Tobyyi's QtQuick Example | Powered by NoderCMS