Jump to content

PySide set Color of cells in QTableView


RichardF

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

Link to comment
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.

Link to comment
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())

Link to comment
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.

   
Link to comment
Share on other sites

  • 1 year later...

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)

 

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...