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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T09:18:41+00:00 2026-06-05T09:18:41+00:00

I’m trying to figure out how to work with the models and views in

  • 0

I’m trying to figure out how to work with the models and views in PyQt4.9.1 and I’ve run into a bit of a problem.

Here’s the code that matters:

class TestModel(QtGui.QStandardItemModel):
    def __init__(self,parent=None):
        QtGui.QStandardItemModel.__init__(self,parent)
        self.initData()
        self.headings = ['name','value','']

    def initData(self):
        self.rows = [['name {0}'.format(i), i] for i in range(20)]

    def data(self, index, value, role):
        print ("foo")
        if not index.isValid():
            return 
        if (role == QtCore.Qt.DisplayRole):
            row = index.row()
            col = index.column()
            if  (col == 3):
                return "BUTTON GOES HERE"
        return self.rows[row][col]

    def headerData(self,section,orientation,role):
        if (role == QtCore.Qt.DisplayRole):
            if (orientation == QtCore.Qt.Horizontal):
                 return self.headings[section]

    def rowCount(self,parent):
        return len(self.rows)

    def columnCount(self,parent):
        return 3

class MainWindow(QtGui.QWidget):
    def __init__(self):
        super(MainWindow, self).__init__()
        self.m = TestModel()
        self.initUi()

    def initUi(self):   
        layout = QtGui.QVBoxLayout()
        widget = QtGui.QTableView()
        widget.setModel(self.m)
        layout.addWidget(widget)
        self.setLayout(layout)
        self.show()

Here’s what happens when I launch my application’s MainWindow: There are no error messages, the table is drawn with the right number of rows and columns and the correct headings, but the table is empty. You might notice that the model’s draw method starts off with a print statement. That statement is never reached. Is there something I’m missing? I cant find any tutorials at all for PyQt4.9.1.

  • 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-05T09:18:42+00:00Added an answer on June 5, 2026 at 9:18 am

    data() doesn’t have any value parameter, but removing it doesn’t solve the problem.

    The virtual method index(row, column, parent), which is called whenever a QModelIndex needs to be created, always returns an invalid index for QStandardItemModel, unless a QStandardItem was explicitly created for the requested index. The view probably doesn’t try to display cells when the index is invalid, so data() is never called.

    If you kept inheriting from QStandardItemModel, you would need to reimplement index() to create valid indexes, but since you are using your own structure to store the data instead of using QStandardItem, you could simply inherit from QtCore.QAbstractTableModel:

    class TestModel(QtCore.QAbstractTableModel):
        def __init__(self,parent=None):
            super(TestModel, self).__init__(parent)
            self.initData()
            self.headings = ['name','value','']
    
        def initData(self):
            self.rows = [['name {0}'.format(i), i] for i in range(20)]
    
        def data(self, index, role):
            if index.parent().isValid():
                return None             
            if (role == QtCore.Qt.DisplayRole):
                row = index.row()
                col = index.column()
                # 3rd column index is 2 not 3
                if  (col == 2):
                    return "BUTTON GOES HERE"
                # that return should also be "inside" the role DisplayRole
                return self.rows[row][col]
            return None
    
        def headerData(self,section,orientation,role):
            if (role == QtCore.Qt.DisplayRole):
                if (orientation == QtCore.Qt.Horizontal):
                    return self.headings[section]
    

    Also, you should only return a non-zero row/column count for top-level items (the one without parent), if you are not representing a tree model :

        def rowCount(self,parent):
            if not parent.isValid():
                return len(self.rows)
            return 0
    
        def columnCount(self,parent):
            if not parent.isValid():
                return 3
            return 0
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I know there's a lot of other questions out there that deal with this
I am currently running into a problem where an element is coming back from
I'm trying to create an if statement in PHP that prevents a single post
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
I am trying to understand how to use SyndicationItem to display feed which is

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.