diff --git a/.settings/org.eclipse.core.resources.prefs b/.settings/org.eclipse.core.resources.prefs index 83e8587..f58db9a 100644 --- a/.settings/org.eclipse.core.resources.prefs +++ b/.settings/org.eclipse.core.resources.prefs @@ -1,6 +1,9 @@ eclipse.preferences.version=1 encoding//ActiveX/\u663E\u793Aword_excel_pdf.py=utf-8 -encoding//Tmps/FaderWidget.py=utf-8 +encoding//Item\ Views/QTableView/CopyContent/CopyContent.py=utf-8 +encoding//Item\ Views/QTableView/CopyContent/__main__.py=utf-8 +encoding//Item\ Widgets/QTableWidget/SqlQuery/SqlQuery.py=utf-8 +encoding//Item\ Widgets/QTableWidget/SqlQuery/__main__.py=utf-8 encoding//\u4E0B\u62C9\u6846/\u4E0B\u62C9\u9009\u62E9\u8054\u52A8/\u4E0B\u62C9\u9009\u62E9\u8054\u52A8.py=utf-8 encoding//\u5176\u5B83/C\u548CC++\u6269\u5C55/py\u8F6Cpyd/pydmod.py=utf-8 encoding//\u5176\u5B83/QRC\u8D44\u6E90\u6587\u4EF6\u4F7F\u7528/qrctest1.py=utf-8 diff --git a/Item Views/QTableView/CopyContent/CopyContent.py b/Item Views/QTableView/CopyContent/CopyContent.py new file mode 100644 index 0000000..fa93310 --- /dev/null +++ b/Item Views/QTableView/CopyContent/CopyContent.py @@ -0,0 +1,107 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +''' +Created on 2017年4月6日 +@author: Irony."[讽刺] +@site: https://pyqt5.com, https://github.com/892768447 +@email: 892768447@qq.com +@file: CopyContent +@description: +''' +from PyQt5.QtCore import Qt +from PyQt5.QtGui import QStandardItemModel, QStandardItem +from PyQt5.QtWidgets import QTableView, QApplication, QAction, QMessageBox + + +__Author__ = "By: Irony.\"[讽刺]\nQQ: 892768447\nEmail: 892768447@qq.com" +__Copyright__ = "Copyright (c) 2017 Irony.\"[讽刺]" +__Version__ = "Version 1.0" + + +class TableView(QTableView): + + def __init__(self, parent=None): + super(TableView, self).__init__(parent) + self.resize(800, 600) + self.setContextMenuPolicy(Qt.ActionsContextMenu) # 右键菜单 + self.setEditTriggers(self.NoEditTriggers) # 禁止编辑 + self.doubleClicked.connect(self.onDoubleClick) + self.addAction(QAction("复制", self, triggered=self.copyData)) + self.myModel = QStandardItemModel() # model + self.initHeader() # 初始化表头 + self.setModel(self.myModel) + self.initData() # 初始化模拟数据 + + def onDoubleClick(self, index): + print(index.row(), index.column(), index.data()) + + def keyPressEvent(self, event): + super(TableView, self).keyPressEvent(event) + # Ctrl + C + if event.modifiers() == Qt.ControlModifier and event.key() == Qt.Key_C: + self.copyData() + + def copyData(self): + count = len(self.selectedIndexes()) + if count == 0: + return + if count == 1: # 只复制了一个 + QApplication.clipboard().setText( + self.selectedIndexes()[0].data()) # 复制到剪贴板中 + QMessageBox.information(self, "提示", "已复制一个数据") + return + rows = set() + cols = set() + for index in self.selectedIndexes(): # 得到所有选择的 + rows.add(index.row()) + cols.add(index.column()) + # print(index.row(),index.column(),index.data()) + if len(rows) == 1: # 一行 + QApplication.clipboard().setText("\t".join( + [index.data() for index in self.selectedIndexes()])) # 复制 + QMessageBox.information(self, "提示", "已复制一行数据") + return + if len(cols) == 1: # 一列 + QApplication.clipboard().setText("\r\n".join( + [index.data() for index in self.selectedIndexes()])) # 复制 + QMessageBox.information(self, "提示", "已复制一列数据") + return + mirow, marow = min(rows), max(rows) # 最(少/多)行 + micol, macol = min(cols), max(cols) # 最(少/多)列 + print(mirow, marow, micol, macol) + arrays = [ + [ + "" for _ in range(macol - micol + 1) + ] for _ in range(marow - mirow + 1) + ] # 创建二维数组(并排除前面的空行和空列) + print(arrays) + # 填充数据 + for index in self.selectedIndexes(): # 遍历所有选择的 + arrays[index.row() - mirow][index.column() - micol] = index.data() + print(arrays) + data = "" # 最后的结果 + for row in arrays: + data += "\t".join(row) + "\r\n" + print(data) + QApplication.clipboard().setText(data) # 复制到剪贴板中 + QMessageBox.information(self, "提示", "已复制") + + def initHeader(self): + for i in range(5): + self.myModel.setHorizontalHeaderItem( + i, QStandardItem("表头" + str(i + 1))) + + def initData(self): + for row in range(100): + for col in range(5): + self.myModel.setItem( + row, col, QStandardItem("row: {row},col: {col}".format(row=row + 1, col=col + 1))) + +if __name__ == "__main__": + import sys + app = QApplication(sys.argv) + app.setApplicationName("TableView") + w = TableView() + w.show() + sys.exit(app.exec_()) diff --git a/Item Views/QTableView/CopyContent/__main__.py b/Item Views/QTableView/CopyContent/__main__.py new file mode 100644 index 0000000..afe68fc --- /dev/null +++ b/Item Views/QTableView/CopyContent/__main__.py @@ -0,0 +1,30 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +""" +Created on 2018年12月6日 +@author: Irony +@site: https://pyqt5.com, https://github.com/892768447 +@email: 892768447@qq.com +@file: +@description: +""" + +__Author__ = """By: Irony +QQ: 892768447 +Email: 892768447@qq.com""" +__Copyright__ = 'Copyright (c) 2018 Irony' +__Version__ = 1.0 + +import sys + +from PyQt5.QtWidgets import QApplication + +from CopyContent import TableView + + +app = QApplication(sys.argv) +app.setApplicationName("TableView") +w = TableView() +w.show() +sys.exit(app.exec_()) \ No newline at end of file diff --git a/Item Views/QTableView/README.md b/Item Views/QTableView/README.md index e69de29..61b5bcf 100644 --- a/Item Views/QTableView/README.md +++ b/Item Views/QTableView/README.md @@ -0,0 +1,10 @@ +# QTableView + +## [1、表格内容复制](CopyContent) + +1. 通过构造一个和选中区域一样的空数组,然后对数组进行填充形成表格 +1. 最后循环数组用`\t`进行拼接`join`,换行用`\r\n` +1. 把字符串复制到剪切板中 + +![截图](ScreenShot/CopyContent1.png)![截图](ScreenShot/CopyContent2.png) + diff --git a/Item Views/QTableView/ScreenShot/CopyContent1.png b/Item Views/QTableView/ScreenShot/CopyContent1.png new file mode 100644 index 0000000..0034297 Binary files /dev/null and b/Item Views/QTableView/ScreenShot/CopyContent1.png differ diff --git a/Item Views/QTableView/ScreenShot/CopyContent2.png b/Item Views/QTableView/ScreenShot/CopyContent2.png new file mode 100644 index 0000000..6c7118e Binary files /dev/null and b/Item Views/QTableView/ScreenShot/CopyContent2.png differ diff --git a/Item Widgets/QTableWidget/README.md b/Item Widgets/QTableWidget/README.md index e69de29..9c382ee 100644 --- a/Item Widgets/QTableWidget/README.md +++ b/Item Widgets/QTableWidget/README.md @@ -0,0 +1,7 @@ +# QTableWidget + +## [1、Sqlalchemy动态拼接字段查询显示表格](SqlQuery) + +通过判断界面中选择的条件对`Sqlalchemy`的`model`进行字段拼接从而实现按条件查询 + +![截图](ScreenShot/SqlQuery.png) \ No newline at end of file diff --git a/Item Widgets/QTableWidget/ScreenShot/SqlQuery.png b/Item Widgets/QTableWidget/ScreenShot/SqlQuery.png new file mode 100644 index 0000000..9776199 Binary files /dev/null and b/Item Widgets/QTableWidget/ScreenShot/SqlQuery.png differ diff --git a/Item Widgets/QTableWidget/SqlQuery/SqlQuery.py b/Item Widgets/QTableWidget/SqlQuery/SqlQuery.py new file mode 100644 index 0000000..6586953 --- /dev/null +++ b/Item Widgets/QTableWidget/SqlQuery/SqlQuery.py @@ -0,0 +1,149 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +""" +Created on 2018年5月15日 +@author: Irony +@site: https://pyqt5.com, https://github.com/892768447 +@email: 892768447@qq.com +@file: SqlQuery +@description: +""" +from PyQt5.QtCore import pyqtSlot +from PyQt5.QtWidgets import QWidget, QMessageBox, QTableWidgetItem +from sqlalchemy.engine import create_engine +from sqlalchemy.ext.declarative.api import declarative_base +from sqlalchemy.orm.session import sessionmaker +from sqlalchemy.sql.expression import and_ +from sqlalchemy.sql.schema import Column +from sqlalchemy.sql.sqltypes import Integer, Text +from mainui import Ui_Form + +__Author__ = """By: Irony +QQ: 892768447 +Email: 892768447@qq.com""" +__Copyright__ = "Copyright (c) 2018 Irony" +__Version__ = "Version 1.0" + +# engine = create_engine('mysql+mysqldb://root@localhost:3306/tourist?charset=utf8') +engine = create_engine('sqlite:///data.sqlite3', echo=True) # echo 表示开启命令显示 +Base = declarative_base() + + +class Tourist(Base): + + __tablename__ = 'tourist' + + id = Column(Integer, primary_key=True) + name = Column(Text) + license = Column(Text) + flightnumber = Column(Text) + flightdate = Column(Text) + seatnumber = Column(Text) + boardingport = Column(Text) + no = Column(Text) + departurestation = Column(Text) + destinationstation = Column(Text) + + +class Window(QWidget, Ui_Form): + + def __init__(self, *args, **kwargs): + super(Window, self).__init__(*args, **kwargs) + self.setupUi(self) + # sql的拼接字段 + self.sql = {} + # 数据库连接 + self.session = sessionmaker(bind=engine)() + + @pyqtSlot() + def on_pushButtonQuery_clicked(self): + """查询按钮""" + self.applyName() + self.applySeat() + self.applyLicense() + self.applyPort() + if not self.sql: + return QMessageBox.warning(self, '提示', '没有进行任何输入') + # 清空数据 + self.tableWidget.clear() + # 重新设置表头 + self.tableWidget.setHorizontalHeaderLabels( + ['编号', '姓名', '证件号', '航班号', '航班日期', '座位号', '登机口', '序号', '出发地', '目的地']) + # 根据选择的字段进行并列查询 + rets = self.session.query(Tourist).filter( + and_(*(key == value for key, value in self.sql.items()))).all() + if not rets: + return QMessageBox.information(self, '提示', '未查询到结果') + self.tableWidget.setRowCount(len(rets)) + # 根据查询结果添加到表格中 + for row, tourist in enumerate(rets): + self.tableWidget.setItem(row, 0, QTableWidgetItem(str(tourist.id))) + self.tableWidget.setItem( + row, 1, QTableWidgetItem(str(tourist.name))) + self.tableWidget.setItem( + row, 2, QTableWidgetItem(str(tourist.license))) + self.tableWidget.setItem( + row, 3, QTableWidgetItem(str(tourist.flightnumber))) + self.tableWidget.setItem( + row, 4, QTableWidgetItem(str(tourist.flightdate))) + self.tableWidget.setItem( + row, 5, QTableWidgetItem(str(tourist.seatnumber))) + self.tableWidget.setItem( + row, 6, QTableWidgetItem(str(tourist.boardingport))) + self.tableWidget.setItem(row, 7, QTableWidgetItem(str(tourist.no))) + self.tableWidget.setItem( + row, 8, QTableWidgetItem(str(tourist.departurestation))) + self.tableWidget.setItem( + row, 9, QTableWidgetItem(str(tourist.destinationstation))) + + def applyName(self): + """姓名""" + if not self.checkBoxName.isChecked(): + if Tourist.name in self.sql: + # 移除 + self.sql.pop(Tourist.name) + # 更新或添加到字典里 + else: + self.sql[Tourist.name] = self.lineEditName.text().strip() + + def applySeat(self): + """座位号""" + if not self.checkBoxSeat.isChecked(): + if Tourist.seatnumber in self.sql: + # 移除 + self.sql.pop(Tourist.seatnumber) + # 更新或添加到字典里 + else: + self.sql[Tourist.seatnumber] = self.lineEditSeat.text().strip() + + def applyLicense(self): + """证件号""" + if not self.checkBoxLicense.isChecked(): + if Tourist.license in self.sql: + # 移除 + self.sql.pop(Tourist.license) + # 更新或添加到字典里 + else: + self.sql[Tourist.license] = self.lineEditLicense.text().strip() + + def applyPort(self): + """登机口""" + if not self.checkBoxPort.isChecked(): + if Tourist.boardingport in self.sql: + # 移除 + self.sql.pop(Tourist.boardingport) + # 更新或添加到字典里 + else: + self.sql[Tourist.boardingport] = self.lineEditPort.text().strip() + + +if __name__ == '__main__': + import sys + import cgitb + sys.excepthook = cgitb.Hook(1, None, 5, sys.stderr, 'text') + from PyQt5.QtWidgets import QApplication + app = QApplication(sys.argv) + w = Window() + w.show() + sys.exit(app.exec_()) diff --git a/Item Widgets/QTableWidget/SqlQuery/__main__.py b/Item Widgets/QTableWidget/SqlQuery/__main__.py new file mode 100644 index 0000000..e3439cb --- /dev/null +++ b/Item Widgets/QTableWidget/SqlQuery/__main__.py @@ -0,0 +1,29 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +""" +Created on 2018年12月6日 +@author: Irony +@site: https://pyqt5.com, https://github.com/892768447 +@email: 892768447@qq.com +@file: +@description: +""" + +__Author__ = """By: Irony +QQ: 892768447 +Email: 892768447@qq.com""" +__Copyright__ = 'Copyright (c) 2018 Irony' +__Version__ = 1.0 + +import sys + +from PyQt5.QtWidgets import QApplication + +from SqlQuery import Window + + +app = QApplication(sys.argv) +w = Window() +w.show() +sys.exit(app.exec_()) \ No newline at end of file diff --git a/Item Widgets/QTableWidget/SqlQuery/data.sqlite3 b/Item Widgets/QTableWidget/SqlQuery/data.sqlite3 new file mode 100644 index 0000000..7b75fb0 Binary files /dev/null and b/Item Widgets/QTableWidget/SqlQuery/data.sqlite3 differ diff --git a/Item Widgets/QTableWidget/SqlQuery/mainui.py b/Item Widgets/QTableWidget/SqlQuery/mainui.py new file mode 100644 index 0000000..d5f7041 --- /dev/null +++ b/Item Widgets/QTableWidget/SqlQuery/mainui.py @@ -0,0 +1,111 @@ +# -*- coding: utf-8 -*- + +# Form implementation generated from reading ui file 'mainui.ui' +# +# Created by: PyQt5 UI code generator 5.5.1 +# +# WARNING! All changes made in this file will be lost! + +from PyQt5 import QtCore, QtGui, QtWidgets + +class Ui_Form(object): + def setupUi(self, Form): + Form.setObjectName("Form") + Form.resize(400, 362) + self.gridLayout = QtWidgets.QGridLayout(Form) + self.gridLayout.setObjectName("gridLayout") + self.checkBoxName = QtWidgets.QCheckBox(Form) + self.checkBoxName.setObjectName("checkBoxName") + self.gridLayout.addWidget(self.checkBoxName, 0, 0, 1, 1) + self.checkBoxSeat = QtWidgets.QCheckBox(Form) + self.checkBoxSeat.setObjectName("checkBoxSeat") + self.gridLayout.addWidget(self.checkBoxSeat, 0, 2, 1, 1) + self.lineEditName = QtWidgets.QLineEdit(Form) + self.lineEditName.setObjectName("lineEditName") + self.gridLayout.addWidget(self.lineEditName, 0, 1, 1, 1) + self.tableWidget = QtWidgets.QTableWidget(Form) + self.tableWidget.setObjectName("tableWidget") + self.tableWidget.setColumnCount(10) + self.tableWidget.setRowCount(0) + item = QtWidgets.QTableWidgetItem() + self.tableWidget.setHorizontalHeaderItem(0, item) + item = QtWidgets.QTableWidgetItem() + self.tableWidget.setHorizontalHeaderItem(1, item) + item = QtWidgets.QTableWidgetItem() + self.tableWidget.setHorizontalHeaderItem(2, item) + item = QtWidgets.QTableWidgetItem() + self.tableWidget.setHorizontalHeaderItem(3, item) + item = QtWidgets.QTableWidgetItem() + self.tableWidget.setHorizontalHeaderItem(4, item) + item = QtWidgets.QTableWidgetItem() + self.tableWidget.setHorizontalHeaderItem(5, item) + item = QtWidgets.QTableWidgetItem() + self.tableWidget.setHorizontalHeaderItem(6, item) + item = QtWidgets.QTableWidgetItem() + self.tableWidget.setHorizontalHeaderItem(7, item) + item = QtWidgets.QTableWidgetItem() + self.tableWidget.setHorizontalHeaderItem(8, item) + item = QtWidgets.QTableWidgetItem() + self.tableWidget.setHorizontalHeaderItem(9, item) + self.gridLayout.addWidget(self.tableWidget, 3, 0, 1, 4) + self.lineEditSeat = QtWidgets.QLineEdit(Form) + self.lineEditSeat.setObjectName("lineEditSeat") + self.gridLayout.addWidget(self.lineEditSeat, 0, 3, 1, 1) + self.lineEditPort = QtWidgets.QLineEdit(Form) + self.lineEditPort.setObjectName("lineEditPort") + self.gridLayout.addWidget(self.lineEditPort, 1, 3, 1, 1) + self.checkBoxPort = QtWidgets.QCheckBox(Form) + self.checkBoxPort.setObjectName("checkBoxPort") + self.gridLayout.addWidget(self.checkBoxPort, 1, 2, 1, 1) + self.checkBoxLicense = QtWidgets.QCheckBox(Form) + self.checkBoxLicense.setObjectName("checkBoxLicense") + self.gridLayout.addWidget(self.checkBoxLicense, 1, 0, 1, 1) + self.lineEditLicense = QtWidgets.QLineEdit(Form) + self.lineEditLicense.setObjectName("lineEditLicense") + self.gridLayout.addWidget(self.lineEditLicense, 1, 1, 1, 1) + self.pushButtonQuery = QtWidgets.QPushButton(Form) + self.pushButtonQuery.setObjectName("pushButtonQuery") + self.gridLayout.addWidget(self.pushButtonQuery, 2, 0, 1, 4) + + self.retranslateUi(Form) + QtCore.QMetaObject.connectSlotsByName(Form) + + def retranslateUi(self, Form): + _translate = QtCore.QCoreApplication.translate + Form.setWindowTitle(_translate("Form", "Form")) + self.checkBoxName.setText(_translate("Form", "姓名")) + self.checkBoxSeat.setText(_translate("Form", "座位号")) + item = self.tableWidget.horizontalHeaderItem(0) + item.setText(_translate("Form", "编号")) + item = self.tableWidget.horizontalHeaderItem(1) + item.setText(_translate("Form", "姓名")) + item = self.tableWidget.horizontalHeaderItem(2) + item.setText(_translate("Form", "证件号")) + item = self.tableWidget.horizontalHeaderItem(3) + item.setText(_translate("Form", "航班号")) + item = self.tableWidget.horizontalHeaderItem(4) + item.setText(_translate("Form", "航班日期")) + item = self.tableWidget.horizontalHeaderItem(5) + item.setText(_translate("Form", "座位号")) + item = self.tableWidget.horizontalHeaderItem(6) + item.setText(_translate("Form", "登机口")) + item = self.tableWidget.horizontalHeaderItem(7) + item.setText(_translate("Form", "序号")) + item = self.tableWidget.horizontalHeaderItem(8) + item.setText(_translate("Form", "出发地")) + item = self.tableWidget.horizontalHeaderItem(9) + item.setText(_translate("Form", "目的地")) + self.checkBoxPort.setText(_translate("Form", "登机口")) + self.checkBoxLicense.setText(_translate("Form", "证件号")) + self.pushButtonQuery.setText(_translate("Form", "查询")) + + +if __name__ == "__main__": + import sys + app = QtWidgets.QApplication(sys.argv) + Form = QtWidgets.QWidget() + ui = Ui_Form() + ui.setupUi(Form) + Form.show() + sys.exit(app.exec_()) + diff --git a/Item Widgets/QTableWidget/SqlQuery/mainui.ui b/Item Widgets/QTableWidget/SqlQuery/mainui.ui new file mode 100644 index 0000000..a523c08 --- /dev/null +++ b/Item Widgets/QTableWidget/SqlQuery/mainui.ui @@ -0,0 +1,122 @@ + + + Form + + + + 0 + 0 + 400 + 362 + + + + Form + + + + + + 姓名 + + + + + + + 座位号 + + + + + + + + + + + 编号 + + + + + 姓名 + + + + + 证件号 + + + + + 航班号 + + + + + 航班日期 + + + + + 座位号 + + + + + 登机口 + + + + + 序号 + + + + + 出发地 + + + + + 目的地 + + + + + + + + + + + + + + 登机口 + + + + + + + 证件号 + + + + + + + + + + 查询 + + + + + + + +