Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from PyQt5.QtCore import *
- from PyQt5.QtGui import *
- from PyQt5.QtWidgets import *
- import sys
- class My_Model_table(QAbstractTableModel):
- def __init__(self, table_data=[], parent=None):
- super().__init__()
- self.table_data = table_data
- def rowCount(self, parent):
- return len(self.table_data)
- def columnCount(self, parent):
- return 1
- def data(self, index, role):
- if role == Qt.DisplayRole:
- value = self.table_data[index.row()]
- return value
- if role == Qt.TextAlignmentRole:
- return Qt.AlignCenter
- class My_table(QTableView):
- def __init__(self, parent=None):
- super().__init__()
- # to make tab cycle between widgets not rows
- def keyPressEvent(self, event):
- if event.key() == 16777217:
- self.clearSelection()
- self.parent().focusNextChild()
- return
- if event.key() == 16777218:
- self.clearSelection()
- self.parent().focusPreviousChild()
- return
- QTableView.keyPressEvent(self, event)
- # to select the row on focus
- def focusInEvent(self, event):
- row = self.currentIndex().row()
- if row == -1:
- row = 0
- self.selectRow(row)
- QTableView.focusInEvent(self, event)
- class the_whole_thing_put_together(QWidget):
- def __init__(self, parent=None):
- super().__init__()
- self.init()
- def init(self):
- data = ['1', '2', '3', '4', '5']
- mt = My_table()
- mt.setModel(My_Model_table(data))
- li = QLineEdit()
- g = QGridLayout()
- g.addWidget(li, 0, 0)
- g.addWidget(mt, 1, 0)
- self.setLayout(g)
- self.show()
- if __name__ == '__main__':
- app = QApplication(sys.argv)
- go = the_whole_thing_put_together()
- sys.exit(app.exec_())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement