I am working on a ui as shown in the image below.
In the First columns i have a Check Box, Second columns i have a Slider, Third just text
the slider value applied to a function. the result will be shown in the third column.
I am not not able to see the slider not the checkbox
Output is shown in the image
Code:
from PyQt4 import QtGui, QtCore, uic
import sys
from PyQt4.QtCore import *
from PyQt4.QtGui import *
class PaletteTableModel(QtCore.QAbstractTableModel):
def __init__(self, colors = [[]], parent = None):
QtCore.QAbstractTableModel.__init__(self, parent)
self.__colors = colors
def rowCount(self, parent):
return len(self.__colors)
def columnCount(self, parent):
return len(self.__colors[0])
def flags(self, index):
if not index.isValid():
return None
if index.column() == 0:
return QtCore.Qt.ItemIsEnabled | QtCore.Qt.ItemIsSelectable | QtCore.Qt.ItemIsUserCheckable
else:
return QtCore.Qt.ItemIsEnabled | QtCore.Qt.ItemIsSelectable
def data(self, index, role):
row = index.row()
col = index.column()
if role == QtCore.Qt.DisplayRole:
return '{0}'.format(self.__colors[row][col])
elif role == Qt.CheckStateRole and col==0:
return QPersistentModelIndex(index)
else:
return None
def setData(self, index, value, role=Qt.EditRole):
if not index.isValid():
return False
if role == Qt.CheckStateRole:
if index.column() == 0:
if self.__colors[index.row()][index.column()].isChecked():
return QtCore.Qt.Checked
else:
return QtCore.Qt.Unchecked
if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
app.setStyle("plastique")
tableView = QtGui.QTableView()
tableView.show()
row = 6
col = 3
table_data = []
for row in range(row):
data = [None,None,None]
#Checkbox
c1 = QCheckBox("c"+str(row))
c1.setChecked(True)
data[0] = c1
#slider
s1 = QSlider(Qt.Horizontal)
s1.setMinimum(10)
s1.setMaximum(30)
s1.setValue(20)
data[1] = s1
#text
data[2] = 'TEST'
table_data.append(data)
model = PaletteTableModel(table_data)
tableView.setModel(model)
sys.exit(app.exec_())
UI:
The models serve to keep information, do not keep the view. If you want to customize the view you should use delegates. By default if you save it in the
CheckStateRole
role, a checkbox will be created, if you save in theDisplayRole
it will be displayed as text, so even if you save the widgets what is shown is the__str__
that shows the memory address.What you should do is just save the data, that is, the bool, the value and the text, and then create a delegate by creating an editor in the second column and make it persistent.