简体   繁体   English

如何在QInputDialog中检测值的变化?

[英]How can I detect the change of value in a QInputDialog?

I want to print the value when changing the value of the dialog called by the getInt method of QInputDialog . 我想在更改由QInputDialoggetInt方法调用的对话框的值时打印该值。

I run the below code, but it is not working: 我运行以下代码,但无法正常工作:

import sys
from PyQt5.QtCore import Slot
from PyQt5.QtWidgets import QApplication, QInputDialog

@Slot(int)
def int_value_changed(val):
    print(val)

if 'qapp' not in globals():
    qapp = QApplication(sys.argv)

dlg = QInputDialog(None)
dlg.intValueChanged.connect(int_value_changed)

dlg.getInt(None, 'title', 'Type Value', 0)

Functions like getInt are static, which means they create an internal instance of QInputDialog which is not directly accessible from code. getInt这样的函数是静态的,这意味着它们创建了QInputDialog的内部实例,该实例无法从代码直接访问。 If you create your own instance of QInputDialog , you must do all the initialisation yourself and then call exec() (just like an ordinary dialog). 如果创建自己的QInputDialog实例, QInputDialog必须自己进行所有初始化,然后调用exec() (就像普通对话框一样)。 As the documentation for QInputDialog shows, this approach is more flexible than using the static functions, since it provides much more scope for customisation. QInputDialog的文档所示,此方法比使用静态函数更灵活,因为它提供了更多的自定义范围。

A roughly equivalent implementation of getInt would be: getInt大致等效实现为:

import sys
from PyQt5.QtWidgets import QApplication, QInputDialog

def int_value_changed(val):
    print(val)

if QApplication.instance() is None:
    qapp = QApplication(sys.argv)

def getInt(parent, title, label, value=0):
    dlg = QInputDialog(parent)
    dlg.setInputMode(QInputDialog.IntInput)
    dlg.setWindowTitle(title)
    dlg.setLabelText(label)
    dlg.setIntValue(value)
    dlg.intValueChanged.connect(int_value_changed)
    accepted = dlg.exec_() == QInputDialog.Accepted
    dlg.deleteLater()
    return dlg.intValue(), accepted

print(getInt(None, 'Title', 'Type Value', 5))

# print(QInputDialog.getInt(None, 'title', 'Type Value', 5))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM