1. 程式人生 > >PyQt4自定義控件----導航欄控件

PyQt4自定義控件----導航欄控件

sse ins seve 關於 導航 bsp and etc sele

PyQt4中自定義導航欄控件,運行如圖所示:

技術分享圖片

代碼:

  1 # -*- coding: utf-8 -*-#
  2 
  3 #-------------------------------------------------------------------------------
  4 # Name:         導航條控件
  5 # Description:  
  6 # Author:       lgk
  7 # Date:         2018/6/21
  8 #-------------------------------------------------------------------------------
9 10 import sys 11 from PyQt4.QtGui import * 12 from PyQt4.QtCore import * 13 14 class NavigationWidget(QWidget): 15 currentItemChanged = pyqtSignal([int, str]) 16 def __init__(self, parent=None): 17 super(NavigationWidget, self).__init__(parent) 18 19 self.initUI()
20 21 def initUI(self): 22 self.backgroundColor = #E4E4E4 23 self.selectedColor = #2CA7F8 24 self.rowHeight = 40 25 self.currentIndex = 0 #當前選擇的項索引 26 self.listItems = [] 27 self.cursorIndex = -1 #當前光標所在位置的項索引 28 29 self.setMouseTracking(True)
30 self.setMinimumWidth(120) 31 32 def addItem(self, item): 33 self.listItems.append(item) 34 self.update() 35 36 def setItems(self, items): 37 self.listItems = items 38 self.update() 39 40 def setBackgroundColor(self, color): 41 self.backgroundColor = color 42 self.update() 43 44 def setSelectColor(self, color): 45 self.selectedColor = color 46 self.update() 47 48 def setRowHeight(self, height): 49 self.rowHeight = height 50 self.update() 51 52 def setCurrentIndex(self, idx): 53 self.currentIndex = idx 54 self.currentItemChanged.emit(idx, self.listItems[idx]) 55 self.update() 56 57 def paintEvent(self, evt): 58 painter = QPainter(self) 59 painter.setRenderHint(QPainter.Antialiasing, True) 60 61 #畫背景色 62 painter.setPen(Qt.NoPen) 63 painter.setBrush(QColor(self.backgroundColor)) 64 painter.drawRect(self.rect()) 65 66 #畫子項 67 for i in range(len(self.listItems)): 68 itemPath = QPainterPath() 69 itemPath.addRect(QRectF(0, i*self.rowHeight, self.width()-1, self.rowHeight-1)) 70 71 if i == self.currentIndex: 72 painter.setPen(QColor(#FFFFFF)) 73 painter.fillPath(itemPath, QColor(self.selectedColor)) 74 elif i == self.cursorIndex: 75 painter.setPen(QColor(#FFFFFF)) 76 painter.fillPath(itemPath, QColor(self.selectedColor)) 77 else: 78 painter.setPen(QColor(#202020)) 79 painter.fillPath(itemPath, QColor(self.backgroundColor)) 80 81 painter.drawText(QRect(0, i*self.rowHeight, self.width(), self.rowHeight), Qt.AlignVCenter|Qt.AlignHCenter, self.listItems[i]) 82 83 def mouseMoveEvent(self, evt): 84 idx = evt.y() / self.rowHeight 85 if idx >= len(self.listItems): 86 idx = -1 87 if idx < len(self.listItems) and idx != self.cursorIndex: 88 self.update() 89 self.cursorIndex = idx 90 91 def mousePressEvent(self, evt): 92 idx = evt.y()/self.rowHeight 93 if idx< len(self.listItems): 94 self.currentIndex = idx 95 self.currentItemChanged.emit(idx, self.listItems[idx]) 96 self.update() 97 98 def leaveEvent(self, QEvent): 99 self.cursorIndex = -1 100 self.update() 101 102 103 class MainWindow(QMainWindow): 104 def __init__(self): 105 super(MainWindow, self).__init__() 106 self.initUI() 107 108 def initUI(self): 109 self.resize(600, 400) 110 self.setWindowTitle(u導航條控件) 111 112 mainWidget = QWidget() 113 self.setCentralWidget(mainWidget) 114 115 navigationWidget = NavigationWidget() 116 navigationWidget.setRowHeight(50) 117 navigationWidget.setItems([u常規, u高級, u管理, u其它, u關於]) 118 119 self.tipsLabel = QLabel(u"請選擇:") 120 121 mainLayout = QHBoxLayout(mainWidget) 122 mainLayout.setContentsMargins(0, 0, 0, 0) 123 mainLayout.setSpacing(10) 124 mainLayout.addWidget(navigationWidget, 1) 125 mainLayout.addWidget(self.tipsLabel, 3, Qt.AlignCenter) 126 127 navigationWidget.currentItemChanged[int, str].connect(self.slotCurrentItemChanged) 128 navigationWidget.setCurrentIndex(2) 129 130 self.show() 131 132 def slotCurrentItemChanged(self, index, content): 133 self.tipsLabel.setText(u"Current index and content:{} ---- {}".format(index, content)) 134 135 def main(): 136 app = QApplication(sys.argv) 137 mainWnd = MainWindow() 138 sys.exit(app.exec_()) 139 140 if __name__ == __main__: 141 main()

PyQt4自定義控件----導航欄控件