StyleSheetDemo Example
import sys, os
sys.path.append(os.path.dirname(os.path.realpath(__file__)) + "/../../shared")
from DevMachines import __pyside2__, __pyside6__
from DevMachines import QtitanBase
from DevMachines.QtitanBase import Qtitan
from DevMachines.QtitanGrid import (getGridVersion, Grid, GridViewOptions,
GridColumn, GridEditor, GridSummary,
ContextMenuEventArgs, CellButtonClickEventArgs)
if __pyside2__:
from PySide2 import QtCore
from PySide2.QtCore import Qt, QTime, QDate, QAbstractItemModel, QModelIndex, QFile, QTextCodec
from PySide2.QtWidgets import (QWidget, QApplication, QTextEdit, QDialog, QVBoxLayout, QHBoxLayout, QPushButton,
QSlider, QLabel, QCheckBox, QComboBox, QMessageBox, QSplitter)
if __pyside6__:
from PySide6 import QtCore
from PySide6.QtCore import Qt, QTime, QDate, QAbstractItemModel, QModelIndex, QFile, QTextStream
from PySide6.QtWidgets import (QWidget, QApplication, QTextEdit, QDialog, QVBoxLayout, QHBoxLayout, QPushButton,
QSlider, QLabel, QCheckBox, QComboBox, QMessageBox, QSplitter)
import StyleSheetDemo_rc
from DemoMainWindow import DemoMainWindow
def rand_value(low, high):
return qrand() % ((high + 1) - low) + low
class DataItem:
v0 = None
v1 = None
v2 = None
v3 = None
class CustomFilterModel(QAbstractItemModel):
def __init__(self, parent):
QAbstractItemModel.__init__(self, parent)
self.values = list(range(0, 100))
y = 2012
m = 1
d = 1
for i in self.values:
item = DataItem()
item.v0 = i
item.v1 = "String = " + str(i)
item.v3 = QTime(12, 0, 0)
if d > 28:
d = 1
m = m + 1
if m > 12:
m = 1
y = y + 1
item.v2 = QDate(y, m, d)
if not ((i + 1) % 10):
d = d + 1
self.values[i] = item
def headerData(self, section, orientation, role):
if section == 0:
return "Integer"
elif section == 1:
return "String"
elif section == 2:
return "Date"
elif section == 3:
return "Time"
return None
def parent(self, child):
return QModelIndex()
def hasChildren(self, parent):
if parent.model() == self or not parent.isValid():
return self.rowCount(parent) > 0 and self.columnCount(parent) > 0
return False
def rowCount(self, parent):
if parent.isValid():
return 0
return len(self.values)
def columnCount(self, parent):
if parent.isValid():
return 0
return 4
def index(self, row, column, parent):
if parent.isValid():
return QModelIndex()
if row < 0 or row >= self.rowCount(parent):
return QModelIndex()
if column < 0 or column >= self.columnCount(parent):
return QModelIndex()
return self.createIndex(row, column, parent)
def data(self, index, role):
if not index.isValid():
return None
if index.row() < 0 or index.row() >= self.rowCount(index.parent()):
return None
if index.column() < 0 or index.column() >= self.columnCount(index.parent()):
return None
if role == Qt.DisplayRole or role == Qt.EditRole:
if index.column() == 0:
return self.values[index.row()].v0
elif index.column() == 1:
return self.values[index.row()].v1
elif index.column() == 2:
return self.values[index.row()].v2
elif index.column() == 3:
return self.values[index.row()].v3
return None
def setData(self, index, value, role):
if not index.isValid():
return False
if index.row() < 0 or index.row() >= self.rowCount(index.parent()):
return False
if index.column() < 0 or index.column() >= self.columnCount(index.parent()):
return False
if role != Qt.EditRole:
return False
if index.column() == 0:
self.values[index.row()].v0 = int(value)
elif index.column() == 1:
self.values[index.row()].v1 = str(value)
elif index.column() == 2:
self.values[index.row()].v2 = value
elif index.column() == 3:
self.values[index.row()].v3 = value
self.dataChanged.emit(index, index)
return True
def flags(self, index):
if not index.isValid():
return Qt.ItemFlags()
return Qt.ItemIsEnabled | Qt.ItemIsEditable
class Window(DemoMainWindow):
def __init__(self):
DemoMainWindow.__init__(self, "QtitanDataGrid", getGridVersion())
self.setWindowTitle(self.tr("Style Sheet Demo"))
self.setGeometry(50, 50, 800, 500)
Grid.loadTranslation()
self.grid = Grid()
# Configure grid view
self.grid.setViewType(Grid.TableView)
view = self.grid.view()
view.options().setFooterSummaryVisible(True)
view.options().setGroupSummaryPlace(GridViewOptions.SummaryRowFooter)
# view.options().setGridLineWidth(0)
# view.tableOptions().setColumnAutoWidth(True)
# Connect Grid's context menu handler.
self.connect(view, QtCore.SIGNAL("contextMenu(ContextMenuEventArgs*)"), self, QtCore.SLOT("contextMenu(ContextMenuEventArgs* )"))
model = CustomFilterModel(self.grid)
view.setModel(model)
column = view.getColumn(0)
column.setFixedPosition(Qtitan.AtBeginning)
column.setEditorType(GridEditor.Numeric)
column.editorRepository().setMinimum(-10000)
column.editorRepository().setMaximum(10000)
column.editorRepository().setEditorActivationPolicy(
GridEditor.ActivationPolicy(GridEditor.ActivationPolicy(GridEditor.ActivateByDblClick | GridEditor.ActivateByEditPress)))
column = view.getColumn(1)
column.setEditorType(GridEditor.String)
column.editorRepository().setValidateOnEnter(False)
column.editorRepository().setEditorActivationPolicy(
GridEditor.ActivationPolicy(GridEditor.ActivationPolicy(GridEditor.ActivateByDblClick | GridEditor.ActivateByEditPress)))
column = view.getColumn(2)
column.setEditorType(GridEditor.Date)
column.editorRepository().setAutoSelect(True)
column.editorRepository().setEditorActivationPolicy(
GridEditor.ActivationPolicy(GridEditor.ActivationPolicy(GridEditor.ActivateByDblClick | GridEditor.ActivateByEditPress)))
column.setGroupIndex(0)
column.setGroupSummary(column, GridSummary.Max)
column.setFooterSummary(GridSummary.Count)
column = view.getColumn(3)
column.setEditorType(GridEditor.Time)
column.editorRepository().setAutoSelect(True)
column.editorRepository().setEditorActivationPolicy(
GridEditor.ActivationPolicy(GridEditor.ActivationPolicy(GridEditor.ActivateByDblClick | GridEditor.ActivateByEditPress)))
# Create settings widget
settings = QWidget(self)
l = QVBoxLayout(settings)
text = QLabel(settings)
text.setText("Buttons below allows you to customize the grid via Qt Style Sheet."
"Press 'Add QSS' button to apply Style Sheet and 'Remove QSS' to remove Style Sheet setting"
"and restore the default view.")
text.setWordWrap(True)
l.addWidget(text)
hblayout = QHBoxLayout()
addButton = QPushButton(settings)
addButton.setText(self.tr("Add QSS (new)"))
self.connect(addButton, QtCore.SIGNAL("clicked()"), self, QtCore.SLOT("addStyleSheet()"))
hblayout.addWidget(addButton)
removeButton = QPushButton(settings)
removeButton.setText(self.tr("Remove QSS (new)"))
self.connect(removeButton, QtCore.SIGNAL("clicked()"), self, QtCore.SLOT("removeStyleSheet()"))
hblayout.addWidget(removeButton)
l.addLayout(hblayout)
self.setDemoWidget(self.grid, settings)
self.setMinimumHeight(10)
@QtCore.Slot(ContextMenuEventArgs)
def contextMenu(self, args):
args.contextMenu().addAction("Print Preview", self, QtCore.SLOT("printPreview()"))
args.contextMenu().addSeparator()
args.contextMenu().addAction("Developer Machines on the Web", self, QtCore.SLOT("showCompanyWebSite()"))
def setShadeColor(self, color):
self.grid.themeManager().setShadeColor(color)
@QtCore.Slot()
def addStyleSheet(self):
file = QFile(":/res/StyleSheet.qss")
file.open(QFile.ReadOnly)
if __pyside2__:
data = file.readAll()
codec = QTextCodec.codecForLocale()
styleSheet = codec.toUnicode(data)
if __pyside6__:
stream = QTextStream(file)
styleSheet = stream.readAll()
qApp.setStyleSheet(styleSheet)
@QtCore.Slot()
def removeStyleSheet(self):
qApp.setStyleSheet("")
if __name__ == "__main__":
app = QApplication(sys.argv)
w = Window()
w.show()
sys.exit(app.exec_())