PyQt5-QListWidget和QToolButton
下面是我设计的:
1.界面
2.对象查看器
3.信号槽
4.动作编辑器
5.myWidget.py文件
import sys
from PyQt5.QtWidgets import (QMainWindow,QApplication,QListWidgetItem,
QMenu,QToolButton)
from PyQt5.QtCore import pyqtSlot,Qt
from PyQt5.QtGui import QIcon,QCursor
from ui_QtApp import Ui_MainWindow
class QmyMainWindow(QMainWindow):
def __init__(self,parent=None):
super().__init__(parent) #调用父类构造函数
self.ui=Ui_MainWindow() #创建UI对象
self.ui.setupUi(self) #构造UI
self.__setActionsForButton()
self.__createSelectionPopMenu()
self.__FlagEditable = (Qt.ItemIsSelectable | Qt.ItemIsUserCheckable|
Qt.ItemIsEnabled | Qt.ItemIsEditable)
self.__FlagNotEditable = (Qt.ItemIsSelectable|Qt.ItemIsUserCheckable | Qt.ItemIsEnabled)
## ====由connectSlotsByName()自动与组件的信号关联的槽函数=====
def __setActionsForButton(self): # 为ToolButton按钮设置Action
self.ui.toolButton.setDefaultAction(self.ui.actList_Ini)
self.ui.toolButton_2.setDefaultAction(self.ui.actList_Clear)
self.ui.toolButton_3.setDefaultAction(self.ui.actList_Insert)
self.ui.toolButton_4.setDefaultAction(self.ui.actList_Append)
self.ui.toolButton_5.setDefaultAction(self.ui.actList_Delete)
self.ui.toolButton_7.setDefaultAction(self.ui.actSel_ALL)
self.ui.toolButton_8.setDefaultAction(self.ui.actSel_None)
self.ui.toolButton_9.setDefaultAction(self.ui.actSel_Invs)
def __createSelectionPopMenu(self):##创建ToolButton按钮的下拉菜单
menuSelection=QMenu(self)#下拉菜单
menuSelection.addAction(self.ui.actSel_ALL)
menuSelection. addAction(self.ui.actSel_None)
menuSelection. addAction(self.ui.actSel_Invs)
##listWidget上方的btnSelectItem按钮
self.ui.toolButton_6.setPopupMode(QToolButton.MenuButtonPopup)
##self.ui. btnSelectItem. setPopupMode(QToolButton.InstantPopup)
self.ui.toolButton_6.setToolButtonStyle(Qt.ToolButtonTextBesideIcon)
self.ui.toolButton_6.setDefaultAction(self.ui.actSelPopMenu)
self.ui.toolButton_6.setMenu(menuSelection)
# 设置下拉菜单
##工具栏上的下拉式菜单按钮
toolBtn = QToolButton(self)
toolBtn.setPopupMode(QToolButton.InstantPopup)
toolBtn.setDefaultAction(self.ui.actSelPopMenu)
toolBtn.setToolButtonStyle(Qt.ToolButtonTextUnderIcon)
toolBtn.setMenu(menuSelection) # 设置下拉菜单
self.ui.toolBar.addWidget(toolBtn)
##工具栏添加分隔条和"退出"按钮
self.ui.toolBar.addSeparator()
self.ui.toolBar.addAction(self.ui.actQuit)
@pyqtSlot() ##初始化列表
def on_actList_Ini_triggered(self):
editable = self.ui.checkBox.isChecked()
if (editable == True):
Flag = self.__FlagEditable
# 可编辑
else:
Flag = self.__FlagNotEditable # 不可编辑
self.ui.listWidget.clear()
# 清除列表
for i in range(10):
itemStr = "Item %d" %i
aItem = QListWidgetItem()
aItem.setText(itemStr)
aItem.setCheckState(Qt.Checked)
aItem.setFlags(Flag)# 项的f1ags
self.ui.listWidget.addItem(aItem)
@pyqtSlot()##插入一项
def on_actList_Insert_triggered(self):
editable=self.ui.checkBox.isChecked()
if (editable == True):
Flag=self.__FlagEditable
#可编辑
else:
Flag=self.__F1agNotEditable #不可编辑
aItem=QListWidgetItem()
aItem.setText("Inserted Item")
aItem.setCheckState(Qt.Checked)
aItem.setFlags(Flag)#项的f1ags
curRow=self.ui.listWidget.currentRow ()#当前行
self.ui.listWidget.insertItem(curRow,aItem)
@pyqtSlot()##删除当前项
def on_actList_Delete_triggered(self) :
row=self.ui.listWidget.currentRow()
self.ui.listWidget.takeItem(row)
#移除当前项,Python自动删除
@pyqtSlot()##清空列表
def on_actList_Clear_triggered(self) :
self.ui.listWidget.clear()
@pyqtSlot() # 全选
def on_actSel_ALL_triggered(self):
for i in range(self.ui.listWidget.count()):
aItem = self.ui.listWidget.item(i)
aItem.setCheckState(Qt.Checked)
@pyqtSlot() # 全不选
def on_actSel_None_triggered(self):
for i in range(self.ui.listWidget.count()):
aItem = self.ui.listWidget.item(i)
aItem.setCheckState(Qt.Unchecked)
@pyqtSlot()#反选
def on_actSel_Invs_triggered(self):
for i in range(self.ui.listWidget.count()):
aItem=self.ui.listWidget.item(i)
if (aItem.checkState()!=Qt.Checked) :
aItem.setCheckState(Qt.Checked)
else:
aItem.setCheckState(Qt.Unchecked)
def on_listWidget_currentItemChanged(self, current, previous):
strInfo = ""
if (current!=None):
if (previous == None):
strInfo = "当前:" + current.text()
else:
strInfo = "前一项:" + previous.text() + ";当前项:" + current.text()
self.ui.lineEdit.setText(strInfo)
def on_listWidget_customContextMenuRequested(self,pos): ##右键快捷菜单
menuList=QMenu(self)#创建菜单
menuList.addAction(self.ui.actList_Ini)
menuList.addAction(self.ui.actList_Clear)
menuList.addAction(self.ui.actList_Insert)
menuList.addAction(self.ui.actList_Append)
menuList.addAction(self.ui.actList_Delete)
menuList.addSeparator()
menuList.addAction(self.ui.actSel_ALL)
menuList.addAction(self.ui.actSel_None)
menuList.addAction(self.ui.actSel_Invs)
menuList.exec(QCursor.pos())#显示菜单
if __name__ == "__main__": ##用于当前窗体测试
app=QApplication(sys.argv) #创建GUI应用程序
form=QmyMainWindow() #创建窗体
form.show()
sys.exit(app.exec_())
6.appMain.py文件
## GUI应用程序主程序
import sys
from PyQt5.QtWidgets import QApplication
from myWidget import QmyMainWindow
app = QApplication(sys.argv) # 创建app,用QApplication类
myWidget = QmyMainWindow() #创建窗体
myWidget.show()
sys.exit(app.exec_())
7.最终效果图
转载请注明出处,欢迎讨论和交流!