Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 721739
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T05:54:30+00:00 2026-05-14T05:54:30+00:00

I am having an issue with PyQt4. I want to create a new widget

  • 0

I am having an issue with PyQt4. I want to create a new widget within a window, and I want this widget to have a custom color.

When i create a subclass of the QWidget class, and instantiate it, I am not able to change its background color through the setStyleSheet() function.

When I instantiate a new QWidget object, I have no problems in changing its background color. But i dont want an ordinary QWidget object.I want to create my own subclass of QWidget.

When i create a subclass of a QPushButton, I am also able to change its background color using the setStyleSheet() function.

There are no error messages or warnings in the console window, it just refuses to work properly without any indication as to why.

So what i would like to know is why is it that i can change the background color of a widget if i simply create a QWidget object, or a subclass of QPushButton, but not when i create a subclass of QWidget. And how can i therefore change the background color of an object that is a subclass of QWidget?

Is it maybe something specific to the version of python or PyQt that i am using? Is it a bug in the library? or some flaw in the way that i am writing my code?

I am using python 2.6.4 and PyQt4

Below is an example of the code that leads me to trouble. There are three widgets within the window one below the other. The parent widget is set with background color of green. The top widget is set to red, the middle one, is the subclass of QWidget, which should be blue, but it appears invisible because it takes on the color of the parent window for some reason. and the bottom widget is a subclass of QPushButton and is white.

import sys
from PyQt4 import QtGui, QtCore


################################################################################
#--------------------------------------------------------- CUSTOM WIDGET CLASS 1
class CustomWidget(QtGui.QWidget):
    def __init__(self, parent):
        QtGui.QWidget.__init__(self, parent)
        # some custom properties and functions will follow


################################################################################
#--------------------------------------------------------- CUSTOM WIDGET CLASS 2
class CustomWidget2(QtGui.QPushButton):
    def __init__(self, parent):
        QtGui.QPushButton.__init__(self, parent)
        # some custom properties and functions will follow


################################################################################
#----------------------------------------------------------- PARENT WIDGET CLASS
class Parent(QtGui.QWidget):
    def __init__(self, parent=None):
        #---------------------------------------------------------- SETUP WINDOW
        QtGui.QWidget.__init__(self, parent)
        self.resize(500, 340)
        self.setStyleSheet("QWidget {background-color: #00FF00}")

        #-------------------------------------------------- SETUP DEFAULT WIDGET
        wid1 = QtGui.QWidget(self)
        wid1.setGeometry(10, 10, 480, 100)
        wid1.setStyleSheet("QWidget {background-color: #FF0000 }")

        #------------------------------------------------- SETUP CUSTOM WIDGET 1
        wid2 = CustomWidget(self)
        wid2.setGeometry(10, 120, 480, 100)
        wid2.setStyleSheet("QWidget {background-color: #0000FF }")


        #------------------------------------------------- SETUP CUSTOM WIDGET 2
        wid3 = CustomWidget2(self)
        wid3.setGeometry(10, 230, 480, 100)
        wid3.setStyleSheet("QWidget {background-color: #FFFFFF }")


################################################################################
#-------------------------------------------------------------------------- MAIN
app = QtGui.QApplication(sys.argv)
win = Parent()
win.show()
app.exec_()
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T05:54:30+00:00Added an answer on May 14, 2026 at 5:54 am

    Well, I discovered a solution, I dont know if its the best one or not, so if anyone else has any suggestions, please leave a comment.

    By calling the show() and setAutoFillBackground(True) methods for the QWidget subclass object, i can get the colors to show up. eg:

        wid2.setStyleSheet("QWidget {background-color: #0000FF }")
        wid2.show()
        wid2.setAutoFillBackground(True)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.