class UserList(ScrollArea): def __init__(self, parent=None): super().__init__() self.parent = parent self.setObjectName('userList') self.userService = self.parent.parent.userService self.drawPanel() self.setLayouts() with open('qss/userList.qss', 'r', encoding="utf-8") as f: self.setStyleSheet(f.read()) self.echoUserList() def drawPanel(self): self.spaceLine = QFrame(self) self.spaceLine.setObjectName("spaceLine") self.spaceLine.setFrameShape(QFrame.HLine) self.spaceLine.setFrameShadow(QFrame.Plain) self.spaceLine.setLineWidth(2) self.userTable = QTableWidget() self.userTable.setObjectName('userTable') self.userTable.setMinimumWidth(self.width()) self.userTable.setColumnCount(6) self.userTable.setHorizontalHeaderLabels( ['用户名', '昵称', '邮箱', '联系方式', '签名', '添加好友']) self.userTable.setColumnWidth(0, self.width() / 6 * 0.9) self.userTable.setColumnWidth(1, self.width() / 6 * 0.9) self.userTable.setColumnWidth(2, self.width() / 6 * 1.1) self.userTable.setColumnWidth(3, self.width() / 6 * 1.1) self.userTable.setColumnWidth(4, self.width() / 6 * 1.5) self.userTable.setColumnWidth(5, self.width() / 6 * 0.5) self.userTable.horizontalHeader().setStretchLastSection(True) self.userTable.verticalHeader().setVisible(False) self.userTable.setShowGrid(False) self.userTable.setAlternatingRowColors(True) self.userTable.setEditTriggers(QAbstractItemView.NoEditTriggers) self.userTable.setSelectionBehavior(QAbstractItemView.SelectRows) def setLayouts(self): self.mainLayout = VBoxLayout() self.mainLayout.addSpacing(10) self.contentLayout = HBoxLayout() self.mainLayout.addWidget(self.spaceLine) self.mainLayout.addSpacing(10) self.contentLayout.addSpacing(30) self.contentLayout.addWidget(self.userTable) self.contentLayout.addSpacing(30) self.mainLayout.addLayout(self.contentLayout) self.mainLayout.addSpacing(30) self.setLayout(self.mainLayout) def echoUserList(self): allUser = self.userService.selectAll() for user in allUser: rowCount = self.userTable.rowCount() self.userTable.setRowCount(rowCount + 1) userInfo = [ user.user_name, user.nick_name, user.email, user.phone, user.sign ] for i in range(5): self.userTable.setItem(rowCount, i, QTableWidgetItem(userInfo[i]))
class MainWindow(QWidget): def __init__(self, parent=None): super(MainWindow, self).__init__() self.parent = parent self.user = self.parent.user self.setObjectName("MainWindow") self.setWindowFlags(Qt.FramelessWindowHint) self.setWindowIcon(QIcon('resource/header.PNG')) self.setWindowTitle("酌酒援北斗") self.resize(1022, 670) self.header = Header(self) self.navigation = Navigation(self) self.personalContent = Personal(self) self.userAdd = UserAdd(self) self.userList = UserList(self) self.blogAdd = BlogAdd(self) self.ownBlog = BlogList(self, self.user.id) self.blogList = BlogList(self) self.mainContents = QStackedWidget() self.mainContents.addWidget(self.personalContent) self.mainContents.addWidget(self.userAdd) self.mainContents.addWidget(self.userList) self.mainContents.addWidget(self.blogList) self.mainContents.addWidget(self.ownBlog) self.mainContents.addWidget(self.blogAdd) self.mainContents.setCurrentIndex(0) # 设置布局小细线。 self.setLines() # 设置布局。 self.setLayouts() with open('qss/window.qss', 'r') as f: self.setStyleSheet(f.read()) def setLines(self): """设置布局小细线。""" self.line1 = QFrame(self) self.line1.setObjectName("line1") self.line1.setFrameShape(QFrame.HLine) self.line1.setFrameShadow(QFrame.Plain) self.line1.setLineWidth(2) def setLayouts(self): self.mainLayout = QVBoxLayout() self.mainLayout.addWidget(self.header) self.mainLayout.addWidget(self.line1) self.contentLayout = QHBoxLayout() self.contentLayout.setStretch(0, 70) self.contentLayout.setStretch(1, 570) self.contentLayout.addWidget(self.navigation) self.contentLayout.addWidget(self.mainContents) self.contentLayout.setSpacing(0) self.contentLayout.setContentsMargins(0, 0, 0, 0) self.mainLayout.addLayout(self.contentLayout) # self.mainLayout.addWidget(self.playWidgets) self.mainLayout.setStretch(0, 43) self.mainLayout.setStretch(1, 0) self.mainLayout.setStretch(2, 576) self.mainLayout.setStretch(3, 50) self.mainLayout.setSpacing(0) self.mainLayout.setContentsMargins(0, 0, 0, 0) self.setLayout(self.mainLayout)