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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T13:25:19+00:00 2026-06-13T13:25:19+00:00

I’m started to learning Qt4 Model/View Programming and I have beginner question. I have

  • 0

I’m started to learning Qt4 Model/View Programming and I have beginner question.

I have simple application which show sqlite table in QTableView:

class Model(QtSql.QSqlTableModel):
    def __init__(self, parent=None):
        super(Model, self).__init__(parent)
        self.setEditStrategy(QtSql.QSqlTableModel.OnFieldChange)

        self.setTable("test")
        self.select()

class App(QtGui.QMainWindow):
    def __init__(self, model):
        QtGui.QMainWindow.__init__(self)

        self.ui = Ui_MainWindow()
        self.ui.setupUi(self)

        self.ui.tableView.setModel(model)

if __name__ == "__main__":
    myDb = QtSql.QSqlDatabase.addDatabase("QSQLITE")
    myDb.setDatabaseName("test.db")

    if not myDb.open():
        print 'FIXME'

    model = Model()

    app = QtGui.QApplication(sys.argv)
    window = App(model)
    window.show()
    sys.exit(app.exec_())

Here how database looks like:

sqlite> create table test (a INTEGER, b INTEGER, c STRING);
sqlite> insert into test VALUES(1, 2, "xxx");
sqlite> insert into test VALUES(6, 7, "yyy");

So I’m getting something like:

+---+---+-----+
| a | b |  c  |
+---+---+-----+
| 1 | 2 | xxx |
+---+---+-----+
| 6 | 7 | yyy |
+---+---+-----+

Is it possible to modify Model to have in QTableView something like virtual column? For example something like:

+---+---+-----+-----+
| a | b | sum |  c  |
+---+---+-----+-----+
| 1 | 2 |  3  | xxx |
+---+---+-----+-----+
| 6 | 7 | 13  | yyy |
+---+---+-----+-----+

Or maybe I should do it in some other way?

  • 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-13T13:25:20+00:00Added an answer on June 13, 2026 at 1:25 pm

    Yes, you can do that. Although @BrtH’s answer is relevant, models are tricky and it’s easy to get lost. So I thought a more case in point example would be better.

    Personally, I’d use a proxy model derived from QAbstractProxyModel. But, in your case reimplementing QSqlTableModel is also feasible. Below is an implementation for your goal. Note that, it’s essential for you to know basics of Model/View methodology so that you understand what each method does.

    class Model(QtSql.QSqlTableModel):
        def __init__(self, parent=None):
            super(Model, self).__init__(parent)
            self.setEditStrategy(QtSql.QSqlTableModel.OnFieldChange)
    
            self.setTable("test")
            self.select()
    
    
        def columnCount(self, parent=QtCore.QModelIndex()):
            # this is probably obvious
            # since we are adding a virtual column, we need one more column
            return super(Model, self).columnCount()+1
    
    
        def data(self, index, role=QtCore.Qt.DisplayRole):
            if role == QtCore.Qt.DisplayRole and index.column()==2:
                # 2nd column is our virtual column.
                # if we are there, we need to calculate and return the value
                # we take the first two columns, get the data, turn it to integer and sum them
                # [0] at the end is necessary because pyqt returns value and a bool
                # http://www.riverbankcomputing.co.uk/static/Docs/PyQt4/html/qvariant.html#toInt
                return sum(self.data(self.index(index.row(), i)).toInt()[0] for i in range(2))
            if index.column() > 2:
                # if we are past 2nd column, we need to shift it to left by one
                # to get the real value
                index = self.index(index.row(), index.column()-1)
            # get the value from base implementation
            return super(Model, self).data(index, role)
    
    
        def headerData(self, section, orientation, role=QtCore.Qt.DisplayRole):
            # this is similar to `data`
            if section==2 and orientation==QtCore.Qt.Horizontal and role==QtCore.Qt.DisplayRole:
                return 'Sum'
            if section > 2 and orientation==QtCore.Qt.Horizontal:
                section -= 1
            return super(Model, self).headerData(section, orientation, role)
    
    
        def flags(self, index):
            # since 2nd column is virtual, it doesn't make sense for it to be Editable
            # other columns can be Editable (default for QSqlTableModel)
            if index.column()==2:
                return QtCore.Qt.ItemIsSelectable | QtCore.Qt.ItemIsEnabled
            return QtCore.Qt.ItemIsSelectable | QtCore.Qt.ItemIsEnabled | QtCore.Qt.ItemIsEditable
    
    
        def setData(self, index, data, role):
            # similar to data.
            # we need to be careful when setting data (after edit)
            # if column is after 2, it is actually the column before that
            if index.column() > 2:
                index = self.index(index.row(), index.column()-1)
            return super(Model, self).setData(index, data, role)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have just tried to save a simple *.rtf file with some websites and
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have an autohotkey script which looks up a word in a bilingual dictionary
This could be a duplicate question, but I have no idea what search terms
I have an array which has BIG numbers and small numbers in it. I
I have a text area in my form which accepts all possible characters from
I don't have much knowledge about the IPv6 protocol, so sorry if the question
I have an MVC Razor view @{ ViewBag.Title = Index; var c = (char)146;
I have a view passing on information from a database: def serve_article(request, id): served_article
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example

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.