pyqt5常见问题

pyqt5常见问题,第1张

python版本3.8.10

  1. 方便做窗口设计的利器desinger,安装之后pyqt5-tools不会创建快捷方式的问题,pyqt的desinger路径: **\Python38\Lib\site-packages\qt5_applications\Qt\bin\designer.exe
  2. 在desinger设计加载静态资源(网页,本地html)找不到QWebEngineView控件,先创建一个QWidget然后将其提升为QWebEngineView,头文件为PyQt5.QtWebEngineWidgets
  3. 客户端无法拖动的问题,在继承QWidget类下重写鼠标动作函数
	self.m_Position = None
	self.m_flag = None
    def mousePressEvent(self, event):
        if event.button() == Qt.LeftButton:
            self.m_flag = True
            self.m_Position = event.globalPos() - self.pos()  # 获取鼠标相对窗口的位置
            event.accept()
            self.setCursor(QCursor(Qt.OpenHandCursor))  # 更改鼠标图标

    def mouseMoveEvent(self, QMouseEvent):
        if Qt.LeftButton and self.m_flag:
            self.move(QMouseEvent.globalPos() - self.m_Position)  # 更改窗口位置
            QMouseEvent.accept()

    def mouseReleaseEvent(self, QMouseEvent):
        self.m_flag = False
        self.setCursor(QCursor(Qt.ArrowCursor))
  1. 客户端显示位置问题,显示在屏幕中央
screen = QDesktopWidget().screenGeometry()
size = clientInstance.geometry()
widgetInstance.move(int((screen.width() - size.width()) / 2), int((screen.height() - size.height()) / 2))
  1. 客户端可以启动多个exe的问题,使用共享内存变量,相当于一把锁同时只能一个exe占用
share = QSharedMemory()
share.setKey("main_window")
if share.attach():
	logger.warning("程序已启动,请勿重复启动!")
	sys.exit(-1)
if share.create(1):
	goto('regular process step...')
  1. 启动客户端的时候创建快捷方式
def set_shortcut():
    mainpath = os.path.dirname(__file__)
    try:
        filename = mainpath + r"\client.exe"  # 可执行exe绝对路径
        iconname = app_path() + r"\images\client.ico"
        desktop = os.path.join(os.path.expanduser('~'), "Desktop")
        lnkname = desktop + r"\client.lnk"

        shortcut = pythoncom.CoCreateInstance(
            shell.CLSID_ShellLink, None,
            pythoncom.CLSCTX_INPROC_SERVER, shell.IID_IShellLink)
        shortcut.SetPath(filename)
        # 设置快捷方式的起始位置
        shortcut.SetWorkingDirectory(mainpath)
        # 快捷方式桌面图标
        shortcut.SetIconLocation(iconname, 0)
        if os.path.splitext(lnkname)[-1] != '.lnk':
            lnkname += ".lnk"
        shortcut.QueryInterface(pythoncom.IID_IPersistFile).Save(lnkname, 0)

        return True
    except Exception as e:
        logger.warning("创建桌面快捷方式失败:{}", e.args)
        return False

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/langs/570814.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-04-09
下一篇2022-04-09

发表评论

登录后才能评论

评论列表(0条)

    保存