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 7860575
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T22:20:56+00:00 2026-06-02T22:20:56+00:00

I have some code (see below) that works as expected under Python 2, but

  • 0

I have some code (see below) that works as expected under Python 2, but when I execute it under Python 3 it raises the exception:

Traceback (most recent call last):
File "./test3.py", line 23, in <module>
programsCombo­Box.addItem("Jan Novak",QtCore­.QVariant("661107/39­39"))
TypeError: PyQt4.QtCore.QVa­riant represents a mapped type and cannot be instantiated

Why is this and is there any workaround?

#!/usr/bin/env python
# -*- coding: utf-8 -*-
from PyQt4 import QtCore, QtGui
import sys

def activated(i):
    data=programsComboBox.itemData(i)
    label.setText("Rodne cislo: "+data.toString())

app = QtGui.QApplication(sys.argv)
mainWindow = QtGui.QMainWindow()
mainWindow.setWindowTitle("QVariant")
mainWidget=QtGui.QWidget(mainWindow)
mainWindow.setCentralWidget(mainWidget)
layout=QtGui.QVBoxLayout(mainWidget)

label=QtGui.QLabel("Rodne cislo: ",mainWidget)

programsComboBox=QtGui.QComboBox(mainWidget)
programsComboBox.addItem("Jan Novak",QtCore.QVariant("661107/3939"))
programsComboBox.addItem("Jakub Dvorak",QtCore.QVariant("750802/1278"))

layout.addWidget(programsComboBox)
layout.addWidget(label)

app.connect(programsComboBox,QtCore.SIGNAL("activated (int)"),activated)
mainWindow.show()
sys.exit(app.exec_())

Update: For this simple code it’s enough to add these before the first PyQt4 import

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

But in the real code I can’t do that (somewhere in a different place QVariant requires api2).

  • 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-02T22:20:58+00:00Added an answer on June 2, 2026 at 10:20 pm

    Is there any reason why you have to use a QVariant?

    If you add your item as a string:

    programsComboBox.addItem("Jan Novak", "661107/3939")
    

    then the code should work in Python 2 and 3.

    I don’t use Python 3 myself so can’t test this, but the modification runs fine on Python 2.7.

    EDIT:

    I was curious, so I tested this in a VM using Python 3.2. It seems that .itemData() will return a string object rather than a QVariant in Python 3.

    In addition to using a string when adding an item as above you could modify your activated function to:

    def activated(i):
        data=programsComboBox.itemData(i)
        if not isinstance(data, str):
            data = data.toString()
        label.setText("Rodne cislo: " +data)
    

    so that it would work with Python 2 and 3. If the return of .itemData() isn’t a string then the function will convert to a string using .toString().

    I’m not sure if this is the correct way, but it works for me. I still need to look into the whole 2 to 3 porting thing properly.

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

Sidebar

Related Questions

I have some really funky code. As you can see from the code below
I have some code that is using SyncEnumerator. As you can see here ,
I have an example of some code that I see often in websites that
In my Android program, I have some code that downloads a file. This works
Basically I have some code to check a specific directory to see if an
I have the following as part of some code - you can see it
I have some code that looks like this (I left out the parts that
i have some code that tries to list images based on their height. the
I have some code that creates a new event source: EventLog.CreateEventSource(Source, LogName); I know
I have some code that shows a tables row when something is clicked. So,

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.