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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T05:43:38+00:00 2026-06-10T05:43:38+00:00

I’m trying to put together a simple table that shows my model’s data. I

  • 0

I’m trying to put together a simple table that shows my model’s data. I need row selection behaviour so I set:

self.setSelectionBehavior(QAbstractItemView.SelectRows)

All is good until I implement selectionChanged() which causes the redraw to get a bit confused every time a row is selected (cell’s dont’ seem to update their selection state). Here is some test code that causes the problem for me:

import sys
from PySide.QtGui import *
from PySide.QtCore import *

class Item( QStandardItem ):
    def __init__( self, parent=None ):
        super( Item, self).__init__( parent )
        self.pixmap = QPixmap("colour.png")

    #def data(self, role=Qt.UserRole + 1):
        #'''with this method in place the cells get a checkbox and are not selectable'''
        #return 'test'


class Model (QStandardItemModel):
    def __init__( self, parent=None ):
        super( Model, self).__init__( parent )
        self.setHorizontalHeaderLabels(['a', 'b', 'c'])
        self.init_data()

    def init_data(self):
        for row in range(0, 15):
            for col in range(0, 10):
                col_item = Item( '%s, %s' % (row, col) )
                self.setItem(row, col, col_item)

class TableView( QTableView ):
    def __init__( self, parent=None ):
        super( TableView, self).__init__( parent )
        model = Model()
        self.setModel(model)
        self.setSelectionBehavior(QAbstractItemView.SelectRows)
        self.setSelectionMode(QAbstractItemView.ContiguousSelection)
        self.setMouseTracking(True)

    def selectionChanged(self, selected, deselected):
        print selected

if __name__ == '__main__':

    app = QApplication([])
    table = TableView()
    table.show()
    sys.exit(app.exec_())

I’m also a bit confused about why the cell’s all get a checkbox and become un-selectable if the data() method is implemented in the QStandardItem. Can somebody please help?

Cheers,
frank

  • 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-10T05:43:40+00:00Added an answer on June 10, 2026 at 5:43 am

    You are overriding the QTableViews selectionChanged. That is (probably) used internally by the view and you prevent that. I am not sure why you would want to do that. You should use the selectionChanged signal of the selectionModel() for the view if you want to do custom things when the selection changes.

    But if you insist on overriding selectionChanged on TableView at least call the parents function too, so that view could do its work:

    class TableView( QTableView ):
        def __init__( self, parent=None ):
            super( TableView, self).__init__( parent )
            model = Model()
            self.setModel(model)
            self.setSelectionBehavior(QAbstractItemView.SelectRows)
            self.setSelectionMode(QAbstractItemView.ContiguousSelection)
            self.setMouseTracking(True)
    
        def selectionChanged(self, selected, deselected):
            print selected
            super(TableView, self).selectionChanged(selected, deselected)
    

    But, you don’t need to and you really shouldn’t subclass a QTableView just to set a few properties. You can do that for an instance. Actually, all your sub-classes are unnecessary. You could write that code like this:

    import sys
    from PySide.QtGui import *
    from PySide.QtCore import *
    
    if __name__ == '__main__':
        app = QApplication([])
    
        # create model
        model = QStandardItemModel()
        model.setHorizontalHeaderLabels(['a', 'b', 'c'])
    
        # fill data
        for row in range(15):
            model.appendRow([QStandardItem('%d, %d' % (row, col)) for col in range(10)])
    
        # create table view
        table = QTableView()
    
        # set parameters
        table.setModel(model)
        table.setSelectionBehavior(QAbstractItemView.SelectRows)
        table.setSelectionMode(QAbstractItemView.ContiguousSelection)
        table.setMouseTracking(True)
    
        # show
        table.show()
    
        sys.exit(app.exec_())
    

    As for overriding the data method for the model: data method is responsible for returning many different values depending on the role that view asks. You are always returning "test" for all of them. That is, to say the least, bad. If you want to create your own models, you should at least read about how models/views work in Qt. Official docs have a nice section about it. You can also find some decent video tutorials in the web.

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

Sidebar

Related Questions

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 need a function that will clean a strings' special characters. I do NOT
I'm trying to create an if statement in PHP that prevents a single post
I am trying to loop through a bunch of documents I have to put
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and

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.