Skip to content

Instantly share code, notes, and snippets.

@paulwinex
Created November 9, 2015 12:05
Show Gist options
  • Save paulwinex/f50c990bc79e150300d4 to your computer and use it in GitHub Desktop.
Save paulwinex/f50c990bc79e150300d4 to your computer and use it in GitHub Desktop.
from PySide.QtCore import *
from PySide.QtGui import *
lineWidth = 3
rows = 4
columns = 6
color = 'red'
class UnderlinedItemClass(QWidget):
def __init__(self):
super(UnderlinedItemClass, self).__init__()
self.ly = QVBoxLayout(self)
self.vly = QHBoxLayout()
self.select = QPushButton('Select Items')
self.select.clicked.connect(self.select_items)
self.unselect = QPushButton('Unselect Items')
self.unselect.clicked.connect(lambda : self.select_items(False))
self.vly.addWidget(self.select)
self.vly.addWidget(self.unselect)
self.ly.addLayout(self.vly)
self.table = QTableWidget()
self.ly.addWidget(self.table)
self.fill_table()
self.resize(700,400)
def fill_table(self):
self.table.clear()
self.table.setColumnCount(columns)
for i in range(rows):
self.table.insertRow(self.table.rowCount())
for j in range(columns):
w = ItemWidget()
self.table.setCellWidget(i, j, w)
def select_items(self, sel=True):
for ind in self.table.selectedIndexes():
if sel:
self.table.cellWidget(ind.row(), ind.column()).select()
else:
self.table.cellWidget(ind.row(), ind.column()).unselect()
class ItemWidget(QWidget):
def __init__(self):
super(ItemWidget, self).__init__()
self.content_ly = QVBoxLayout(self)
self.content_ly.setContentsMargins(0,0,0,0)
self.text = QLabel()
self.text.setStyleSheet('padding:3px;padding-bottom:-2px;')
self.content_ly.addWidget(self.text)
self.line = QFrame()
self.line.setMaximumHeight(lineWidth)
self.line.setMinimumHeight(lineWidth)
self.content_ly.addWidget(self.line)
self.setText('ITEM')
def setText(self, text):
self.text.setText(text)
def select(self):
# self.line.setStyleSheet('QWidget{border:none; border-bottom: 2px solid red;}')
self.line.setStyleSheet('background-color:%s;' % color)
def unselect(self):
self.line.setStyleSheet('')
if __name__ == '__main__':
app = QApplication([])
w = UnderlinedItemClass()
w.show()
app.exec_()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment