GVKun编程网logo

PyQt4.QtCore.QVariant对象而不是字符串?(qt pyqt)

15

以上就是给各位分享PyQt4.QtCore.QVariant对象而不是字符串?,其中也会对qtpyqt进行解释,同时本文还将给你拓展ApolloClient-如何传递数组而不是字符串?、c–qVari

以上就是给各位分享PyQt4.QtCore.QVariant对象而不是字符串?,其中也会对qt pyqt进行解释,同时本文还将给你拓展ApolloClient - 如何传递数组而不是字符串?、c – qVariantValue是“QT_DEPRECATED” – 替换是什么?、Differences Between PyQt4 and PyQt5、ImportError:无法从“ PyQt4”导入名称“ QtCore”等相关知识,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!

本文目录一览:

PyQt4.QtCore.QVariant对象而不是字符串?(qt pyqt)

PyQt4.QtCore.QVariant对象而不是字符串?(qt pyqt)

我按照下面的示例Key / Value pyqtQComboBox进行操作,使用以下代码将ID值存储到组合框项目中。

self.combox_widget.addItem(''Apples'', ''Green'')indx = self.combox_widget.currentIndex()print self.combox_widget.itemData(indx)

但是,我继续<PyQt4.QtCore.QVariant object at0x02AC1A70>尝试检索DATA值(以上示例中的“绿色”)。我在这里想念什么?

答案1

小编典典

大多数设置和检索任意“数据”的Qt API都会将其存储为QVariant。

对于 Python2 ,默认情况下,PyQt会在您设置它时自动将python对象 转换 为QVariant,但在您 检索
它时不会自动将其再次转换回QVariant 。因此,您必须采取额外的步骤来做到这一点:

    print self.combox_widget.itemData(indx).toPyObject()

对于 Python3 ,默认情况下,始终总是在两个方向上自动完成转换,因此不需要额外的步骤。

要解决此差异,PyQt提供了一种使用sip模块来显式设置默认模式的方法:

import sipsip.setapi(''QVariant'', 2)from PyQt4 import QtCore, QtGui

在导入其他PyQt模块 之前 ,需要 程序开始时执行一次此操作,这将确保任何Qt API都不会返回QVariant。

ApolloClient - 如何传递数组而不是字符串?

ApolloClient - 如何传递数组而不是字符串?

解决方案:

  1. 不需要 JSON.stringify
  2. 变量中的第一个条目需要用双引号括起来
const { data: SubCategory } = await client.query({
    query: getSubCategories,variables: {
      "CatIDs": CatIDs
    },});

c – qVariantValue是“QT_DEPRECATED” – 替换是什么?

c – qVariantValue是“QT_DEPRECATED” – 替换是什么?

我需要将Qt遗留代码从4转换为5.1
现在我在visual studio 2010中有编译错误:

SingleItem* item = qVariantValue<SingleItem*>(index.data());

给我 :

.cpp(63): error C2065: ‘qVariantValue’ : undeclared identifier

当我去标题时,我看到:

#if QT_DEPRECATED_SINCE(5,0)
template<typename T>
inline QT_DEPRECATED T qVariantValue(const QVariant &variant)
{ return qvariant_cast<T>(variant); }

template<typename T>
inline QT_DEPRECATED bool qVariantCanConvert(const QVariant &variant)
{ return variant.template canConvert<T>(); }
#endif

好的…那么更换什么?

解决方法

见 compatibility notes.

对于qVariantValue,请使用QVariant :: value< T>代替.

index.data().value<SingleItem*>();

Differences Between PyQt4 and PyQt5

Differences Between PyQt4 and PyQt5

Differences Between PyQt4 and PyQt5

PyQt5 is not compatibile with PyQt4 (although experience shows that the effort in porting applications from PyQt4 to PyQt5 is not great). This section describes the main differences between the two.

Supported Python Versions

Versions of Python earlier than v2.6 are not supported.

