QWidget的边框为何覆盖内容? [英] Why does the border of QWidget cover the contents?

查看:133
本文介绍了QWidget的边框为何覆盖内容?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个从QWidget派生的自定义窗口小部件,该窗口小部件的minimumSize(30, 30),而QLabel作为childWidget:

I have a custom widget derived from QWidget, which has a minimumSize of (30, 30) and a QLabel as a childWidget:

MyWidget::MyWidget (QWidget *parent, QPoint p,
                  QWidget *childWidget) : QWidget (parent)
{
    childWidget = this->childWidget;
    setAttribute (Qt::WA_DeleteOnClose);
    this->move (p);
    verticalLayout = new QVBoxLayout (this);

    if (childWidget != NULL)
    {
        childWidget->setParent (this);
        childWidget->releaseMouse();
        childWidget->setAttribute (Qt::WA_TransparentForMouseEvents,     true);
        verticalLayout->addWidget (childWidget);
    }
    this->setLayout(verticalLayout);
};

MyWidget::mouseMoveEvent (QMouseEvent *e)
{
    if (! (e->button() == Qt::RightButton))
    {
        this->update();
        this->raise();
    }
}

void MyWidget::mouseReleaseEvent (QMouseEvent *evt)
{
    QWidget::mouseReleaseEvent(evt);
    this->update();
}

MyWidget::mousePressEvent (QMouseEvent *e)
{
    if (! (e->button() == Qt::RightButton))
    {

        this->update();
        this->raise();
    }
}

void MyWidget::paintEvent(QPaintEvent *)
{
    QPainter painter(this);
    painter.setRenderHint(QPainter::Antialiasing);
    painter.setPen(Qt::darkGreen);
    painter.drawRect(1, 2, 6, 4);
    painter.setPen(Qt::darkGray);
    painter.drawLine(2, 8, 6, 2);
}

//And some getter/setter methods.

为了给小部件设置边框,我使用以下代码:

In order to set a border to the widget I use the following code:

 customWidget->setStyleSheet("*{border-width:" +
    2 +
    ";border-style:solid;border-color:" +
    #FFFFFF + " ;color:white;}");

它看起来像这样(父窗口小部件的背景为橙色):

It looks like this (the parent widget has an orange background):

.

当我将border-width更改为10时,边框覆盖了内容:

When I change the border-width to 10, the border covers the contents:

两个图像均以最小高度显示小部件.

Both images show the widget in its minimum height.

在我看来,边框似乎是向内应用的.我该怎么修改以使边框朝外,所以对于更大的border-width文本仍然可见?

To me it looks as if the border were applied inwards. What shall I modify to point the border outwards, so for a larger border-width the text remains visible?

推荐答案

原因

边界确实向外:

Cause

The border does go outwards:

您的尺寸有问题. (30, 30)对于此边框来说太小了. 30 - 2*10(最小高度-边框宽度的2倍)等于10.您的字体大于10像素,因此它不适合剩余空间.

You have a problem with the size. (30, 30) is too small for this border. 30 - 2*10 (the minimum height - 2 times the width of the border) equals 10. Your font is larger than 10px, so it does not fit in the remaining space.

您可能想要设置合理的尺寸,例如(100,50).但是,设置最小大小并不灵活,也就是说,它不考虑小部件内容的更改.但是,如果实现了sizeHintminimumSizeHint,则会在

You might want to set a reasonable size, e.g. (100, 50). However, setting the minimum size is not flexible, meaning, that it does not account for changes in the widget's content. If the sizeHint and minimumSizeHint are implemented though, the necessary space will be reported whenever needed, as it is done in QLabel for example.

由于您已经拥有QLabel作为子窗口小部件,因此只需避免设置自定义窗口小部件的minimumSize即可自动计算出正确的大小.

Since you already have a QLabel as a child widget, just avoid setting the minimumSize of your custom widget and the correct size will be calculated automatically.

这篇关于QWidget的边框为何覆盖内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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