从按键事件设置小部件

Posted

技术标签:

【中文标题】从按键事件设置小部件【英文标题】:set widget from keypress event 【发布时间】:2017-05-02 17:44:04 【问题描述】:

当我按下空格键时,我正在尝试设置一个小部件。 我有一个包含 QLabel 的 QGridLayout。 我想在 QGridLayout 中从您的坐标中设置特定 Qlabel 的文本,但是当我从我的按键函数访问 QGridLayout 时,该变量为 null。

import sys
from PyQt5 import QtCore, QtWidgets
from PyQt5.QtWidgets import QMainWindow, QLabel, QGridLayout, QWidget
from PyQt5.QtCore import QSize    

class Position:
   def __init__(self, x, y):
      self.x = x
      self.y = y

class HelloWindow(QMainWindow):
    global gridLayout
    def __init__(self):
        QMainWindow.__init__(self)

        self.setMinimumSize(QSize(640, 640))    
        self.setWindowTitle("Hello world") 

        centralWidget = QWidget(self)          
        self.setCentralWidget(centralWidget)   

        gridLayout = QGridLayout(self)
        gridLayout.setSpacing(0)  
        centralWidget.setLayout(gridLayout)  

        file_text = open("logic/file.txt", "r")
        file = file_text.read()
        file = file.replace("[(","")
        file = file.replace(")]","")
        file = file.replace("),(",";")
        positions = file.split(';')
        lista = []
        for pos in positions:
         coord = pos.split(',')
         temp = Position(int(coord[0]),int(coord[1]))
         lista.append(temp)
        file_text.close()

        cont = 0
        for x in range(0, 9):
         for y in range(0, 9):
            if cont < 64:
               title = QLabel(str(cont+1), self) 
               title.setAlignment(QtCore.Qt.AlignCenter)
               title.setContentsMargins(0,0,0,0)
               title.setStyleSheet('QLabel background-color: white; color: red; font-size: 24px; font-weight: bold')
               if cont%2 == 0:
                  title.setStyleSheet('QLabel background-color: black; color: red; font-size: 24px; font-weight: bold')
               gridLayout.addWidget(title,lista[cont].x,lista[cont].y)
               cont += 1

    def keyPressEvent(self, event):
      if event.key() == QtCore.Qt.Key_Escape:
         self.close()
      if event.key() == QtCore.Qt.Key_Space:
         item = gridLayout.itemAtPosition(1,1)
         w = item.widget()
         w.setText("test")

if __name__ == "__main__":
    app = QtWidgets.QApplication(sys.argv)
    mainWin = HelloWindow()
    mainWin.show()
    sys.exit( app.exec_() )

下一节是测试

def keyPressEvent(self, event):
      if event.key() == QtCore.Qt.Key_Escape:
         self.close()
      if event.key() == QtCore.Qt.Key_Space:
         item = gridLayout.itemAtPosition(1,1)
         w = item.widget()
         w.setText("test")

我从您的坐标访问小部件以设置您的文本。

【问题讨论】:

【参考方案1】:

问题源于对全局变量的误用,在您的情况下,每次使用时都必须声明:

class HelloWindow(QMainWindow):
    def __init__(self):
        QMainWindow.__init__(self)
        [...]
        global gridLayout
        gridLayout = QGridLayout(self)
        [...]

    def keyPressEvent(self, event):
      if event.key() == QtCore.Qt.Key_Escape:
         self.close()
      if event.key() == QtCore.Qt.Key_Space:
        global gridLayout
        item = gridLayout.itemAtPosition(1, 1)
        w = item.widget()
        w.setText("test")

更优雅的解决方案是将gridLayout 声明为类的属性,为此您必须在所有情况下将gridLayout 更改为self.gridLayout

class Position:
   def __init__(self, x, y):
      self.x = x
      self.y = y

class HelloWindow(QMainWindow):
    def __init__(self):
        QMainWindow.__init__(self)

        self.setMinimumSize(QSize(640, 640))    
        self.setWindowTitle("Hello world") 

        centralWidget = QWidget(self)          
        self.setCentralWidget(centralWidget)   
        self.gridLayout = QGridLayout(self)
        self.gridLayout.setSpacing(0)  
        centralWidget.setLayout(self.gridLayout)  

        file_text = open("logic/file.txt", "r")
        file = file_text.read()
        file = file.replace("[(", "")
        file = file.replace(")]", "")
        file = file.replace("),(", ";")
        positions = file.split(';')
        lista = []
        for pos in positions:
            coord = pos.split(',')
            temp = Position(int(coord[0]), int(coord[1]))
            lista.append(temp)
        file_text.close()

        cont = 0
        for x in range(0, 9):
            for y in range(0, 9):
                if cont < 64:
                    title = QLabel(str(cont+1), self) 
                    title.setAlignment(QtCore.Qt.AlignCenter)
                    title.setContentsMargins(0, 0, 0, 0)
                    title.setStyleSheet('QLabel background-color: white; color: red; font-size: 24px; font-weight: bold')
                    if cont%2 == 0:
                       title.setStyleSheet('QLabel background-color: black; color: red; font-size: 24px; font-weight: bold')
                    self.gridLayout.addWidget(title, lista[cont].x, lista[cont].y)
                    cont += 1

    def keyPressEvent(self, event):
        if event.key() == QtCore.Qt.Key_Escape:
            self.close()
        if event.key() == QtCore.Qt.Key_Space:
            item = self.gridLayout.itemAtPosition(1, 1)
            w = item.widget()
            w.setText("test")

if __name__ == "__main__":
    app = QtWidgets.QApplication(sys.argv)
    mainWin = HelloWindow()
    mainWin.show()
    sys.exit(app.exec_())

【讨论】:

我没有看到对 keyPressEvent 的任何引用..它是否像那样工作

以上是关于从按键事件设置小部件的主要内容,如果未能解决你的问题,请参考以下文章

如何在子窗口小部件下使用 eventfilter 来捕获自定义事件

从 QGraphicsView 向前按键

有没有办法区分 Qt 小部件是通过鼠标单击还是从表格按键获得焦点?

PySide2 QOpenGLWidget 按键事件不起作用

JavaFX实战:几种事件监听实现,键盘按键事件监听,鼠标滚轮事件监听,鼠标按键事件监听,鼠标移动事件监听

JavaFX实战:几种事件监听实现,键盘按键事件监听,鼠标滚轮事件监听,鼠标按键事件监听,鼠标移动事件监听