码迷,mamicode.com
首页 > 其他好文 > 详细

PyQt4自定义控件----导航栏控件

时间:2018-06-21 13:39:21      阅读:410      评论:0      收藏:0      [点我收藏+]

标签:gui   awt   pat   qt4   inf   导航条   leave   pre   layout   

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自定义控件----导航栏控件

标签:gui   awt   pat   qt4   inf   导航条   leave   pre   layout   

原文地址:https://www.cnblogs.com/luke0011/p/9208334.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!