为什么QWidget的边境和背景图像的造型表现来的QLabel,QDialog的不同,...? [英] Why is QWidget with border and background image styling behaving different to QLabel, QDialog, ...?

查看:382
本文介绍了为什么QWidget的边境和背景图像的造型表现来的QLabel,QDialog的不同,...?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用的是Qt4.8的Python(在Windows上的Python 3.3版本1.2.2)结合PySide我看到 QWidget的表现不同QWidget的衍生部件如 QDialog的的QLabel 的QMainWindow 与样式表样式时。

I use the Qt4.8 Python binding PySide (version 1.2.2 on Python 3.3 on Windows) and I see that QWidget behaves different from QWidgetderived widgets like QDialog, QLabel or QMainWindow when styling with stylesheets.

在特定我观察到属性 WA_StyledBackground 必须设置为显示一个边界或在一个背景色独立设置的背景图像。在对其他部件的对比这个属性没有影响,边框或背景图像总是显示。重写的paintEvent 方法也是可能的。

In particular I observe that the attribute WA_StyledBackground must be set to show a border or a background image while a background color is set independently. In contrast for the other widgets this attribute has no influence, a border or background image is always shown. Overriding the paintEvent method is also a possibility.

举例code:

from PySide import QtGui, QtCore

class MyWidget(QtGui.QWidget):

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)

    def paintEvent(self, event):
        opt = QtGui.QStyleOption()
        opt.initFrom(self)
        painter = QtGui.QStylePainter(self)
        painter.drawPrimitive(QtGui.QStyle.PE_Widget, opt)

def display(w):
    print(w.__class__.__name__)
    w.setWindowTitle(w.__class__.__name__)
    print(' WA_StyledBackground is {}'.format(w.testAttribute(QtCore.Qt.WA_StyledBackground)))
    print(' autoFillBackground is {}'.format(w.autoFillBackground()))
    w.setStyleSheet('border: 4px inset #515c84; border-radius: 9px; background-color: blue; background-image: url(test.png);')
    w.resize(400, 400)
    w.show()

app = QtGui.QApplication([])

w1 = QtGui.QMainWindow(flags=QtCore.Qt.Window)
display(w1) # works

w2 = QtGui.QDialog(f=QtCore.Qt.Window)
display(w2) # works

w3 = QtGui.QWidget(f=QtCore.Qt.Window)
w3.setAttribute(QtCore.Qt.WA_StyledBackground)
display(w3) # works only with the previous line uncommented

w4 = QtGui.QLabel('Text', f=QtCore.Qt.Window)
display(w4) # works

w5 = MyWidget(f=QtCore.Qt.Window)
display(w5) # works

app.exec_()

这话题已经在计算器上部分讨论。在接受的答案<一个href=\"http://stackoverflow.com/questions/18257177/borders-and-background-of-qwidget-arent-set-by-stylesheet\">Borders和QWidget的背景不被样式表建议重写的paintEvent 设置的如何设置QWidget的背景色?建议设置 setAutoFillBackground 的情况下,一个使用的调色板, <一href=\"http://stackoverflow.com/questions/23104051/pyside-qwidget-does-not-draw-background-color\">PySide: QWidget的不画背景色建议对刚刚设置上述 WA_StyledBackground 。现在缺少的是对衍生部件 QWidget的之间的差异及原因。

This topic is already discussed partly on StackOverflow. The accepted answer on Borders and background of QWidget aren't set by stylesheet suggests overriding paintEvent, the accepted answer of How to set QWidget background color? suggests to set setAutoFillBackground in case one uses the palette and PySide: QWidget does not draw background color suggests to just set the above mentioned WA_StyledBackground. What is missing is the reason for the differences between QWidget and derived widgets.

我发现这些差异有点混乱:

I find these differences a bit confusing:


  • 为什么 QWidget的行为源于部件如的QLabel ...
  • 不同
  • 为什么 QWidget的在自然状态下不显示边框或背景图片,但它确实diplay背景颜色?

  • 有没有设置属性 WA_StyledBackground ,并在所描述的方式overridding 的paintEvent 之间的差异?哪种方法将是preferred?

  • Why is QWidget behaving different from derived widgets like QLabel,...?
  • Why can QWidget in the natural state not display borders or background images but it does diplay background colors?
  • Is there a difference between setting attribute WA_StyledBackground and overridding paintEvent in the described way? Which method would be preferred?

推荐答案

TL;博士 - 使用 QFrame

tl;dr - Use QFrame

我不打算在那个走进这个设计决定的推理猜测,但一般来说 QWidget的被认为是无国界的。如果你希望你的小部件有很好的风格的边界,那么你或许应该使用一个 QFrame ,它本质上只是一个 QWidget的与您正在寻找的边框造型能力。

I won't attempt to guess at the reasoning that went into this design decision, but generally speaking QWidget is considered to be borderless. If you want your widget to have nice styled borders, then you should probably be using a QFrame, which essentially just a QWidget with the border styling capabilities that you're looking for.

您会注意到,很多小部件(如的QLabel 你提到)实际上从 QFrame 而不是直接从 QWidget的

You'll note that a lot of widgets (such as the QLabel you've mentioned) actually inherit from QFrame rather than directly from QWidget.

我不能在问候了 WA_StyledBackground 属性权威说话。

I can't speak authoritatively in regards to the WA_StyledBackground property.

这篇关于为什么QWidget的边境和背景图像的造型表现来的QLabel,QDialog的不同,...?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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