try:
# PySide2
from PySide2 import QtGui, QtWidgets, QtCore, QtPrintSupport
from PySide2.QtCore import Signal, Slot
except ImportError:
# PyQt5
from PyQt5 import QtGui, QtWidgets, QtCore, QtPrintSupport
from PyQt5.QtCore import pyqtSignal as Signal, pyqtSlot as Slot
该库提供 pip 支持:
pip install xinet
Qt5 内容将以此模块进行开发。为了方便调用窗口,封装一个启动界面的 API:
# xinet/run_qt.py
from .Qt.qt5 import QtWidgets
def run(window_type, *args, **kwargs):
import sys
app = QtWidgets.QApplication(sys.argv)
window = window_type(*args, **kwargs)
window.show()
app.exec_()
下面写一个调用的例子:
from xinet import QtCore, QtGui, QtWidgets, Signal
from xinet.run_qt import run
class QMyGraphicsView(QtWidgets.QGraphicsView):
sigMouseMovePoint = Signal(QtCore.QPoint)
# 自定义信号sigMouseMovePoint,当鼠标移动时,在mouseMoveEvent事件中,将当前的鼠标位置发送出去
# QPoint--传递的是view坐标
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
def mouseMoveEvent(self, evt):
pt = evt.pos() # 获取鼠标坐标--view坐标
self.sigMouseMovePoint.emit(pt) # 发送鼠标位置
super().mouseMoveEvent(evt)
class MainWindow(QtWidgets.QMainWindow):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.init_Ui()
rect = QtCore.QRectF(-200, -100, 400, 200)
self.scene = QtWidgets.QGraphicsScene(rect) # 创建场景
# 参数:场景区域
# 场景坐标原点默认在场景中心---场景中心位于界面中心
self.view.setScene(self.scene) # 给视图窗口设置场景
item1 = QtWidgets.QGraphicsRectItem(rect) # 创建矩形---以场景为坐标
item1.setFlags(QtWidgets.QGraphicsItem.ItemIsSelectable |
QtWidgets.QGraphicsItem.ItemIsFocusable | QtWidgets.QGraphicsItem.ItemIsMovable) # 给图元设置标志
# QGraphicsItem.ItemIsSelectable---可选择
# QGraphicsItem.ItemIsFocusable---可设置焦点
# QGraphicsItem.ItemIsMovable---可移动
# QGraphicsItem.ItemIsPanel---
self.scene.addItem(item1) # 给场景添加图元
for pos, color in zip([rect.left(), 0, rect.right()], [QtCore.Qt.red, QtCore.Qt.yellow, QtCore.Qt.blue]):
# 创建椭圆--场景坐标
item = QtWidgets.QGraphicsEllipseItem(-50, -50, 100, 100)
# 参数1 参数2 矩形左上角坐标
# 参数3 参数4 矩形的宽和高
item.setPos(pos, 0) # 给图元设置在场景中的坐标(移动图元)--图元中心坐标
item.setBrush(color) # 设置画刷
item.setFlags(QtWidgets.QGraphicsItem.ItemIsSelectable |
QtWidgets.QGraphicsItem.ItemIsFocusable | QtWidgets.QGraphicsItem.ItemIsMovable)
self.scene.addItem(item)
self.scene.clearSelection() # 【清除选择】
self.view.sigMouseMovePoint.connect(self.slotMouseMovePoint)
def init_Ui(self):
self.resize(600, 400)
self.view = QMyGraphicsView() # 创建视图窗口
self.setCentralWidget(self.view) # 设置中央控件
self.statusbar = self.statusBar() # 添加状态栏
self.labviewcorrd = QtWidgets.QLabel('view坐标:')
self.labviewcorrd.setMinimumWidth(150)
self.statusbar.addWidget(self.labviewcorrd)
self.labscenecorrd = QtWidgets.QLabel('scene坐标:')
self.labscenecorrd.setMinimumWidth(150)
self.statusbar.addWidget(self.labscenecorrd)
self.labitemcorrd = QtWidgets.QLabel('item坐标:')
self.labitemcorrd.setMinimumWidth(150)
self.statusbar.addWidget(self.labitemcorrd)
def slotMouseMovePoint(self, pt):
self.labviewcorrd.setText(f'view坐标:{pt.x()},{pt.y()}')
ptscene = self.view.mapToScene(pt) # 把view坐标转换为场景坐标
self.labscenecorrd.setText(
f'scene坐标:{ptscene.x():.0f},{ptscene.y():.0f}')
item = self.scene.itemAt(
ptscene, self.view.transform()) # 在场景某点寻找图元--最上面的图元
# 返回值:图元地址
if item != None:
ptitem = item.mapFromScene(ptscene) # 把场景坐标转换为图元坐标
self.labitemcorrd.setText(
f'item坐标:{ptitem.x():.0f},{ptitem.y():.0f}')
if __name__ == '__main__':
run(MainWindow)
效果图:
热门评论
厉害了