You are not logged in.

Dear visitor, welcome to QtForum.org. If this is your first visit here, please read the Help. It explains in detail how this page works. To use all features of this page, you should consider registering. Please use the registration form, to register here or read more information about the registration process. If you are already registered, please login here.

1

Wednesday, April 14th 2010, 9:12am

QListView with two items in a row not getting :(

I am using QListView to get a list with two items in each row,
like
Martin Luther
Dennis Ritchie


where each text is one column and should be able to sort using each of the string.
With the following code, i m getting only the second name. i want both the names to be visible.

ListView :: ListView (QMainWindow *mainWindow)
{

contactList = new QListView (mainWindow);
QSortFilterProxyModel *proxyModel = new QSortFilterProxyModel(this);
model = new QStandardItemModel (this);

proxyModel->setDynamicSortFilter(true);
proxyModel->setSortCaseSensitivity(Qt::CaseSensitive);
proxyModel->setSourceModel(model);
contactList->setModel(proxyModel);

int row = model->rowCount(); /* +1 for a new row */
int col = 0;
model->setItem( row, col, new QStandardItem("Martin"));
model->setItem( row, col++, new QStandardItem("Luther"));

row = model->rowCount(); /* +1 for a new row */
col = 0;
model->setItem( row, col, new QStandardItem("Dennis"));
model->setItem( row, col++, new QStandardItem("Ritchie"));

proxyModel->setSortRole(0);
proxyModel->sort (0);
}


Can anybody help me get this working.??

Junior

Professional

  • "Junior" is male

Posts: 1,613

Location: San Antonio, TX USA

Occupation: Senior Secure Systems Engineer

  • Send private message

2

Wednesday, April 14th 2010, 1:09pm

Need to gather the QStandardItems together before insterting them; (e.g)

Source code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
ListView :: ListView (QMainWindow *mainWindow)
{
  contactList = new QListView (mainWindow);
  QSortFilterProxyModel *proxyModel = new QSortFilterProxyModel(this);  //function scope?
  model = new QStandardItemModel (this);

  proxyModel->setDynamicSortFilter(true);
  proxyModel->setSortCaseSensitivity(Qt::CaseSensitive);
  proxyModel->setSourceModel(model);
  contactList->setModel(proxyModel);

/** removed
int row = model->rowCount(); /* +1 for a new row */
int col = 0;
model->setItem( row, col, new QStandardItem("Martin"));
model->setItem( row, col++, new QStandardItem("Luther"));

row = model->rowCount(); /* +1 for a new row */
col = 0;
model->setItem( row, col, new QStandardItem("Dennis"));
model->setItem( row, col++, new QStandardItem("Ritchie"));
*/

  QList<QStandardItem *> itemList;
  
  itemList << new QStandardItem("Dennis"); // first name 
  itemList << new QStandardItem( "Ritchie"); // last name
  model->appendRow( itemList );  // expands items across columns when inserting row

  itemList.clear(); // empty list

  itemList << new QStandardItem("Martin"); // first name
  itemList << new QStandardItem( "Luther"); // last name
  model->appendRow( itemList );

  proxyModel->setSortRole(0);
  proxyModel->sort (0);
}