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

  • SEARCH
  • Home
  • 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 7572437
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T15:55:31+00:00 2026-05-30T15:55:31+00:00

I am running into strange behavior where setAlpha is not working on a QColor

  • 0

I am running into strange behavior where setAlpha is not working on a QColor object. I do not understand why it is not working, but I am sure there is a good reason that I am just not aware of yet.

Here is the issue in a small scale example:

from PyQt4 import QtGui

clr = QtGui.QColor('yellow')
qtwi = QtGui.QTableWidgetItem()
qtwi.setBackgroundColor(clr)

print 'Colors are the same object: %s' % (clr == qtwi.backgroundColor())

print 'Alpha before is: %s' % clr.alpha()
clr.setAlpha(67)
print 'Alpha after is: %s' % clr.alpha()

print 'Alpha before is: %s' % qtwi.backgroundColor().alpha()
qtwi.backgroundColor().setAlpha(171)
print 'Alpha after is: %s' % qtwi.backgroundColor().alpha()

print 'Colors are the same object: %s' % (clr == qtwi.backgroundColor())

The results should read:

Colors are the same object: True
Alpha before is: 255
Alpha after is: 67
Alpha before is: 255
Alpha after is: 255
Colors are the same object: False

This makes no sense to me. I am clearly setting the alpha value to 171 in the second part of the example, why is it not working? Furthermore, if clr and qtwi.backgroundColor() are the same object at the beginning, why are they not longer the same at the end? What is going on here? I am quite confused. Thanks.

  • 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-30T15:55:32+00:00Added an answer on May 30, 2026 at 3:55 pm

    qtwi.backgroundColor() is returning unique instances of the same color. If you change one of the instances, it will not change the original color.

    If you run:

    print qtwi.backgroundColor()
    print qtwi.backgroundColor()
    

    You will get:

    <PyQt4.QtGui.QColor object at 0x7f02c61f9ad0>
    <PyQt4.QtGui.QColor object at 0x7f02c61f9a60>
    

    They are two different objects. They are copies of the original. If you change the copy, it will not change the original.

    However, if you set qtwi.backgroundColor() as a variable, your code will work. If you try:

    from PyQt4 import QtGui
    
    clr = QtGui.QColor('yellow')
    qtwi = QtGui.QTableWidgetItem()
    qtwi.setBackgroundColor(clr)
    
    print 'Colors are the same object: %s' % (clr == qtwi.backgroundColor())
    
    print 'Alpha before is: %s' % clr.alpha()
    clr.setAlpha(67)
    print 'Alpha after is: %s' % clr.alpha()
    
    qtwibg = qtwi.backgroundColor()
    print 'Alpha before is: %s' % qtwibg.alpha()
    qtwibg.setAlpha(171)
    print 'Alpha after is: %s' % qtwibg.alpha()
    
    print 'Colors are the same object: %s' % (clr == qtwibg)
    

    You should get the results you are looking for.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm running into a strange result here and am not sure if it is
I'm working on an NSWindow subclass and I'm running into some strange behavior that
I am running into an strange behavior when using NSOpeation. I am calling a
I'm writing a simple program using SDL, but I'm running into a strange problem.
I am running into an extremely strange behavior in Groovy. When I throw an
I'm running into some strange machine/OS dependent GZipStream behavior in .NET 4.0. This is
I'm running into a strange problem with testing an object's visibility with jQuery. I
I'm running into a problem setting up my development environment. I've been working on
I'm running into a strange situation with passing a pointer to a structure with
I am running into a strange issue in perl that I can't seem to

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.