Qt边框,透明度和填充.如何产生这种效果? [英] Qt Border, transparency, and padding. How to create this effect?

查看:529
本文介绍了Qt边框,透明度和填充.如何产生这种效果?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想添加一个右上角的按钮来关闭浮动在其他控件之上的小部件. 我无法使其浮出面板的内容.

I want to add a top right button for closing a widget that floats over others. I can´t make it float out of the contents of the panel.

我尝试了几种方法.具有背景通常是行不通的.我无法使小部件漂浮在该背景内的框外.

I tried several ways. Having the background normally doesn´t work. I cant make a widget float outside the box inside that background.

方法是这样的:

border-style: solid;
border-width: 12px 24px 37px 25px;
border-image: url(:/resources/images/panel_border_corner_btn.png) 12 24 37 25 fill repeat;
margin: 0px;
padding: 0px;

所以我试图制作一个在顶部和右侧具有10像素的透明背景,就像这样(您不会看到透明区域,但是如果下载它,则会看到它).

So i tried to make a background with 10 px on the top and right sides transparents, like this (you won´t see the transparent zones, but if you download it, you will see it).

所以我添加了带有按钮背景的小部件:

so i add the widget with the button background:

但是我还是不能让它溢出边界.用-20修改边距会切掉它,而使用填充-20则行不通...

But i can´t make it overflow the border anyways. Modifying the margin with -20 cuts it, using padding -20 doesn´t work...

推荐答案

这是我的示例:

FloatPanel::FloatPanel(QWidget *parent) :
QWidget(parent)
{
setAutoFillBackground(true);

QVBoxLayout *layout = new QVBoxLayout(this);
layout->setContentsMargins(5, 15, 15, 5);
layout->addWidget(new QLabel("some text"));
layout->addWidget(new QPushButton("some button"));
}

void
FloatPanel::paintEvent(QPaintEvent *event)
{
    Q_UNUSED(event);

    QPainter painter(this);

    painter.setPen(Qt::black);
    painter.setBrush(Qt::white);

    painter.drawRect(0, 10, width() - 12, height() - 11);

    painter.drawPixmap(width() - 38, 0, QPixmap(":/close.png"));
 }

这是结果:

这篇关于Qt边框,透明度和填充.如何产生这种效果?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