How do I get my python object back from a QVariant in PyQt4?

2024/9/20 0:05:27

I am creating a subclass of QAbstractItemModel to be displayed in an QTreeView.

My index() and parent() function creates the QModelIndex using the QAbstractItemModel inherited function createIndex and providing it the row, column, and data needed. Here, for testing purposes, data is a Python string.

class TestModel(QAbstractItemModel):def __init__(self):QAbstractItemModel.__init__(self)def index(self, row, column, parent):if parent.isValid():return self.createIndex(row, column, "bar")return self.createIndex(row, column, "foo")def parent(self, index):if index.isValid():if index.data().data() == "bar":                          <--- NEVER TRUEreturn self.createIndex(0, 0, "foo")return QModelIndex()def rowCount(self, index):if index.isValid():if index.data().data() == "bar":                          <--- NEVER TRUEreturn 0return 1def columnCount(self, index):return 1def data(self, index, role):if index.isValid():return index.data().data()                                <--- CANNOT DO ANYTHING WITH ITreturn "<None>"

Within the index(), parent(), and data() functions I need to get my data back. It comes as a QVariant. How do I get my Python object back from the QVariant?

Answer

Have you tried this?

my_python_object = my_qvariant.toPyObject()

http://pyqt.sourceforge.net/Docs/PyQt4/qvariant.html#toPyObject (just for completeness, but there isn't much to see there...)

https://en.xdnf.cn/q/72540.html

Related Q&A

Django serializers vs rest_framework serializers

What is the difference between Django serializers vs rest_framework serializers? I making a webapp, where I want the API to be part of the primary app created by the project. Not creating a separate A…

Pandas replace non-zero values

I know I can replace all nan values with df.fillna(0) and replace a single value with df.replace(-,1), but how can I replace all non-zero values with a single value?

Pandas percentage change using group by

Suppose I have the following DataFrame: df = pd.DataFrame({city: [a, a, a, b, b, c, d, d, d], year: [2013, 2014, 2016, 2015, 2016, 2013, 2016, 2017, 2018],value: [10, 12, 16, 20, 21, 11, 15, 13, 16]})A…

Django cannot find my static files

I am relatively new to web dev. and I am trying to build my first web application. I have my static folder in project_root/static but for some reason, I keep getting 404s when I run the server:Not Foun…

How can I find intersection of two large file efficiently using python?

I have two large files. Their contents looks like this:134430513125296589151963957125296589The file contains an unsorted list of ids. Some ids may appear more than one time in a single file. Now I want…

Failed to load the native TensorFlow runtime - TensorFlow 2.1

I have a desktop computer and a notebook, when I tried to install tensorflow on a notebook just by using pip install tensorflow it worked ok, then I tried the same on my desktop computer and when I tri…

(Python) Issues with directories that have special characters

OS: Windows server 03 Python ver: 2.7For the code below, its runs fine when I substitute "[email protected]" with "fuchida". If I use the email format for directory name I get the f…

LibCST: Converting arbitrary nodes to code

Is it possible to dump an arbitrary LibCST node into Python code? My use case is that I want to extract the code for functions that match a specific naming scheme. I can extract the FunctionDef nodes …

calculating the number of k-combinations with and without SciPy

Im puzzled by the fact that the function comb of SciPy appears to be slower than a naive Python implementation. This is the measured time for two equivalent programs solving the Problem 53 of Project E…

How to subclass requests in python through inheritance

I would like to specialize / subclass the requests package to add some method with custom functionality.I tried to do this:# concrete_requests.py import requestsclass concreteRequests(requests):def __i…