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

  • Home
  • SEARCH
  • 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 8916047
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T05:08:04+00:00 2026-06-15T05:08:04+00:00

I’ve read some examples on how to define a custom model for a QComboBox

  • 0

I’ve read some examples on how to define a custom model for a QComboBox widget.

Here’s how I defined my model:

class LevelListModel(QAbstractListModel):
    def __init__(self, parent=None, *args):
        """ datain: a list where each item is a row
        """
        QAbstractListModel.__init__(self, parent, *args)
        self.levelList = []
    def rowCount(self, parent=QModelIndex()):
        return len(self.levelList)

    def data(self, index, role):
        if index.isValid() and role == Qt.DisplayRole:
            return QVariant(index.row())
        else:
            return QVariant()
    def addLevel(self,level):
        self.beginResetModel()
        self.levelList.append(level)
        self.endResetModel()

I set the model to my QComboBox:

self.levelListModel = LevelListModel()
self.ui.levelComboBox.setModel(self.levelListModel)

I add a model to my list this way:

newLevel = Level (self.levelListModel.rowCount() + 1)
self.levelListModel.addLevel(newLevel)

The item is added correctly and I can see it inside the combobox, but I would like to change the currentIndex to be the new item’s index.

I guess QAbstractListModel could raise some kind of events that QComboBox can listen to, but I haven’t still found how to do that.

My questions are:

  1. How can I notify the QComboBox that model data changed, and listen to that event to modify currentIndex accordingly?
  2. I used [begin|end]ResetModel because my entry should be an ordered sequence of integer. So I need to rebuild the data list completely once an item in the middle of the list have been removed. I don’t know if this is the right way to go. Any better solution?
  • 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-15T05:08:06+00:00Added an answer on June 15, 2026 at 5:08 am

    1

    How can I notify the QComboBox that model data changed, and listen to
    that event to modify currentIndex accordingly?

    No need to listen an event from the way you do things. You know when the model data is changed, because you add things yourself. Just change the currentIndex after adding a data.

    I’d probably modify the addLevel method to return the QModelIndex of the added item and then use it to set the currentIndex of the QComboBox:

    class LevelListModel(QAbstractListModel):
        # [skipped]
        def addLevel(self,level):
            self.beginInsertRows(QModelIndex(), len(self.levelList), len(self.levelList))
            self.levelList.append(level)
            self.endInsertRows()
            return self.index(len(self.levelList)-1)
    

    and

    newLevel = Level (self.levelListModel.rowCount() + 1)
    newIndex = self.levelListModel.addLevel(newLevel)
    self.ui.levelComboBox.setCurrentIndex(newIndex)
    

    2

    I used [begin|end]ResetModel because my entry should be an ordered
    sequence of integer. So I need to rebuild the data list completely
    once an item in the middle of the list have been removed. I don’t know
    if this is the right way to go. Any better solution?

    That depends. [begin|end]ResetModel is for really drastic changes. I don’t see how keeping an ordered list of integers would lead to such changes for single item addition/removal. From what you describe, you should be using [begin|end]InsertRows and [begin|end]RemoveRows.

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I am doing a simple coin flipping experiment for class that involves flipping a
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I have a view passing on information from a database: def serve_article(request, id): served_article
Let's say I'm outputting a post title and in our database, it's Hello Y’all

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.