<i id='c4fbk'><tr id='c4fbk'><dt id='c4fbk'><q id='c4fbk'><span id='c4fbk'><b id='c4fbk'><form id='c4fbk'><ins id='c4fbk'></ins><ul id='c4fbk'></ul><sub id='c4fbk'></sub></form><legend id='c4fbk'></legend><bdo id='c4fbk'><pre id='c4fbk'><center id='c4fbk'></center></pre></bdo></b><th id='c4fbk'></th></span></q></dt></tr></i><div id='c4fbk'><tfoot id='c4fbk'></tfoot><dl id='c4fbk'><fieldset id='c4fbk'></fieldset></dl></div>

  • <legend id='c4fbk'><style id='c4fbk'><dir id='c4fbk'><q id='c4fbk'></q></dir></style></legend>

    1. <tfoot id='c4fbk'></tfoot>
          <bdo id='c4fbk'></bdo><ul id='c4fbk'></ul>
      1. <small id='c4fbk'></small><noframes id='c4fbk'>

        通过信号调用函数将默认键控参数更改为“Fals

        时间:2023-08-04
          <i id='SQ7yI'><tr id='SQ7yI'><dt id='SQ7yI'><q id='SQ7yI'><span id='SQ7yI'><b id='SQ7yI'><form id='SQ7yI'><ins id='SQ7yI'></ins><ul id='SQ7yI'></ul><sub id='SQ7yI'></sub></form><legend id='SQ7yI'></legend><bdo id='SQ7yI'><pre id='SQ7yI'><center id='SQ7yI'></center></pre></bdo></b><th id='SQ7yI'></th></span></q></dt></tr></i><div id='SQ7yI'><tfoot id='SQ7yI'></tfoot><dl id='SQ7yI'><fieldset id='SQ7yI'></fieldset></dl></div>

            <tbody id='SQ7yI'></tbody>
          • <bdo id='SQ7yI'></bdo><ul id='SQ7yI'></ul>
              <legend id='SQ7yI'><style id='SQ7yI'><dir id='SQ7yI'><q id='SQ7yI'></q></dir></style></legend>

                <tfoot id='SQ7yI'></tfoot>

                1. <small id='SQ7yI'></small><noframes id='SQ7yI'>

                  本文介绍了通过信号调用函数将默认键控参数更改为“False".为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  当通过信号连接调用函数时

                  mybutton.clicked.connect(myfunction)

                  调用该函数时,其所有参数都设置为 False.即使已设置默认参数.这是预期的行为吗?

                  下面的代码显示了一个简单的示例.对于我的特殊情况,将所有参数设置为 False 并不是一个大问题,因为我可以简单地用 if 语句来捕捉它.但是,我认为了解为什么以这种方式实施它可能对进一步的项目有所帮助.

                  这个简单的程序显示了我所期望的行为.

                  def 函数(foo=None):打印(富)功能()

                  <块引用>

                  但是,在这个最小的 Qt 示例中,函数参数不再是None",而是False".

                  导入系统从 PyQt5.QtWidgets 导入 QMainWindow、QGridLayout、QWidget、QPushButton、QApplication类主窗口(QMainWindow):def __init__(self):QMainWindow.__init__(self)centralWidget = QWidget(self)self.setCentralWidget(centralWidget)gridLayout = QGridLayout(self)centralWidget.setLayout(gridLayout)btn = QPushButton("按钮",self)gridLayout.addWidget(btn)btn.clicked.connect(self.function)def 函数(自我,foo=None):打印(富)如果 __name__ == "__main__":应用程序 = QApplication(sys.argv)mainWin = MainWindow()mainWin.show()sys.exit(app.exec_())

                  <块引用>

                  错误

                  我想,问题是:我是否必须忍受在我的函数中没有可用的默认值(即,一旦我注意到它们被设置为 False,我是否需要重置它们)还是有任何更聪明的方法处理它?<​​/p>

                  解决方案

                  clicked是一个重载信号,即它们是2个同名信号不同签名.

                  clicked = QtCore.pyqtSignal([], [bool])# 点击 = QtCore.pyqtSignal()# clicked = QtCore.pyqtSignal(bool)

                  有关详细信息,请阅读此答案.

                  建立连接时,PyQt 确定将使用哪个信号,在这种情况下,您的函数的参数类似于第二种形式,因此信号将发送布尔值 False,在您的情况下,它将替换默认值.

                  如果您不希望这种情况发生,那么您必须使用 @QtCore.pyqtSlot() 装饰器,以便它不会向您发送任何参数,因此您的函数使用默认参数.

                  导入系统从 PyQt5 导入 QtCore、QtWidgets类 MainWindow(QtWidgets.QMainWindow):def __init__(self, parent=None):超级(主窗口,自我).__init__(父)centralWidget = QtWidgets.QWidget()self.setCentralWidget(centralWidget)gridLayout = QtWidgets.QGridLayout(centralWidget)btn = QtWidgets.QPushButton("按钮")gridLayout.addWidget(btn)btn.clicked.connect(self.function)@QtCore.pyqtSlot()def 函数(自我,foo=None):打印(富)如果 __name__ == "__main__":应用程序 = QtWidgets.QApplication(sys.argv)mainWin = MainWindow()mainWin.show()sys.exit(app.exec_())

                  输出:

                  When calling a function via a signal connection as in

                  mybutton.clicked.connect(myfunction)
                  

                  the function is called with all its arguments set to False. Even though default arguments have been set. Is this the expected behavior?

                  The code below shows a simple example. For my particular case having all arguments set to False is not a big issue as I can simply catch it with an if statement. However I feel that knowledge on why it is implemented this way may be helpful for further projects.

                  This simple program shows the behavior I would expect.

                  def function(foo=None):
                      print(foo)
                  
                  function()
                  

                  None

                  With this minimal Qt example however, the functions argument is no longer 'None', but 'False'.

                  import sys
                  from PyQt5.QtWidgets import QMainWindow, QGridLayout, QWidget, QPushButton, QApplication
                  
                  class MainWindow(QMainWindow):
                      def __init__(self):
                          QMainWindow.__init__(self)
                          centralWidget = QWidget(self)
                          self.setCentralWidget(centralWidget)
                          gridLayout = QGridLayout(self)
                          centralWidget.setLayout(gridLayout)
                          btn = QPushButton("button",self)
                          gridLayout.addWidget(btn)
                          btn.clicked.connect(self.function)
                  
                      def function(self, foo=None):
                          print(foo)
                  
                  if __name__ == "__main__":
                      app = QApplication(sys.argv)
                      mainWin = MainWindow()
                      mainWin.show()
                      sys.exit( app.exec_() )
                  

                  False

                  The question, i guess, is: Do I have to live with no having the default values available in my functions (i.e. do I need to reset them once I notice that they were set to False) or is there any smarter way to handle it?

                  解决方案

                  clicked is an overloaded signal, that is, they are 2 signals with the same name and with different signatures.

                  clicked = QtCore.pyqtSignal([], [bool])
                  # clicked = QtCore.pyqtSignal()
                  # clicked = QtCore.pyqtSignal(bool)
                  

                  For more detail read this answer.

                  When the connection is made PyQt determines which of the signals will use, in this case the arguments of your function are similar to the second form so the signal will send the boolean False, and in your case it will replace the default value.

                  If you do not want that to happen then you must force PyQt to use the first form using the @QtCore.pyqtSlot() decorator so that it does not send you any parameter and therefore your function uses the default parameter.

                  import sys
                  from PyQt5 import QtCore, QtWidgets
                  
                  
                  class MainWindow(QtWidgets.QMainWindow):
                      def __init__(self, parent=None):
                          super(MainWindow, self).__init__(parent)
                          centralWidget = QtWidgets.QWidget()
                          self.setCentralWidget(centralWidget)
                          gridLayout = QtWidgets.QGridLayout(centralWidget)
                          btn = QtWidgets.QPushButton("button")
                          gridLayout.addWidget(btn)
                          btn.clicked.connect(self.function)
                  
                      @QtCore.pyqtSlot()
                      def function(self, foo=None):
                          print(foo)
                  
                  
                  if __name__ == "__main__":
                      app = QtWidgets.QApplication(sys.argv)
                      mainWin = MainWindow()
                      mainWin.show()
                      sys.exit(app.exec_())
                  

                  Output:

                  None
                  

                  这篇关于通过信号调用函数将默认键控参数更改为“False".为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:如何获取 QTableView 右键单击​​索引 下一篇:如何在右侧 cilck 上的 Qdoublespinbox 上向 QtCore.Qt.

                  相关文章

                  最新文章

                  <tfoot id='qN6EM'></tfoot>

                  1. <legend id='qN6EM'><style id='qN6EM'><dir id='qN6EM'><q id='qN6EM'></q></dir></style></legend>

                    <small id='qN6EM'></small><noframes id='qN6EM'>

                      <i id='qN6EM'><tr id='qN6EM'><dt id='qN6EM'><q id='qN6EM'><span id='qN6EM'><b id='qN6EM'><form id='qN6EM'><ins id='qN6EM'></ins><ul id='qN6EM'></ul><sub id='qN6EM'></sub></form><legend id='qN6EM'></legend><bdo id='qN6EM'><pre id='qN6EM'><center id='qN6EM'></center></pre></bdo></b><th id='qN6EM'></th></span></q></dt></tr></i><div id='qN6EM'><tfoot id='qN6EM'></tfoot><dl id='qN6EM'><fieldset id='qN6EM'></fieldset></dl></div>
                      • <bdo id='qN6EM'></bdo><ul id='qN6EM'></ul>