PDA

View Full Version : Displaying Multiple Columns in QListView



millard
5th December 2007, 10:44
Hi,

How do I display mulitple columns in a QListView that is connected to a QSqlTableModel.

For example I have a the following table:

player
-----------------------------
id | firstname | surname
-----------------------------

How do I list the "surname, firstname" in the QListView using models and views?

QListView has "setModelColumn" function but I can't use both columns.

Thanks Tim

jpn
5th December 2007, 10:47
QListView represents a flow of items. It cannot have multiple columns. Use QTreeView or QTableView for multiple columns.

millard
5th December 2007, 10:55
How do I concat the strings from both columns to make one item for the QListView?
For example concat "surname" and "firstname" to make one item.

Thanks Tim

wysota
5th December 2007, 11:15
You have to do it manually, for example using SQL or using a proxy model (see QAbstractProxyModel) that will concatenate two columns into one single column.

jpn
5th December 2007, 11:15
You might want to use QSqlQueryModel then and do it within query.

millard
5th December 2007, 11:34
Thanks for you help.

Any ideas how I would do it using QAbstractProxyModel.

Would I sub-class QAbstractProxyModel and overwrite the data function?

Thanks Tim

wysota
5th December 2007, 11:50
Yes, you would :) And also columnCount() and all the pure abstract methods (but they are trivial to reimplement in your case).

millard
5th December 2007, 11:58
Thanks for you help.:)

jpn
5th December 2007, 12:03
Does it need to be editable?


QSqlQueryModel* model = new QSqlQueryModel(this);
model->setQuery("SELECT firstname || surname AS fullname FROM player"); // concat format depends on dbms
listView->setModel(model);