Deprecated Features

PyQt5 does not support any parts of the Qt API that are marked as deprecated or obsolete in Qt v5.0. However it is possible that some of these have been included accidentaly. These are considered bugs and will be removed if found.

Multiple APIs

PyQt4 supports a number of different API versions (QStringQVariant etc.). With the exception of QVariant, PyQt5 only implements v2 of those APIs for all versions of Python. The changed support for QVariant, including the removal of QPyNullVariant, is described in Support for QVariant.

Old-style Signals and Slots

PyQt4’s old-style signals and slots are not supported. Therefore the following are not implemented in PyQt5:

All methods that had arguments that are usually the results of calls to SIGNAL() or SLOT() are no longer supported. There will always be an equivalent that takes a bound signal or callable respectively.

In addition the following methods have differences:

New-style Signals and Slots

Qt implements signals with an optional argument as two separate signals, one with the argument and one without it. PyQt4 exposed both of these allowing you to connect to each of them. However, when emitting the signal, you had to use the signal appropriate to the number of arguments being emitted.

PyQt5 exposes only the signal where all arguments are specified. However it allows any optional arguments to be omitted when emitting the signal.

Unlike PyQt4, PyQt5 supports the definition of properties, signals and slots in classes not sub-classed from QObject (i.e. in mixins).

QtDeclarativeQtScript and QtScriptTools Modules

PyQt4’s QtDeclarativeQtScript and QtScriptTools modules are not supported. These have been replaced by PyQt5’s QtQml and QtQuick modules. Unlike PyQt4, PyQt5 supports the creation of Python objects from QML.

QtGui Module

PyQt4’s QtGui module has been split into PyQt5’s QtGuiQtPrintSupport and QtWidgets modules.

QtOpenGL Module

Only the QGLContextQGLFormat and QGLWidget classes are supported by PyQt5.

QtWebKit Module

PyQt4’s QtWebKit module has been split into PyQt5’s QtWebKit and QtWebKitWidgets modules.

QtXml Module

PyQt4’s QtXml module is not supported. Use either the QXMLStreamReader and QXMLStreamWriter classes or Python’s standard XML modules.

pyqtconfig Module

PyQt4’s pyqtconfig module is not supported. The section The PyQt5 Extension API describes the support that PyQt5 provides to third-party packages (e.g. QScintilla) that want to build on top of PyQt5.

dbus.mainloop.qt Module

PyQt4’s dbus.mainloop.qt module is called dbus.mainloop.pyqt5 in PyQt5. This allows them to be installed side by side. Their functionality is identical.

QDataStream

The readUInt8()readInt8()writeUInt8() and writeInt8() methods all interpret the values being read and written as numeric values. In PyQt4 they are interpreted as single character strings.

QFileDialog

The getOpenFileNameAndFilter()getOpenFileNamesAndFilter() and getSaveFileNameAndFilter() methods of PyQt4’s QFileDialog have now been renamed getOpenFileName(),getOpenFileNames() and getSaveFileName() respectively in PyQt5. PyQt4’s implementations of getOpenFileName()getOpenFileNames() and getSaveFileName() are not supported in PyQt5.

QGraphicsItemAnimation

Support for the deprecated QGraphicsItemAnimation class has been removed. If porting an existing PyQt4 application then consider first updating it to use QPropertyAnimationinstead.

QMatrix

Support for the deprecated QMatrix class has been removed. If porting an existing PyQt4 application then consider first updating it to use QTransform instead.

QPyTextObject

PyQt4 implements the QPyTextObject as a workaround for the inability to define a Python class that is sub-classed from more than one Qt class. PyQt5 does support the ability to define a Python class that is sub-classed from more than one Qt class so long as all but one of the Qt classes are interfaces, i.e. they have been declared in C++ as such usingQ_DECLARE_INTERFACE. Therefore QPyTextObject is not implemented in PyQt5.

