QT使用stackLayout实现层叠布局

QT使用stackLayout实现层叠布局


前言

为了实现层叠布局样式使用stackLayout分离背景和frontend显示,分层显示叠加布局。


一、实现代码如下所示

使用代码实现,布局无法实现只能使用代码实现此布局。

    QPixmap image(":/icons/Icons/Control_Bar/icon_Fire.png");
    QWidget *backgroundWidget = new QWidget;
    QLabel * severaltext = new QLabel;
    QLabel * icon = new QLabel;
    icon->setPixmap(image);
    icon->setFixedSize(42,42);
    QFont severalFont;
    severalFont.setFamily("Honeywell Cond");
    severalFont.setBold(true);
    severalFont.setPixelSize(14);
    severaltext->setText("SEVERAL ALARM");
    severaltext->setStyleSheet("color:rgb(255,255,255)");
    severaltext->setFont(severalFont);
    severaltext->setAlignment(Qt::AlignCenter);

    QHBoxLayout* iconLayout = new QHBoxLayout;
    iconLayout->addItem(new QSpacerItem(40, 20, QSizePolicy::Expanding, QSizePolicy::Minimum));
    iconLayout->addWidget(icon);
    iconLayout->addItem(new QSpacerItem(40, 20, QSizePolicy::Expanding, QSizePolicy::Minimum));

    backgroundWidget->setMinimumSize(QSize(0, 68));
    backgroundWidget->setMaximumSize(QSize(268, 68));
    backgroundWidget->setStyleSheet("background-color:rgba(64,64,64)");
    QVBoxLayout* backLayout = new QVBoxLayout(backgroundWidget);
    backLayout->addLayout(iconLayout);
    backLayout->addWidget(severaltext);
    backLayout->setAlignment(Qt::AlignCenter);
    backLayout->setContentsMargins(0, 0, 0, 0);

    QWidget *frontenddWidget = new QWidget;
    frontenddWidget->setMinimumSize(QSize(0, 68));
    frontenddWidget->setMaximumSize(QSize(268, 68));
    frontenddWidget->setStyleSheet("background-color:rgba(255,255,255,0)");

    QHBoxLayout* frotendLayout = new QHBoxLayout(frontenddWidget);
    m_severalUp   = new QPushButton(this);
    m_severalUp->setMinimumSize(QSize(0, 68));
    m_severalUp->setMaximumSize(QSize(140, 68));
    QIcon upIcon(":/icons/Icons/Control_Bar/Icon_SEVERAL ALARMS_UP.png");
    m_severalUp->setIcon(upIcon);
    m_severalUp->setStyleSheet("color:rgb(255,255,255); padding-right: 70px; border: none;");
    m_severalUp->setFocusPolicy(Qt::NoFocus);

    m_severalDown = new QPushButton(this);
    m_severalDown->setMinimumSize(QSize(0, 68));
    m_severalDown->setMaximumSize(QSize(150, 68));
    m_severalDown->setStyleSheet("color:rgb(255,255,255)");
    QIcon downIcon(":/icons/Icons/Control_Bar/Icon_SEVERAL ALARMS_DOWN.png");
    m_severalDown->setIcon(downIcon);
    m_severalDown->setStyleSheet("color:rgb(255,255,255); padding-left: 70px; border: none;");
    m_severalDown->setFocusPolicy(Qt::NoFocus);

    frotendLayout->addWidget(m_severalUp);
    frotendLayout->addWidget(m_severalDown);
    frotendLayout->setContentsMargins(0, 0, 0, 0);
    frotendLayout->setSpacing(8);
    frotendLayout->setAlignment(Qt::AlignCenter);

    QStackedLayout * stackLayout = new QStackedLayout;
    stackLayout->addWidget(backgroundWidget);
    stackLayout->addWidget(frontenddWidget);
    stackLayout->setStackingMode(QStackedLayout::StackAll);

    m_severalMainLayout= new QVBoxLayout;
    m_severalMainLayout->addLayout(stackLayout);
    setLayout(m_severalMainLayout);

可以将其代码加入新建的工程显示,运行可以显示处两个widget叠加的效果。

总结

此内容是一次应用背景叠加的实现记录,作为后续遇到需要实现此功能的方案可作为参考。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值