欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

python3+PyQt5重新实现QT事件处理程序

程序员文章站 2023-10-18 20:16:33
本文是对《python qt gui快速编程》的第10章的例子events用python3+pyqt5进行改写,涉及到重新实现qwidget的事件处理程序。本例子涉及到上下...

本文是对《python qt gui快速编程》的第10章的例子events用python3+pyqt5进行改写,涉及到重新实现qwidget的事件处理程序。本例子涉及到上下文菜单,鼠标事件,键盘事件,可作为重新实现事件处理程序的参考。

注:在创建上下文菜单最简单的方式使用qwidget.addaction()把动作添加到窗口部件中,再把窗口部件的上下文菜单策略设置为qt.actionscontextmenu即可,但是如果像本例子一样要根据不同的状态来提供不同的选项,则要重新实现上下文菜单事件处理程序。

#!/usr/bin/env python3
import sys
from pyqt5.qtcore import (qevent, qtimer, qt)
from pyqt5.qtwidgets import (qapplication, qmenu, qwidget)
from pyqt5.qtgui import qpainter

class widget(qwidget):

  def __init__(self, parent=none):
    super(widget, self).__init__(parent)
    self.justdoubleclicked = false
    self.key = ""
    self.text = ""
    self.message = ""
    self.resize(400, 300)
    self.move(100, 100)
    self.setwindowtitle("events")
    qtimer.singleshot(0, self.givehelp) # avoids first resize msg


  def givehelp(self):
    self.text = "click to toggle mouse tracking"
    self.update()


  def closeevent(self, event):
    print("closed")


  def contextmenuevent(self, event):
    menu = qmenu(self)
    oneaction = menu.addaction("&one")
    twoaction = menu.addaction("&two")
    oneaction.triggered.connect(self.one)
    twoaction.triggered.connect(self.two)
    if not self.message:
      menu.addseparator()
      threeaction = menu.addaction("thre&e")
      threeaction.triggered.connect(self.three)
    menu.exec_(event.globalpos())


  def one(self):
    self.message = "menu option one"
    self.update()


  def two(self):
    self.message = "menu option two"
    self.update()


  def three(self):
    self.message = "menu option three"
    self.update()


  def paintevent(self, event):
    text = self.text
    i = text.find("\n\n")
    if i >= 0:
      text = text[0:i]
    if self.key:
      text += "\n\nyou pressed: {0}".format(self.key)
    painter = qpainter(self)
    painter.setrenderhint(qpainter.textantialiasing)
    painter.drawtext(self.rect(), qt.aligncenter, text)
    if self.message:
      painter.drawtext(self.rect(), qt.alignbottom|qt.alignhcenter,
               self.message)
      qtimer.singleshot(5000, self.clearmessage)
      qtimer.singleshot(5000, self.update)

  def clearmessage(self):
    self.message=""

  def resizeevent(self, event):
    self.text = "resized to qsize({0}, {1})".format(
              event.size().width(), event.size().height())
    self.update()


  def mousereleaseevent(self, event):
    if self.justdoubleclicked:
      self.justdoubleclicked = false
    else:
      self.setmousetracking(not self.hasmousetracking())
      if self.hasmousetracking():
        self.text = "mouse tracking is on.\n"+\
            "try moving the mouse!\n"+\
            "single click to switch it off"
      else:
        self.text = "mouse tracking is off.\n"+\
                      "single click to switch it on"
      self.update()


  def mousemoveevent(self, event):
    if not self.justdoubleclicked:
      globalpos = self.maptoglobal(event.pos())
      self.text = "the mouse is at\nqpoint({0}, {1}) "+\
          "in widget coords, and\n"+\
          "qpoint({2}, {3}) in screen coords".format(
          event.pos().x(), event.pos().y(), globalpos.x(),
          globalpos.y())
      self.update()


  def mousedoubleclickevent(self, event):
    self.justdoubleclicked = true
    self.text = "double-clicked."
    self.update()


  def keypressevent(self, event):
    self.key = ""
    if event.key() == qt.key_home:
      self.key = "home"
    elif event.key() == qt.key_end:
      self.key = "end"
    elif event.key() == qt.key_pageup:
      if event.modifiers() & qt.controlmodifier:
        self.key = "ctrl+pageup"
      else:
        self.key = "pageup"
    elif event.key() == qt.key_pagedown:
      if event.modifiers() & qt.controlmodifier:
        self.key = "ctrl+pagedown"
      else:
        self.key = "pagedown"
    elif qt.key_a <= event.key() <= qt.key_z:
      if event.modifiers() & qt.shiftmodifier:
        self.key = "shift+"
      self.key += event.text()
    if self.key:
      self.key = self.key
      self.update()
    else:
      qwidget.keypressevent(self, event)


  def event(self, event):
    if (event.type() == qevent.keypress and
      event.key() == qt.key_tab):
      self.key = "tab captured in event()"
      self.update()
      return true
    return qwidget.event(self, event)

if __name__ == "__main__":
  app = qapplication(sys.argv)
  form = widget()
  form.show()
  app.exec_()

运行结果:

python3+PyQt5重新实现QT事件处理程序

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。