QListWidget和QToolButton

学习PyQt推荐大家看这本书:https://weread.qq.com/web/reader/6393267071ccfa97639f573

链接:https://pan.baidu.com/s/1RtssqcvOxUGIomDhyo-sqg 
提取码:gg4z 

import sys
from PyQt5.QtWidgets import (QApplication,QMainWindow,QListWidgetItem,QMenu,QToolButton)
from PyQt5 import QtCore 
from PyQt5.QtGui import QIcon,QCursor
from PyQt5.QtCore import pyqtSlot,Qt 
from ui_Mainwindow import Ui_MainWindow

class QmyMainWindow(QMainWindow):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.ui=Ui_MainWindow()
        self.ui.setupUi(self)

        self.setCentralWidget(self.ui.splitter)#???
        self.__setActionsForButton()
        self.__createSelectionPopMenu()
        self.__FlagEditable=(Qt.ItemIsSelectable|Qt.ItemIsUserCheckable|Qt.ItemIsEnabled|Qt.ItemIsEditable)
        self.__FlagNotEditable=(Qt.ItemIsSelectable|Qt.ItemIsUserCheckable|Qt.ItemIsEnabled)
    def __setActionsForButton(self):
        self.ui.btnListInit.setDefaultAction(self.ui.actionInit)
        self.ui.btnListClear.setDefaultAction(self.ui.actionClear)
        self.ui.btnListInsert.setDefaultAction(self.ui.actionInsert)
        self.ui.btnListAppend.setDefaultAction(self.ui.actionAppend)
        self.ui.btnListDel.setDefaultAction(self.ui.actionDel)
        self.ui.btnSelAll.setDefaultAction(self.ui.actionSelAll)
        self.ui.btnSelNone.setDefaultAction(self.ui.actionSelNone)
        self.ui.btnSelInvs.setDefaultAction(self.ui.actionSelInvs)
    def __createSelectionPopMenu(self):#创建toolbutton下拉菜单
        menuSelection=QMenu(self)#下拉菜单
        menuSelection.addAction(self.ui.actionSelAll)
        menuSelection.addAction(self.ui.actionSelNone)
        menuSelection.addAction(self.ui.actionSelInvs)

        #listWidget上方的btnSelectItem按钮
        self.ui.btnSelectItem.setPopupMode(QToolButton.MenuButtonPopup)
        self.ui.btnSelectItem.setToolButtonStyle(Qt.ToolButtonTextBesideIcon)
        self.ui.btnSelectItem.setDefaultAction(self.ui.actionSelPopMenu)
        self.ui.btnSelectItem.setMenu(menuSelection)#设置下拉菜单

        #工具栏上的下拉菜单
        toolBtn=QToolButton(self)
        toolBtn.setPopupMode(QToolButton.InstantPopup)
        toolBtn.setDefaultAction(self.ui.actionSelPopMenu)
        toolBtn.setToolButtonStyle(Qt.ToolButtonTextOnly)
        toolBtn.setMenu(menuSelection)
        self.ui.toolBar.addWidget(toolBtn)

        #工具栏添加分隔条和“退出”按钮
        self.ui.toolBar.addSeparator()
        self.ui.toolBar.addAction(self.ui.actionQuit)
    @pyqtSlot()
    def on_actionInit_triggered(self):
        icon=QIcon(":/icons/images/app.gif")
        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.setIcon(icon)
            aItem.setCheckState(Qt.Checked)
            aItem.setFlags(Flag)

            self.ui.listWidget.addItem(aItem)
    @pyqtSlot()
    def on_actionInsert_triggered(self):
        icon=QIcon(":/icons/images/app.gif")
        editable=self.ui.checkBox.isChecked()
        if(editable==True):
            Flag=self.__FlagEditable
        else:
            Flag=self.__FlagNotEditable
        aItem=QListWidgetItem()
        aItem.setText("Inserted Item")
        aItem.setIcon(icon)
        aItem.setCheckState(Qt.Checked)
        aItem.setFlags(Flag)

        curRow=self.ui.listWidget.currentRow()
        self.ui.listWidget.insertItem(curRow,aItem)
    @pyqtSlot()
    def on_actionDel_triggered(self):
        row=self.ui.listWidget.currentRow()
        self.ui.listWidget.takeItem(row)
    @pyqtSlot()
    def on_actionClear_triggered(self):
        self.ui.listWidget.clear()
    @pyqtSlot()
    def on_actionSelAll_triggered(self):
        for i in range(self.ui.listWidget.count()):
            aItem=self.ui.listWidget.item(i)
            aItem.setCheckState(Qt.Checked)
    @pyqtSlot()
    def on_actionSelNone_triggered(self):
        for i in range(self.ui.listWidget.count()):
            aItem=self.ui.listWidget.item(i)
            aItem.setCheckState(Qt.Unchecked)
    @pyqtSlot()#反选
    def on_actionSelInvs_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)#self丢了就不起作用了
        menuList.addAction(self.ui.actionInit)
        menuList.addAction(self.ui.actionInsert)
        menuList.addAction(self.ui.actionAppend)
        menuList.addAction(self.ui.actionDel)
        menuList.addAction(self.ui.actionClear)
        menuList.addSeparator()
        menuList.addAction(self.ui.actionSelAll)
        menuList.addAction(self.ui.actionSelNone)
        menuList.addAction(self.ui.actionSelInvs)
        menuList.exec(QCursor.pos())#显示菜单
if __name__=="__main__":
    QtCore.QCoreApplication.setAttribute(QtCore.Qt.AA_EnableHighDpiScaling)#自适应缩放
    app=QApplication(sys.argv)
    form=QmyMainWindow()
    form.show()
    sys.exit(app.exec_())

QListWidget和QToolButton_第1张图片

你可能感兴趣的:(Pyqt5)