Self colouring QPushButton not working inside table
-
Hi everyone,
I've got this snippet of code which creates a self colouring QPushButton (push button to open a colour choser, then use the chosen colour to set the button's bg colour. This is it and it works just fine:
@import sys
import PySide.QtCore as QtCore
import PySide.QtGui as QtGuiclass ColourButton( QtGui.QPushButton ):
def init( self, *args, **kwargs ):
'''A self colouring button'''
super( ColourButton, self ).init( *args, **kwargs)def chooseColour( self ): colour = QtGui.QColorDialog().getColor() self.setStyleSheet( u'background-color:%s' % colour.name() )
if name == "main":
app = QtGui.QApplication( sys.argv )
btn = ColourButton( '...' )
btn.clicked.connect( btn.chooseColour )
btn.setFlat( True )
btn.show()
sys.exit( app.exec_() )
@Now I'm trying to populate a cell table with this button:
@import sys
import PySide.QtCore as QtCore
import PySide.QtGui as QtGuiclass ColourButton( QtGui.QPushButton ):
def init( self, *args, **kwargs ):
'''A self colouring button'''
super( ColourButton, self ).init( *args, **kwargs)def chooseColour( self ): colour = QtGui.QColorDialog().getColor() self.setStyleSheet( u'background-color:%s' % colour.name() )
class TestTable( QtGui.QWidget ):
def init( self, parent=None ):
QtGui.QWidget.init( self, parent )
self.setLayout( QtGui.QVBoxLayout() )
self.resize( 500, 300 )
self.myTable = QtGui.QTableWidget()
self.myTable.header = ['Name', 'Colour' ]
self.myTable.size = [ 75, 375, 85, 600 ]
self.myTable.setColumnCount( len(self.myTable.header) )
self.myTable.setRowCount( 5 )for i in range( 0, self.myTable.rowCount() ): # SET COLOURBUTTON WIDGETS colBtn = ColourButton( '...' ) colBtn.clicked.connect( colBtn.chooseColour ) colBtn.setFlat( True ) # THIS SEEMS TO PREVENT ColourButton.chooseColour() FROM DOING IT'S JOB self.myTable.setCellWidget( i,1,colBtn ) self.layout().addWidget( self.myTable )
if name == "main":
app = QtGui.QApplication( sys.argv )
tableView = TestTable()
tableView.show()
sys.exit( app.exec_() )
@For some reason, the button won't colour itself any longer when called from inside the table. And even stranger (to me at least) is that if I comment out the line that sets the button's style to "flat" (line 31: colBtn.setFlat( True ) ), it does work again.
Am I doing something wrong or is this a bug? I would definitely prefer using a flat button.
Cheers,
frank -
Wow, I posted the same question today. Any succes?