Skip to content

Commit 90894d0

Browse files
committed
update Item
1 parent 6135016 commit 90894d0

File tree

13 files changed

+569
-1
lines changed

13 files changed

+569
-1
lines changed

.settings/org.eclipse.core.resources.prefs

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
eclipse.preferences.version=1
22
encoding//ActiveX/\u663E\u793Aword_excel_pdf.py=utf-8
3-
encoding//Tmps/FaderWidget.py=utf-8
3+
encoding//Item\ Views/QTableView/CopyContent/CopyContent.py=utf-8
4+
encoding//Item\ Views/QTableView/CopyContent/__main__.py=utf-8
5+
encoding//Item\ Widgets/QTableWidget/SqlQuery/SqlQuery.py=utf-8
6+
encoding//Item\ Widgets/QTableWidget/SqlQuery/__main__.py=utf-8
47
encoding//\u4E0B\u62C9\u6846/\u4E0B\u62C9\u9009\u62E9\u8054\u52A8/\u4E0B\u62C9\u9009\u62E9\u8054\u52A8.py=utf-8
58
encoding//\u5176\u5B83/C\u548CC++\u6269\u5C55/py\u8F6Cpyd/pydmod.py=utf-8
69
encoding//\u5176\u5B83/QRC\u8D44\u6E90\u6587\u4EF6\u4F7F\u7528/qrctest1.py=utf-8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
#!/usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
4+
'''
5+
Created on 2017年4月6日
6+
@author: Irony."[讽刺]
7+
@site: https://pyqt5.com, https://github.com/892768447
8+
@email: 892768447@qq.com
9+
@file: CopyContent
10+
@description:
11+
'''
12+
from PyQt5.QtCore import Qt
13+
from PyQt5.QtGui import QStandardItemModel, QStandardItem
14+
from PyQt5.QtWidgets import QTableView, QApplication, QAction, QMessageBox
15+
16+
17+
__Author__ = "By: Irony.\"[讽刺]\nQQ: 892768447\nEmail: 892768447@qq.com"
18+
__Copyright__ = "Copyright (c) 2017 Irony.\"[讽刺]"
19+
__Version__ = "Version 1.0"
20+
21+
22+
class TableView(QTableView):
23+
24+
def __init__(self, parent=None):
25+
super(TableView, self).__init__(parent)
26+
self.resize(800, 600)
27+
self.setContextMenuPolicy(Qt.ActionsContextMenu) # 右键菜单
28+
self.setEditTriggers(self.NoEditTriggers) # 禁止编辑
29+
self.doubleClicked.connect(self.onDoubleClick)
30+
self.addAction(QAction("复制", self, triggered=self.copyData))
31+
self.myModel = QStandardItemModel() # model
32+
self.initHeader() # 初始化表头
33+
self.setModel(self.myModel)
34+
self.initData() # 初始化模拟数据
35+
36+
def onDoubleClick(self, index):
37+
print(index.row(), index.column(), index.data())
38+
39+
def keyPressEvent(self, event):
40+
super(TableView, self).keyPressEvent(event)
41+
# Ctrl + C
42+
if event.modifiers() == Qt.ControlModifier and event.key() == Qt.Key_C:
43+
self.copyData()
44+
45+
def copyData(self):
46+
count = len(self.selectedIndexes())
47+
if count == 0:
48+
return
49+
if count == 1: # 只复制了一个
50+
QApplication.clipboard().setText(
51+
self.selectedIndexes()[0].data()) # 复制到剪贴板中
52+
QMessageBox.information(self, "提示", "已复制一个数据")
53+
return
54+
rows = set()
55+
cols = set()
56+
for index in self.selectedIndexes(): # 得到所有选择的
57+
rows.add(index.row())
58+
cols.add(index.column())
59+
# print(index.row(),index.column(),index.data())
60+
if len(rows) == 1: # 一行
61+
QApplication.clipboard().setText("\t".join(
62+
[index.data() for index in self.selectedIndexes()])) # 复制
63+
QMessageBox.information(self, "提示", "已复制一行数据")
64+
return
65+
if len(cols) == 1: # 一列
66+
QApplication.clipboard().setText("\r\n".join(
67+
[index.data() for index in self.selectedIndexes()])) # 复制
68+
QMessageBox.information(self, "提示", "已复制一列数据")
69+
return
70+
mirow, marow = min(rows), max(rows) # 最(少/多)行
71+
micol, macol = min(cols), max(cols) # 最(少/多)列
72+
print(mirow, marow, micol, macol)
73+
arrays = [
74+
[
75+
"" for _ in range(macol - micol + 1)
76+
] for _ in range(marow - mirow + 1)
77+
] # 创建二维数组(并排除前面的空行和空列)
78+
print(arrays)
79+
# 填充数据
80+
for index in self.selectedIndexes(): # 遍历所有选择的
81+
arrays[index.row() - mirow][index.column() - micol] = index.data()
82+
print(arrays)
83+
data = "" # 最后的结果
84+
for row in arrays:
85+
data += "\t".join(row) + "\r\n"
86+
print(data)
87+
QApplication.clipboard().setText(data) # 复制到剪贴板中
88+
QMessageBox.information(self, "提示", "已复制")
89+
90+
def initHeader(self):
91+
for i in range(5):
92+
self.myModel.setHorizontalHeaderItem(
93+
i, QStandardItem("表头" + str(i + 1)))
94+
95+
def initData(self):
96+
for row in range(100):
97+
for col in range(5):
98+
self.myModel.setItem(
99+
row, col, QStandardItem("row: {row},col: {col}".format(row=row + 1, col=col + 1)))
100+
101+
if __name__ == "__main__":
102+
import sys
103+
app = QApplication(sys.argv)
104+
app.setApplicationName("TableView")
105+
w = TableView()
106+
w.show()
107+
sys.exit(app.exec_())
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#!/usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
4+
"""
5+
Created on 2018年12月6日
6+
@author: Irony
7+
@site: https://pyqt5.com, https://github.com/892768447
8+
@email: 892768447@qq.com
9+
@file:
10+
@description:
11+
"""
12+
13+
__Author__ = """By: Irony
14+
QQ: 892768447
15+
Email: 892768447@qq.com"""
16+
__Copyright__ = 'Copyright (c) 2018 Irony'
17+
__Version__ = 1.0
18+
19+
import sys
20+
21+
from PyQt5.QtWidgets import QApplication
22+
23+
from CopyContent import TableView
24+
25+
26+
app = QApplication(sys.argv)
27+
app.setApplicationName("TableView")
28+
w = TableView()
29+
w.show()
30+
sys.exit(app.exec_())

