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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T06:48:20+00:00 2026-05-28T06:48:20+00:00

I populate a QListView with QSqlTableModel. projectModel = QSqlTableModel() projectModel.setTable(project) projectModel.select() projectView = QListView()

  • 0

I populate a QListView with QSqlTableModel.

  projectModel = QSqlTableModel()
  projectModel.setTable("project")
  projectModel.select()

  projectView = QListView()
  projectView.setModel(projectModel)
  projectView.setModelColumn(1)
  • edit my wording a bit. –

Say my QSqlTableModel has 3 columns. (0) for id, (1) for name, (2) for color, for example.

In my QListView I’m displaying column 1, so only the names. Then I have 1 item selected. How can I retrieve that item’s id and color?

Sorry I’m a very beginner at this. Too beginner I can’t figure things out reading documentations 🙁

  • 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-28T06:48:21+00:00Added an answer on May 28, 2026 at 6:48 am

    Though i didn’t fully understand what you are trying to do, I’m showing you the answer of your two question.

    For example we have an sqlite3 database “sqlitedb.rdb”
    And a table name “info”,

    CREATE TABLE info (
    id integer  PRIMARY KEY AUTOINCREMENT DEFAULT NULL,
    name TEXT(20) DEFAULT NULL,
    country TEXT(20) DEFAULT NULL,
    age integer)
    

    and have some data,

    INSERT INTO info (name,country,age) VALUES ('Leroy Hale','San Marino','46');
    INSERT INTO info (name,country,age) VALUES ('William Coleman','Namibia','50');
    INSERT INTO info (name,country,age) VALUES ('Phelan Waller','Belgium','43');
    INSERT INTO info (name,country,age) VALUES ('Kato Martin','Virgin Islands, British','21');
    INSERT INTO info (name,country,age) VALUES ('Jameson Mccoy','United Arab Emirates','45');
    INSERT INTO info (name,country,age) VALUES ('Fulton Reeves','Belarus','34');
    INSERT INTO info (name,country,age) VALUES ('Calvin Love','Morocco','28');
    INSERT INTO info (name,country,age) VALUES ('Peter Solis','Bhutan','31');
    INSERT INTO info (name,country,age) VALUES ('Connor Stephenson','Dominican Republic','26');
    INSERT INTO info (name,country,age) VALUES ('Aristotle Smith','Chad','45');
    

    Now,

    I only want to display the id and name (column 0 and 1) how do I do that?

    To do this you can,

    from PyQt4.QtSql import QSqlQueryModel,QSqlDatabase,QSqlQuery
    from PyQt4.QtGui import QTableView,QApplication
    import sys
    
    app = QApplication(sys.argv)
    
    db = QSqlDatabase.addDatabase("QSQLITE")
    db.setDatabaseName("sqlitedb.rdb")
    db.open()
    
    projectModel = QSqlQueryModel()
    projectModel.setQuery("select name,age from info",db)
    
    projectView = QTableView()
    projectView.setModel(projectModel)
    
    projectView.show()
    app.exec_()
    

    You can set which field/column you want to show using the query.

    and,

    how can I retrieve the information from different columns of the item I have selected?

    You can navigate through the result of the query and have the data, example,

    from PyQt4.QtSql import QSqlDatabase,QSqlQuery
    from PyQt4.QtCore import QCoreApplication
    import sys
    
    app = QCoreApplication(sys.argv)
    
    db = QSqlDatabase.addDatabase("QSQLITE")
    db.setDatabaseName("sqlitedb.rdb")
    db.open()
    
    query = QSqlQuery()
    query.exec_("select * from info")
    while (query.next()):
        id = query.value(0).toInt()
        name = query.value(1).toString()
        country = query.value(2).toString()
        age = query.value(3).toInt()
    
        print "name = %s \ncountry = %s" % (name,country)
    
    app.exec_()
    

    you can loop through the query and get access to the data.

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

Sidebar

Related Questions

I populate a DataGrid from a CollectionViewSource. Each row has a delete button. On
On a treeview after the select event, I populate a listview with images. I
I am using LINQ-to-Entities and need to populate a ListView using a select statement
I am trying to populate a ListView in a separate class with data taken
Is it possible to populate a listview with a dataset? I have a function
I have an xml data file and I want to populate a listview in
I populate the GridView.DataSource from a EntityFramework Model: gwTimeLog.DataSource = _entities.TimeLogs; When a new
I'd like to populate an arraylist by specifying a list of values just like
I am trying to populate a string with a double value using a sprintf
I need to populate ListView with List of objects returned from my Dao object.

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.