RichardF Posted June 5, 2016 Share Posted June 5, 2016 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 Quote Link to comment Share on other sites More sharing options...
Erik_JE Posted June 6, 2016 Share Posted June 6, 2016 Have you tried going the qss way? http://doc.qt.io/qt-4.8/stylesheet-examples.html#customizing-qtableview I have had some funky business going on lately with PySide styling in Houdini but qss seems to work usually. Quote Link to comment Share on other sites More sharing options...
RichardF Posted June 6, 2016 Author Share Posted June 6, 2016 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. Quote Link to comment Share on other sites More sharing options...
Erik_JE Posted June 7, 2016 Share Posted June 7, 2016 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()) Quote Link to comment Share on other sites More sharing options...
RichardF Posted June 7, 2016 Author Share Posted June 7, 2016 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. Quote Link to comment Share on other sites More sharing options...
90ender Posted April 4, 2018 Share Posted April 4, 2018 I am running into this issue as well. There has got to be a way to do this without unparenting the window. Anyone? Quote Link to comment Share on other sites More sharing options...
toadstorm Posted April 4, 2018 Share Posted April 4, 2018 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) Quote Link to comment Share on other sites More sharing options...
Recommended Posts
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.