Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8254107
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T01:05:29+00:00 2026-06-08T01:05:29+00:00

This is not so much a question as it is a request for an

  • 0

This is not so much a question as it is a request for an explanation. I’m following Mark Summerfield’s “Rapid GUI Programming with Python and Qt”, and I must’ve missed something because I cannot make sense of the following mechanism to link together a real “instance_item” which I am using and is full of various types of data, and a “widget_item” which represents it in a QTreeWidget model for convenience.

Setting:

widget_item.setData(0, Qt.UserRole, QVariant(long(id(instance_item))))

Getting

widget_item.data(0, Qt.UserRole).toLongLong()[0]

Stuff like toLongLong() doesn’t seem “Pythonic” at all, and why are we invoking Qt.UserRole and QVariant? are the “setData” and “data” functions part of the Qt framework or is it a more general Python command?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T01:05:30+00:00Added an answer on June 8, 2026 at 1:05 am

    There are at least 2 better solutions. In order of increasing pythonicity:

    1) You don’t need quite so much data type packing

    widget_item.setData(0, Qt.UserRole, QVariant(instance_item))
    widget_item.data(0, Qt.UserRole).toPyObject()
    

    2) There is an alternate API to PyQt4 where QVariant is done away with, and the conversion to-from QVariant happens transparently. To enable it, you need to add the following lines before any PyQt4 import statements:

    import sip
    sip.setapi('QVariant', 2)
    

    Then, your code looks like this:

    widget_item.setData(0, Qt.UserRole, instance_item)
    widget_item.data(0, Qt.UserRole)  # original python object
    

    Note that there is also an option sip.setapi('QString', 2) where QString is done away with, and you can use unicode instead.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For starters, this question is not so much about programming in the NetBeans IDE
I thought I would post this here not so much as a question but
Not much to say about this question...
This is not programming related as much as it is software engineering related. I
This not a programming question but Most of the programmers using Eclipse should have
This is driving me crazy and has resulted in lost work (not much, at
Found this Multimap containing pairs? , but it is not much help How would
This feels like it should be pretty simple, but not much seems to be
Apologies if this doesn't make sense, i'm not much of an experienced programmer. Consider
I've been working at this for two days and have not had much luck.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.