import os import sys from PyQt5.QtGui import * from PyQt5.QtWidgets import * from PyQt5.QtCore import Qt from utils.QSSLoader import QSSLoader from view.UserLabel import User from view.SelfListWidgetItem import SelfListWidgetItem class MainWidget(QWidget): def __init__(self): super(MainWidget, self).__init__() self.user_label = User() # 主布局,左右两侧 layout = QHBoxLayout() # 子布局,左边的部分 self.sub_layout = QVBoxLayout() self.sub_layout.spacing() self.sub_layout.addWidget(self.user_label, 1) # 左侧列表 self.system_listWidget = QListWidget(self) self.sub_layout.addWidget(self.system_listWidget, 2) # 可自定义添加的listWidget self.self_listWidget = QListWidget(self) self.sub_layout.addWidget(self.self_listWidget, 7) # todo # 需要把下面的重写QLabel解决问题,单个QLabel无法解决问题 self.add_item_label = QLabel('新建列表') self.add_item_label.setMaximumHeight(50) self.add_item_label.setObjectName('add_item_label') # self.add_item_label.setPixmap(QPixmap()) self.sub_layout.addWidget(self.add_item_label, 1) # 禁止双击可编辑 self.self_listWidget.setEditTriggers(QAbstractItemView.NoEditTriggers) # 右键菜单 self.self_listWidget.setContextMenuPolicy(Qt.CustomContextMenu) self.self_listWidget.setObjectName('self_listWidget') self.self_listWidget.customContextMenuRequested.connect(self.myListWidgetContext) # 右侧层叠窗口 self.stackedWidget = QStackedWidget(self) layout.addLayout(self.sub_layout, Qt.AlignJustify) layout.addWidget(self.stackedWidget, 2) self.one_day = SelfListWidgetItem('我的一天', 1, os.getcwd() + '/../images/sun.svg') self.system_listWidget.setCurrentRow(1) # self.one_day.setSelected(True) self.important = SelfListWidgetItem('重要', 0, os.getcwd() + '/../images/star.svg') self.setLayout(layout) self.initUI() def initUI(self): # 系统默认的两个 item self.system_listWidget.addItem(self.one_day) self.system_listWidget.setItemWidget(self.one_day, self.one_day.widget) self.system_listWidget.addItem(self.important) self.system_listWidget.setItemWidget(self.important, self.important.widget) self.two_day = SelfListWidgetItem('我的一天', 1, os.getcwd() + '/../images/sun.svg') self.self_listWidget.addItem(self.two_day) self.self_listWidget.setItemWidget(self.two_day, self.two_day.widget) pass # todo # 仔细研究右键菜单 def myListWidgetContext(self, position): # 设置右键菜单 pop_menu = QMenu(self) rename_action = QAction(u'重命名', self) copy_action = QAction("复制分组", self) del_action = QAction("删除分组", self) # 查看右键时是否在item上面,如果不在.就不显示删除和修改. pop_menu.addAction(rename_action) if self.self_listWidget.itemAt(position): pop_menu.addAction(del_action) pop_menu.addAction(rename_action) rename_action.triggered.connect(self.RenameItem) copy_action.triggered.connect(self.CreateNewItem) del_action.triggered.connect(self.DeleteItem) pop_menu.exec_(self.self_listWidget.mapToGlobal(position)) # 创建新的分组 def CreateNewItem(self): # 创建一个没有名字的item item = QListWidgetItem("") item.setTextAlignment(Qt.AlignCenter) # 使得item是可以编辑的. item.setFlags(item.flags() | Qt.ItemIsEditable) self.self_listWidget.addItem(item) # 创建后就可以编辑item,用户自己起名字. self.self_listWidget.editItem(item) # 删除分组 def DeleteItem(self): self.self_listWidget.takeItem(self.self_listWidget.currentRow()) # 重命名分组 def RenameItem(self): curRow = self.self_listWidget.currentRow() item = self.self_listWidget.item(curRow) item.setFlags(item.flags() | Qt.ItemIsEditable) self.self_listWidget.editItem(item) self.self_listWidget.itemChanged.connect(lambda: self.ChangeItem(item)) def ChangeItem(self, item): print("test") class MainWindow(QWidget): def __init__(self): super(MainWindow, self).__init__() self.resize(1150, 850) self.setWindowTitle("待办事项") # 主布局,用来显示主页面,设置页面等 self.layout = QStackedLayout(self) self.main_widget = MainWidget() # self.main_widget.resize(50, 200) self.layout.addWidget(self.main_widget) if __name__ == "__main__": app = QApplication(sys.argv) main = MainWindow() # print(os.path.abspath('../')) style_sheet = QSSLoader.read_qss_file('../resource/current.qss') main.setStyleSheet(style_sheet) app.setWindowIcon(QIcon(os.path.abspath('../') + '/images/todo.svg')) main.show() sys.exit(app.exec_())