2014-04-16 9 views
7

मैं पायथन 2.7 के साथ पायसाइड 1.2.1 का उपयोग कर रहा हूं और मुझे रंगीन पृष्ठभूमि खींचने के लिए एक विजेट की आवश्यकता है। क्यूटी डिजाइनर में मैंने एक साधारण खिड़की बनाई जिसमें एक लेबल, एक विजेट जिसमें तीन अन्य आइटम और एक अन्य लेबल शामिल था। विजेट, रेडियो बटन और चेकबॉक्स युक्त विजेट के लिए मैंने styleSheet संपत्ति background-color: #FFFFFF पर सेट की है। क्यूटी डिजाइनर सब कुछ renders वांछित के रूप में:पायसाइड: QWidget पृष्ठभूमि रंग नहीं खींचता

Window in Qt Designer

लेकिन Pyside में विजेट आकर्षित नहीं करता है पृष्ठभूमि रंग - लेकिन उस पर वस्तुएँ सही रंग के वारिस:

Window in PySide

यहाँ यूई-एक्सएमएल:

<?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> 

यहां मेरा पायथन कोड डोई है एनजी कुछ भी नहीं विशेष:

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) 

मैं पहले से ही self.widget.setAutoFillBackground(True) की कोशिश की है, लेकिन वहाँ पृष्ठभूमि के लिए एक वैध स्टाइल शीट मूल्य के रूप में the documentation के अनुसार इस संपत्ति वैसे भी रूप में जल्द ही अक्षम है।

यह रूप में अच्छी तरह से काम नहीं करता:

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

मैं विजेट सफेद पृष्ठभूमि रंग आकर्षित करने के लिए कैसे प्राप्त कर सकते हैं (How to set QWidget background color? से इन संकेतों समझे)?

उत्तर

8

कंटेनर विजेट पर WA_StyledBackground विशेषता सेट करें:

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

आपको बहुत बहुत धन्यवाद! यह काम। – Robert

+0

कुछ मुझे समझ में नहीं आता है कि विशेषता 'WA_StyledBackground' सेट नहीं है (विधि' testAttribute() ') दोनों QWidget()' और 'QLabel' के लिए) लेकिन पहली बार स्टाइल पृष्ठभूमि को चित्रित करने के लिए सेट की गई है और दूसरा नहीं ? (बस इसका परीक्षण किया।) – Trilarion

संबंधित मुद्दे