QSet

In PyQt4, QSet was implemented as a list in Python v2 and a set in Python v3. In PyQt5 QSet is always implemented as a set.

pyuic5

pyuic5 does not support the --pyqt3-wrapper flag of pyuic4.

pyrcc5

pyrcc5 does not support the -py2 and -py3 flags of pyrcc4. The output of pyrcc5 is compatible with all versions of Python starting with Python v2.6.

Cooperative Multi-inheritance

Unlike PyQt4, PyQt5 classes implement cooperative multi-inheritance. In other words PyQt5 classes always do the equivalent of the following Python v3 code in their __init__methods (where kwds is a dictionary of unused keyword arguments):

super().__init__(**kwds)

This means that those unused keyword arguments are passed to the __init__ methods of any mixin classes. Those mixin classes must cooperate, i.e. they must make a similar call if they have their own __init__ implementations.

When using multiple inheritance in PyQt4 it is common to call the __init__ methods of the super-classes explicitly, for example:

class MyQObject(QObject, MyMixin):
    def __init__(self, parent, mixin_arg):
        QObject.__init__(self, parent)
        MyMixin.__init__(self, mixin_arg)

        # Other initialisation...

In PyQt5 the above would cause MyMixin.__init__ to be called twice. Instead it should be implemented as follows:

class MyQObject(QObject, MyMixin):
    def __init__(self, **kwds):
        super().__init__(**kwds)

        # Other initialisation...

Note that if there is no other initialisation to do then the __init__ method isn’t actually needed.

The mixin class should be implemented as follows:

class MyMixin:
    def __init__(self, mixin_arg, **kwds):
        super().__init__(**kwds)

        # Other initialisation...

If a class only inherits from a single class then it can still call the super-class’s __init__ method explicitly (although it is recommended to use super()).

See Support for Cooperative Multi-inheritance.

Releasing the GIL

The GIL is only released when it is known to be needed. PyQt4 always released the GIL when calling Qt.

Object Destruction on Exit

When the Python interpreter exits PyQt4 (by default) calls the C++ destructor of all wrapped instances that it owns. This happens in a random order and can therefore cause the interpreter to crash. This behavior can be disabled by calling the sip.setdestroyonexit() function.

PyQt5 always calls sip.setdestroyonexit() automatically. However if the wrapped instances have not been created at the module level and have instead been created in a function then the problem will still exist. For example, do not do the following:

def main():
    app = QApplication(sys.argv)

    w = QWidget()
    w.show()

    app.exec()

if __name__ == '__main__':
    main()

Instead, do the following:

if __name__ == '__main__':
    app = QApplication(sys.argv)

    w = QWidget()
    w.show()

    app.exec()

ImportError:无法从“ PyQt4”导入名称“ QtCore”

ImportError:无法从“ PyQt4”导入名称“ QtCore”

如何解决ImportError:无法从“ PyQt4”导入名称“ QtCore”?

我在manjaro上遇到此错误ImportError: cannot import name ''QtCore'' from ''PyQt4'',因此我尝试安装PyQt4,但出现此错误

/Scaricati/PyQt4_gpl_x11-4.12.3 $ python configure-ng.py Querying qmake about your Qt installation... Determining the details of your Qt installation... This is the GPL version of PyQt 4.12.3 (licensed under the GNU General Public License) for Python 3.8.5 on linux. Error: This version of PyQt4 and the commercial version of Qt have incompatible licenses.

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

关于PyQt4.QtCore.QVariant对象而不是字符串?qt pyqt的介绍已经告一段落,感谢您的耐心阅读,如果想了解更多关于ApolloClient - 如何传递数组而不是字符串?、c – qVariantValue是“QT_DEPRECATED” – 替换是什么?、Differences Between PyQt4 and PyQt5、ImportError:无法从“ PyQt4”导入名称“ QtCore”的相关信息,请在本站寻找。

本文标签: