2014-04-16 26 views
7

Sto usando PySide 1.2.1 con Python 2.7 e ho bisogno di un widget per disegnare uno sfondo colorato. In Qt Designer ho creato una semplice finestra composta da un'etichetta, un widget contenente altri tre elementi e un'altra etichetta. Per il widget contenente il pulsante, il pulsante di opzione e la casella di controllo, ho impostato la proprietà styleSheet su background-color: #FFFFFF. In Qt Designer tutto rende come desiderato:PySide: QWidget non disegna il colore di sfondo

Window in Qt Designer

Ma nel PySide il widget non disegnare il colore di sfondo - ma le voci su di esso eredita il colore in modo corretto:

Window in PySide

Ecco l'interfaccia utente-XML:

<?xml version="1.0" encoding="UTF-8"?> 
<ui version="4.0"> 
<class>MainWindow</class> 
<widget class="QMainWindow" name="MainWindow"> 
    <property name="geometry"> 
    <rect> 
    <x>0</x> 
    <y>0</y> 
    <width>276</width> 
    <height>133</height> 
    </rect> 
    </property> 
    <property name="windowTitle"> 
    <string>MainWindow</string> 
    </property> 
    <widget class="QWidget" name="centralwidget"> 
    <layout class="QVBoxLayout" name="verticalLayout" stretch="0,1,1"> 
    <item> 
    <widget class="QLabel" name="label"> 
     <property name="text"> 
     <string>The following should have white background:</string> 
     </property> 
    </widget> 
    </item> 
    <item> 
    <widget class="QWidget" name="widget" native="true"> 
     <property name="styleSheet"> 
     <string notr="true">background-color: #FFFFFF</string> 
     </property> 
     <layout class="QHBoxLayout" name="horizontalLayout"> 
     <item> 
     <widget class="QPushButton" name="pushButton"> 
     <property name="text"> 
      <string>PushButton</string> 
     </property> 
     </widget> 
     </item> 
     <item> 
     <widget class="QRadioButton" name="radioButton"> 
     <property name="text"> 
      <string>RadioButton</string> 
     </property> 
     </widget> 
     </item> 
     <item> 
     <widget class="QCheckBox" name="checkBox"> 
     <property name="text"> 
      <string>CheckBox</string> 
     </property> 
     </widget> 
     </item> 
     </layout> 
    </widget> 
    </item> 
    <item> 
    <widget class="QLabel" name="label_2"> 
     <property name="text"> 
     <string>But it hasn't :-(</string> 
     </property> 
    </widget> 
    </item> 
    </layout> 
    </widget> 
    <widget class="QMenuBar" name="menubar"> 
    <property name="geometry"> 
    <rect> 
    <x>0</x> 
    <y>0</y> 
    <width>276</width> 
    <height>18</height> 
    </rect> 
    </property> 
    </widget> 
    <widget class="QStatusBar" name="statusbar"/> 
</widget> 
<resources/> 
<connections/> 
</ui> 

Ecco il mio codice Python doi ng niente di speciale:

import sys 

from PySide import QtCore, QtGui 

from generated.test import Ui_MainWindow 

class MainWindow(Ui_MainWindow,QtCore.QObject): 

    def __init__(self, *args, **kwargs): 
     Ui_MainWindow.__init__(self, *args, **kwargs) 
     QtCore.QObject.__init__(self) 

    def setupUi(self, MainWindow): 
     Ui_MainWindow.setupUi(self, MainWindow) 

def main(argv): 
    app = QtGui.QApplication(argv) 
    mainwindow = QtGui.QMainWindow() 

    ui = MainWindow() 
    ui.setupUi(mainwindow) 

    mainwindow.show() 
    sys.exit(app.exec_()) 

if __name__ == "__main__": 
    main(sys.argv) 

ho già provato self.widget.setAutoFillBackground(True), ma secondo the documentation questa proprietà è disabilitata in ogni caso, non appena c'è un valore styleSheet valido per lo sfondo.

Questo non funziona così:

p = self.widget.palette() 
p.setColor(self.widget.backgroundRole(), QtCore.Qt.white) 
self.widget.setPalette(p) 

(Got questi suggerimenti da How to set QWidget background color?)

come posso ottenere il widget per disegnare il colore di sfondo bianco?

risposta

8

impostare l'attributo WA_StyledBackground sul widget contenitore:

ui = MainWindow() 
ui.setupUi(mainwindow) 
ui.widget.setAttribute(QtCore.Qt.WA_StyledBackground, True) 
+0

Grazie mille! Questo funziona. – Robert

+0

Qualcosa che non capisco è che l'attributo 'WA_StyledBackground' non è impostato (metodo' testAttribute() ') sia per' QWidget() 'che' QLabel' ma il primo ne ha bisogno per disegnare lo sfondo in stile e il secondo no ? (L'ho appena testato.) – Trilarion