Getting data from selected item in QListView

后端 未结 3 547
北恋
北恋 2021-01-03 12:45

I populate a QListView with QSqlTableModel.

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

  projectView = Q         


        
相关标签:
3条回答
  • 2021-01-03 13:31

    You can use the selectedIndexes() method of the model to get the indexes of the selected items using something like

    idx = self.projectView.selectionModel().selectedRows()
    

    then looping on idx, you can get the values you need with

    for rec in idx:
        entry = rec.row()
        val = self.projectModel.record(entry).field(x).value()
    

    with x beeing the number of the column you want to retrive

    val is now a QVariant and you can further elaborate it.

    0 讨论(0)
  • 2021-01-03 13:41

    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 讨论(0)
  • 2021-01-03 13:44

    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?

    Please check the following example. You can add a function to the "clicked" slot of QListView. Then get the which row is selected.

    from PyQt4.QtSql import QSqlDatabase,QSqlTableModel
    from PyQt4.QtGui import QListView,QApplication
    import sys
    
    def listclicked(index):
        row = index.row()
        print "id = %s" % projectModel.record(row).field(0).value().toString()
        print "country = %s" % projectModel.record(row).field(2).value().toString()
    
    app = QApplication(sys.argv)
    
    db = QSqlDatabase.addDatabase("QSQLITE")
    db.setDatabaseName("sqlitedb.rdb")
    db.open()
    
    projectModel = QSqlTableModel()
    projectModel.setTable("info")
    projectModel.select()
    
    projectView = QListView()
    projectView.setModel(projectModel)
    projectView.setModelColumn(1)
    projectView.clicked.connect(listclicked)
    projectView.show()
    
    app.exec_()
    
    0 讨论(0)
提交回复
热议问题