0%

PyQt4--《二十二》无边框窗口及QSS样式使用

未完,待续…

栗子

1
2
3
4
5
6
7
# test.qss
QWidget {
background-color: white;
}
QLabel#smallbtn{
color: red;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#-*-coding:utf-8-*-
# test.py
import sys
from PyQt4.QtCore import *
from PyQt4.QtGui import *

class window(QWidget):
def __init__(self, parent=None):
super(window, self).__init__(parent)
self.init()

def init(self):

#创建布局
hbox = QHBoxLayout()
self.leftlist = QListWidget()
self.Stack = QStackedWidget()
self.toplayout = QHBoxLayout()
#创建对象
self.smallbtn = QLabel(u"最小化")
self.smallbtn.setObjectName("smallbtn")#设定控件类名
self.bigbtn = QLabel(u"最大化")
self.closebtn = QLabel(u"关闭")
self.smallbtn.setAlignment(Qt.AlignRight)
self.bigbtn.setAlignment(Qt.AlignRight)
self.closebtn.setAlignment(Qt.AlignRight)
self.leftlist.insertItem(0,"Contact")#左边添加list子项,添加index
self.leftlist.insertItem(1,"Personal")
self.leftlist.insertItem(2,"Educational")
self.stack1 = QWidget()#添加对象
self.stack2 = QWidget()
self.stack3 = QWidget()
self.stack1Window()#设置对象内容
self.stack2Window()
self.stack3Window()
self.leftlist.currentRowChanged.connect(self.display)#设置listWidget选中行切换事件

#添加对象到布局
self.toplayout.addWidget(self.smallbtn)
self.toplayout.addWidget(self.bigbtn)
self.toplayout.addWidget(self.closebtn)
self.Stack.addWidget(self.stack1)
self.Stack.addWidget(self.stack2)
self.Stack.addWidget(self.stack3)
hbox.addWidget(self.leftlist)
hbox.addWidget(self.Stack)
mainlayout = QVBoxLayout(self)
mainlayout.addLayout(self.toplayout)
mainlayout.addLayout(hbox)
self.setLayout(mainlayout)
self.setWindowTitle(u"关联标签显示")
# 开始装载样式表
file = QFile('test.qss')
file.open(QFile.ReadOnly)
styleSheet = file.readAll()
styleSheet = unicode(styleSheet, encoding='utf8')
self.setStyleSheet(styleSheet)
#鼠标轨迹
self.setMouseTracking(True)
#窗口居中展示
desktop = QApplication.desktop()
width = desktop.width()
height = desktop.height()
if (width/height) <3 :
self.move((width - self.width())/2, (height - self.height())/2)
# 无边框
self.setWindowFlags(Qt.FramelessWindowHint)

# 支持窗口拖动,重写两个方法
def mousePressEvent(self, event):
if event.button() == Qt.LeftButton:
self.m_drag = True
self.m_DragPosition = event.globalPos() - self.pos()
event.accept()

def mouseMoveEvent(self, QMouseEvent):
if QMouseEvent.buttons() and Qt.LeftButton:
self.move(QMouseEvent.globalPos() - self.m_DragPosition)
QMouseEvent.accept()

def mouseReleaseEvent(self, QMouseEvent):
self.m_drag = True

def stack1Window(self):
layout = QFormLayout()
layout.addRow("Name", QLineEdit())
layout.addRow("Address", QLineEdit())
# self.setTabText(0,"Contact Details")
self.stack1.setLayout(layout)

def stack2Window(self):
layout = QFormLayout()
sex = QHBoxLayout()
sex.addWidget(QRadioButton("Male"))
sex.addWidget(QRadioButton("Female"))
layout.addRow(QLabel("Sex"), sex)
layout.addRow("Date of Birth", QLineEdit())

self.stack2.setLayout(layout)

def stack3Window(self):
layout = QHBoxLayout()
layout.addWidget(QLabel("subjects"))
layout.addWidget(QCheckBox("Physics"))
layout.addWidget(QCheckBox("Maths"))
self.stack3.setLayout(layout)

def display(self, i):
self.Stack.setCurrentIndex(i)

def main():
app = QApplication(sys.argv)
ex = window()
ex.show()
sys.exit(app.exec_())

if __name__ == '__main__':
main()