Item Views/QTableView/README.md

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# QTableView
2+
3+
## [1、表格内容复制](CopyContent)
4+
5+
1. 通过构造一个和选中区域一样的空数组,然后对数组进行填充形成表格
6+
1. 最后循环数组用`\t`进行拼接`join`,换行用`\r\n`
7+
1. 把字符串复制到剪切板中
8+
9+
![截图](ScreenShot/CopyContent1.png)![截图](ScreenShot/CopyContent2.png)
10+
Loading
Loading

Item Widgets/QTableWidget/README.md

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# QTableWidget
2+
3+
## [1、Sqlalchemy动态拼接字段查询显示表格](SqlQuery)
4+
5+
通过判断界面中选择的条件对`Sqlalchemy``model`进行字段拼接从而实现按条件查询
6+
7+
![截图](ScreenShot/SqlQuery.png)
Loading
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
#!/usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
4+
"""
5+
Created on 2018年5月15日
6+
@author: Irony
7+
@site: https://pyqt5.com, https://github.com/892768447
8+
@email: 892768447@qq.com
9+
@file: SqlQuery
10+
@description:
11+
"""
12+
from PyQt5.QtCore import pyqtSlot
13+
from PyQt5.QtWidgets import QWidget, QMessageBox, QTableWidgetItem
14+
from sqlalchemy.engine import create_engine
15+
from sqlalchemy.ext.declarative.api import declarative_base
16+
from sqlalchemy.orm.session import sessionmaker
17+
from sqlalchemy.sql.expression import and_
18+
from sqlalchemy.sql.schema import Column
19+
from sqlalchemy.sql.sqltypes import Integer, Text
20+
from mainui import Ui_Form
21+
22+
__Author__ = """By: Irony
23+
QQ: 892768447
24+
Email: 892768447@qq.com"""
25+
__Copyright__ = "Copyright (c) 2018 Irony"
26+
__Version__ = "Version 1.0"
27+
28+
# engine = create_engine('mysql+mysqldb://root@localhost:3306/tourist?charset=utf8')
29+
engine = create_engine('sqlite:///data.sqlite3', echo=True) # echo 表示开启命令显示
30+
Base = declarative_base()
31+
32+
33+
class Tourist(Base):
34+
35+
__tablename__ = 'tourist'
36+
37+
id = Column(Integer, primary_key=True)
38+
name = Column(Text)
39+
license = Column(Text)
40+
flightnumber = Column(Text)
41+
flightdate = Column(Text)
42+
seatnumber = Column(Text)
43+
boardingport = Column(Text)
44+
no = Column(Text)
45+
departurestation = Column(Text)
46+
destinationstation = Column(Text)
47+
48+
49+
class Window(QWidget, Ui_Form):
50+
51+
def __init__(self, *args, **kwargs):
52+
super(Window, self).__init__(*args, **kwargs)
53+
self.setupUi(self)
54+
# sql的拼接字段
55+
self.sql = {}
56+
# 数据库连接
57+
self.session = sessionmaker(bind=engine)()
58+
59+
@pyqtSlot()
60+
def on_pushButtonQuery_clicked(self):
61+
"""查询按钮"""
62+
self.applyName()
63+
self.applySeat()
64+
self.applyLicense()
65+
self.applyPort()
66+
if not self.sql:
67+
return QMessageBox.warning(self, '提示', '没有进行任何输入')
68+
# 清空数据
69+
self.tableWidget.clear()
70+
# 重新设置表头
71+
self.tableWidget.setHorizontalHeaderLabels(
72+
['编号', '姓名', '证件号', '航班号', '航班日期', '座位号', '登机口', '序号', '出发地', '目的地'])
73+
# 根据选择的字段进行并列查询
74+
rets = self.session.query(Tourist).filter(
75+
and_(*(key == value for key, value in self.sql.items()))).all()
76+
if not rets:
77+
return QMessageBox.information(self, '提示', '未查询到结果')
78+
self.tableWidget.setRowCount(len(rets))
79+
# 根据查询结果添加到表格中
80+
for row, tourist in enumerate(rets):
81+
self.tableWidget.setItem(row, 0, QTableWidgetItem(str(tourist.id)))
82+
self.tableWidget.setItem(
83+
row, 1, QTableWidgetItem(str(tourist.name)))
84+
self.tableWidget.setItem(
85+
row, 2, QTableWidgetItem(str(tourist.license)))
86+
self.tableWidget.setItem(
87+
row, 3, QTableWidgetItem(str(tourist.flightnumber)))
88+
self.tableWidget.setItem(
89+
row, 4, QTableWidgetItem(str(tourist.flightdate)))
90+
self.tableWidget.setItem(
91+
row, 5, QTableWidgetItem(str(tourist.seatnumber)))
92+
self.tableWidget.setItem(
93+
row, 6, QTableWidgetItem(str(tourist.boardingport)))
94+
self.tableWidget.setItem(row, 7, QTableWidgetItem(str(tourist.no)))
95+
self.tableWidget.setItem(
96+
row, 8, QTableWidgetItem(str(tourist.departurestation)))
97+
self.tableWidget.setItem(
98+
row, 9, QTableWidgetItem(str(tourist.destinationstation)))
99+
100+
def applyName(self):
101+
"""姓名"""
102+
if not self.checkBoxName.isChecked():
103+
if Tourist.name in self.sql:
104+
# 移除
105+
self.sql.pop(Tourist.name)
106+
# 更新或添加到字典里
107+
else:
108+
self.sql[Tourist.name] = self.lineEditName.text().strip()
109+
110+
def applySeat(self):
111+
"""座位号"""
112+
if not self.checkBoxSeat.isChecked():
113+
if Tourist.seatnumber in self.sql:
114+
# 移除
115+
self.sql.pop(Tourist.seatnumber)
116+
# 更新或添加到字典里
117+
else:
118+
self.sql[Tourist.seatnumber] = self.lineEditSeat.text().strip()
119+
120+
def applyLicense(self):
121+
"""证件号"""
122+
if not self.checkBoxLicense.isChecked():
123+
if Tourist.license in self.sql:
124+
# 移除
125+
self.sql.pop(Tourist.license)
126+
# 更新或添加到字典里
127+
else:
128+
self.sql[Tourist.license] = self.lineEditLicense.text().strip()
129+
130+
def applyPort(self):
131+
"""登机口"""
132+
if not self.checkBoxPort.isChecked():
133+
if Tourist.boardingport in self.sql:
134+
# 移除
135+
self.sql.pop(Tourist.boardingport)
136+
# 更新或添加到字典里
137+
else:
138+
self.sql[Tourist.boardingport] = self.lineEditPort.text().strip()
139+
140+
141+
if __name__ == '__main__':
142+
import sys
143+
import cgitb
144+
sys.excepthook = cgitb.Hook(1, None, 5, sys.stderr, 'text')
145+
from PyQt5.QtWidgets import QApplication
146+
app = QApplication(sys.argv)
147+
w = Window()
148+
w.show()
149+
sys.exit(app.exec_())
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#!/usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
4+
"""
5+
Created on 2018年12月6日
6+
@author: Irony
7+
@site: https://pyqt5.com, https://github.com/892768447
8+
@email: 892768447@qq.com
9+
@file:
10+
@description:
11+
"""
12+
13+
__Author__ = """By: Irony
14+
QQ: 892768447
15+
Email: 892768447@qq.com"""
16+
__Copyright__ = 'Copyright (c) 2018 Irony'
17+
__Version__ = 1.0
18+
19+
import sys
20+
21+
from PyQt5.QtWidgets import QApplication
22+
23+
from SqlQuery import Window
24+
25+
26+
app = QApplication(sys.argv)
27+
w = Window()
28+
w.show()
29+
sys.exit(app.exec_())
Binary file not shown.

0 commit comments

Comments
 (0)