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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T21:52:16+00:00 2026-05-16T21:52:16+00:00

I’m using a QSortFilterProxyModel to filter results from a QAbstractListModel. However, I’d like to

  • 0

I’m using a QSortFilterProxyModel to filter results from a QAbstractListModel. However, I’d like to return a first entry which is not present in the original model, that is, it’s somehow artificial.

This is what I have so far:

class ActivedAccountModel(QSortFilterProxyModel):                                                                                                                                  
    def __init__(self, model, parent=None):
        super(ActiveAccountModel, self).__init__(parent)
        self.setSourceModel(model)
        self.setDynamicSortFilter(True)

    def data(self, index, role=Qt.DisplayRole):
        account_info = super(ActiveAccountModel, self).data(index, Qt.UserRole).toPyObject()
        if role == Qt.DisplayRole:
            return account_info.name
        elif role == Qt.UserRole:
            return account_info
        return None

    def filterAcceptsRow(self, source_row, source_parent):
        source_model = self.sourceModel()
        source_index = source_model.index(source_row, 0, source_parent)
        account_info = source_model.data(source_index, Qt.UserRole)
        return isinstance(account_info.account, Account) and account_info.account.enabled

This will return a list in the form of:

Account 1
Account 2
...

Id’ like to return an extra element at the begining of the returned list f elements:

Extra Element
Account 1
Account 2
...

I tried to reimplement rowCount in order to return the real rowCount()+1, but somehow I’d need to shift all the items in order to return this artificial element at index 0, and I’m a bit lost there.

Any clue? I couldn’t find any related code example so far… Thanks!

  • 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-05-16T21:52:17+00:00Added an answer on May 16, 2026 at 9:52 pm

    I’ve done this, only at work, so I can’t give you much code. I can give you the general idea of what to do.

    It works better if you subclass QAbstractProxyModel, which is designed for general manipulation, not sorting or filtering. You’ll want to override rowCount, and also need to override columnCount (although that should just return the information from the source model). You’ll need to override the data function and return your own data for the first row, or call the source model once more.

    You will want to override the mapFromSource and mapToSource functions, to allow switching between the proxy model indexes and source model indexes.

    To do a robust implementation, you’ll need to create some slots and connect to the source model’s signals for data changing, model reset, and rows/columns about to be inserted/removed. You should then emit your own signals, properly adapting them to account for your extra row.

    In our class, we made the text for the first row settable, so we could use the same proxy model in different situations. It’s worth investigating for yours, since it adds minimal effort.

    Edit

    Per commented request, a rough look at mapToSource and mapFromSource. This is approximately what you need to think about.

    // Remember that this maps from the proxy's index to the source's index, 
    // which is invalid for the extra row the proxy adds.
    mapToSource( proxy_index ):
        if proxy_index isn't valid:
            return invalid QModelIndex
        else if proxy_index is for the first row:
            return invalid QModelIndex
        else
            return source model index for (proxy_index.row - 1, proxy_index.column)
    
    mapFromSource( source_index ):
        if source_index isn't valid:
            return invalid QModelIndex
        else if source_index has a parent:
            // This would occur if you are adding an extra top-level 
            // row onto a tree model.
            // You would need to decide how to handle that condition
            return invalid QModelIndex
        else
            return proxy model index for (source_index.row + 1, source_index.column)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I would like to run a str_replace or preg_replace which looks for certain words
We're building an app, our first using Rails 3, and we're having to build
I have a text area in my form which accepts all possible characters from
I'm making a simple page using Google Maps API 3. My first. One marker
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
That's pretty much it. I'm using Nokogiri to scrape a web page what has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I would like to count the length of a string with PHP. The string

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.