This demo program show how to create a side menu using a treeview.
The output:
The code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 | import sys import os from PyQt6.QtWidgets import QApplication, QVBoxLayout, QWidget, QPushButton, QTreeView from PyQt6.QtCore import Qt from PyQt6.QtGui import QFont, QColor, QStandardItemModel, QStandardItem class StandardItem(QStandardItem): def __init__(self, txt='', font_size=12, set_bold=False, color=QColor(0, 0, 0), color1=QColor(0, 0, 0)): super().__init__() fnt = QFont('Open Sans', font_size) fnt.setBold(set_bold) self.setEditable(False) self.setForeground(color) self.setBackground(color1) self.setFont(fnt) self.setText(txt) class Window(QWidget): def __init__(self): super(Window, self).__init__() self.initUI() def initUI(self): self.treeView = QTreeView() self.treeView.setHeaderHidden(True) treeModel = QStandardItemModel() rootNode = treeModel.invisibleRootItem() america = StandardItem('Accounting', 12, set_bold=True, color1=QColor('#4d88ff')) california = StandardItem(' Accounts Payable', 10, color1=QColor('#80aaff')) america.appendRow(california) oakland = StandardItem(' Vendors', 8, color=QColor('#3939ac'), color1=QColor('#b3ccff')) sanfrancisco = StandardItem(' Incoming Invoice', 8, color=QColor('#3939ac'), color1=QColor('#b3ccff')) sanjose = StandardItem(' Purchases', 8, color=QColor('#3939ac'), color1=QColor('#b3ccff')) california.appendRow(oakland) california.appendRow(sanfrancisco) california.appendRow(sanjose) texas = StandardItem(' Accounts Receivable', 10, color1=QColor('#80aaff')) america.appendRow(texas) austin = StandardItem(' Customers', 8, color=QColor('#3939ac'), color1=QColor('#b3ccff')) houston = StandardItem(' Outbound Invoices', 8, color=QColor('#3939ac'), color1=QColor('#b3ccff')) cali = StandardItem(' Dunning', 8, color=QColor('#3939ac'), color1=QColor('#b3ccff')) dallas = StandardItem(' Sales', 8, color=QColor('#3939ac'), color1=QColor('#b3ccff')) texas.appendRow(austin) texas.appendRow(houston) texas.appendRow(cali) texas.appendRow(dallas) # Canada canada = StandardItem('Logistics', 12, set_bold=True, color1=QColor('#4d88ff')) alberta = StandardItem(' Inventory', 10, color1=QColor('#80aaff')) bc = StandardItem(' Materials', 10, color1=QColor('#80aaff')) ontario = StandardItem(' Deliveries', 10, color1=QColor('#80aaff')) canada.appendRows([alberta, bc, ontario]) rootNode.appendRow(america) rootNode.appendRow(canada) self.treeView.setModel(treeModel) self.treeView.expandAll() self.treeView.doubleClicked.connect(self.getValue) self.treeView.setIndentation(0) self.treeView.setAlternatingRowColors(True) self.pb1 = QPushButton('Exit', self) self.pb1.setGeometry(self.width()-110, self.height()-40, 100, 30) self.pb1.clicked.connect(onClick_pb1) self.pb2 = QPushButton('Click Me!', self) self.pb2.setGeometry(self.width()-220, self.height()-40, 100, 30) self.pb2.clicked.connect(self.onClick_pb2) self.pb3 = QPushButton('Open Notepad', self) self.pb3.setGeometry(self.width()-330, self.height()-40, 100, 30) self.pb3.clicked.connect(onClick_pb3) self.setGeometry(50, 65, 650, 450) self.treeView.setFixedSize(170,self.height()-25) self.setWindowTitle('Post 24') windowLayout = QVBoxLayout(self) windowLayout.addWidget(self.treeView, alignment=Qt.AlignmentFlag.AlignTop) self.show() def resizeEvent(self, event): self.treeView.setFixedSize(170,self.height()-25) self.pb1.setGeometry(self.width()-110, self.height()-40, 100, 30) self.pb2.setGeometry(self.width()-220, self.height()-40, 100, 30) self.pb3.setGeometry(self.width()-330, self.height()-40, 100, 30) def onClick_pb2(self): print("clicked") def getValue(self, val): print(val.data()) print(val.row()) print(val.column()) def onClick_pb3(): os.popen(r"C:\WINDOWS\system32\notepad.exe") def onClick_pb1(): exit() def main(): app = QApplication(sys.argv) ex = Window() sys.exit(app.exec()) if __name__ == '__main__': main() |
No comments:
Post a Comment