Advertisement
Guest User

Untitled

a guest
May 7th, 2016
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.86 KB | None | 0 0
  1. from PyQt5.QtCore import *
  2. from PyQt5.QtGui import *
  3. from PyQt5.QtWidgets import *
  4. import sys
  5.  
  6.  
  7. class My_Model_table(QAbstractTableModel):
  8.     def __init__(self, table_data=[], parent=None):
  9.         super().__init__()
  10.         self.table_data = table_data
  11.  
  12.     def rowCount(self, parent):
  13.         return len(self.table_data)
  14.  
  15.     def columnCount(self, parent):
  16.         return 1
  17.  
  18.     def data(self, index, role):
  19.         if role == Qt.DisplayRole:
  20.             value = self.table_data[index.row()]
  21.             return value
  22.         if role == Qt.TextAlignmentRole:
  23.             return Qt.AlignCenter
  24.  
  25.  
  26. class My_table(QTableView):
  27.     def __init__(self, parent=None):
  28.         super().__init__()
  29.  
  30.     # to make tab cycle between widgets not rows
  31.     def keyPressEvent(self, event):
  32.         if event.key() == 16777217:
  33.             self.clearSelection()
  34.             self.parent().focusNextChild()
  35.             return
  36.         if event.key() == 16777218:
  37.             self.clearSelection()
  38.             self.parent().focusPreviousChild()
  39.             return
  40.         QTableView.keyPressEvent(self, event)
  41.  
  42.     # to select the row on focus
  43. def focusInEvent(self, event):
  44.     row = self.currentIndex().row()
  45.     if row == -1:
  46.         row = 0
  47.     self.selectRow(row)
  48.     QTableView.focusInEvent(self, event)
  49.  
  50.  
  51. class the_whole_thing_put_together(QWidget):
  52.     def __init__(self, parent=None):
  53.         super().__init__()
  54.         self.init()
  55.  
  56.     def init(self):
  57.         data = ['1', '2', '3', '4', '5']
  58.         mt = My_table()
  59.         mt.setModel(My_Model_table(data))
  60.  
  61.         li = QLineEdit()
  62.  
  63.         g = QGridLayout()
  64.         g.addWidget(li, 0, 0)
  65.         g.addWidget(mt, 1, 0)
  66.         self.setLayout(g)
  67.         self.show()
  68.  
  69.  
  70. if __name__ == '__main__':
  71.     app = QApplication(sys.argv)
  72.     go = the_whole_thing_put_together()
  73.     sys.exit(app.exec_())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement