在 pythonQt 中使用 BoolResult
Posted
技术标签:
【中文标题】在 pythonQt 中使用 BoolResult【英文标题】:Use BoolResult in pythonQt 【发布时间】:2018-02-24 17:56:43 【问题描述】:在 C++ 中,BoolResult 被强制转换为指针:
bool ok;
int i = QInputDialog::getInt(this, tr("QInputDialog::getInteger()"),
tr("Percentage:"), 25, 0, 100, 1, &ok);
if (ok)
integerLabel->setText(tr("%1%").arg(i));
如何在 pyQt/pythonQt 中检索“ok”布尔值?
它尝试了以下不起作用:
from PythonQt.QtGui import QInputDialog, QWidget
from PythonQt.QtCore import Qt, QTimer
from PythonQt import BoolResult
...
x = QWidget()
x.setAttribute(Qt.WA_DeleteOnClose)
ok = BoolResult()
i = QInputDialog.getInt(x, "test", "hallo", 1000, None, None, None, ok)
print("ok:", ok)
print("i:", i)
print("i:", i)
导致
2/24/2018 17:49:42 pyTSon.PluginHost.onMenuItemEvent Error Error calling onMenuItemEvent of python plugin Auto Channel Commander: Traceback (most recent call last):
File "C:/Users/blusc/AppData/Roaming/TS3Client/plugins/pyTSon/scripts\pluginhost.py", line 663, in onMenuItemEvent
plugin.onMenuItemEvent(schid, atype, locid, selectedItemID)
File "C:/Users/blusc/AppData/Roaming/TS3Client/plugins/pyTSon/scripts\autoCommander\__init__.py", line 64, in onMenuItemEvent
i = QInputDialog.getInt(x, "test", "hallo", 1000, None, None, None, ok)
ValueError: Could not find matching overload for given arguments:
(QWidget(0x2759aa87560) , 'test', 'hallo', 1000, None, None, None, BoolResult(False))
The following slots are available:
static getInt(QWidget parent, QString title, QString label, int value, int minValue, int maxValue, int step, PythonQt.BoolResult ok, Qt::WindowFlags flags) -> int
static getInt(QWidget parent, QString title, QString label, int value, int minValue, int maxValue, int step, PythonQt.BoolResult ok) -> int
static getInt(QWidget parent, QString title, QString label, int value, int minValue, int maxValue, int step) -> int
static getInt(QWidget parent, QString title, QString label, int value, int minValue, int maxValue) -> int
static getInt(QWidget parent, QString title, QString label, int value, int minValue) -> int
static getInt(QWidget parent, QString title, QString label, int value) -> int
static getInt(QWidget parent, QString title, QString label) -> int
【问题讨论】:
【参考方案1】:你的问题是你必须分配适当的值,你不应该使用None
,例如如果我们看到Qt的docs:
int QInputDialog::getInt(QWidget *parent, const QString &title, const QString &label, int value = 0, int min = -2147483647, int max = 2147483647, int step = 1, bool *ok = Q_NULLPTR , Qt::WindowFlags 标志 = Qt::WindowFlags())
所以一个可能的解决方案是复制这些值:
ok = BoolResult()
i = QInputDialog.getInt(push1, "test", "hallo", 1000, -2147483647, 2147483647, 1, ok)
print("ok:", ok)
print("i:", i)
【讨论】:
谢谢,就是这样。我相信None
正在通过默认值:)以上是关于在 pythonQt 中使用 BoolResult的主要内容,如果未能解决你的问题,请参考以下文章
Windows下PythonQt编译(vs2015+Qt5.11.2+PythonQt 3.2)