Jump to content
RichardF

PySide set Color of cells in QTableView

Recommended Posts

Hello everyone,

 

i´m writing a tool in Pyside and i can´t figure out how to set the color of the individual cells in a QTableView.

Here is an example, which demonstrates the problem:
 

from PySide import QtCore, QtGui
import hou

class cellColor(QtGui.QDialog):
    def __init__(self):
        QtGui.QDialog.__init__(self)
        self.resize(297, 174)
        self.verticalLayout = QtGui.QVBoxLayout(self)
        self.tableview = QtGui.QTableView(self)
        self.verticalLayout.addWidget(self.tableview)
        
        # without parenting the ui to houdini, i can set the cell colors
        self.setParent(hou.ui.mainQtWindow(), QtCore.Qt.Window)
        
        model = QtGui.QStandardItemModel()
        item1 = QtGui.QStandardItem("Color Red")
        item1.setBackground(QtGui.QColor(240,10,10))
        item2 = QtGui.QStandardItem("Color Green")
        item2.setBackground(QtGui.QColor(10,240,10))
        
        model.appendRow([item1,item2])
        
        self.tableview.setModel(model)
        
testwindow = cellColor()
testwindow.show()

        
testwindow = cellColor()
testwindow.show()

 

Is there a way to set the color of the cells, when the window is parented to Houdini?

Thanks for all replies

Share this post


Link to post
Share on other sites

Yes i tried it, but it seems that it is only possible to set the stylesheet of all items or the selected item, but not of some specific items. In my case i want to color the first column of my QTableView, but i haven´t found a solution for that with qss.

Share this post


Link to post
Share on other sites

You could also give QTableWidget a spin and see if it works differently.

Support also emailed me last week that you can initialize the QDialog without parenting it to Houdini and then instead set the stylesheet afterwards.

my_dialog = QDialog()
my_dialog.setStyleSheet(hou.ui.qtStyleSheet())

Share this post


Link to post
Share on other sites

QTableWidget might work, but i'm using multiple QTableViews already and i would have to rewrite a lot of code in order to switch to QTableWidget.

Parenting the window has many advantages, which i don't want to miss, but I found a workaround which worked in my case:

I want to have a colored cell without any text in it.

I can't set the background color of the cells, but setting the foreground color (color of the text in the cell) is no problem.

So i set the text of the cell to ascii char 219 ( █ ), increased the font size and set the foreground color. Now it looks like a cell with a colored background.

That works fine in my case.

Thanks for your reply.

   

Share this post


Link to post
Share on other sites

I am running into this issue as well. There has got to be a way to do this without unparenting the window. Anyone?

Share this post


Link to post
Share on other sites

If you're using QTableView, you could always use a QStyledItemDelegate to display the first row however you want, but there's some extra boilerplate involved.

You could also try using the default model behavior's data() method... the role you want is QtCore.Qt.BackgroundRole:

if role == QtCore.Qt.BackgroundRole and index.column() == 0:

    return QtGui.QColor(255,0,0)

 

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×