PDA

View Full Version : I want to create a form with QListView



xiongxiongchuan
2nd June 2010, 05:50
i want to create a listview as the attachement,but i can't find any sample code ,could any one can help me:p

4717

aamer4yu
2nd June 2010, 06:15
Have a look at QListView::setViewMode
Just play in designer a little.

xiongxiongchuan
2nd June 2010, 12:48
i can't get how to use it ,can you give me some sample code from the beginning to end ,thank you

numbat
2nd June 2010, 14:01
#include <QtGui>
int main(int argc, char * argv[])
{
QApplication a(argc, argv);

QStandardItemModel * model = new QStandardItemModel(2, 1);
QListView * lv = new QListView(0);

model->setData(model->index(0, 0), lv->style()->standardIcon(QStyle::SP_BrowserReload), Qt::DecorationRole);
model->setData(model->index(0, 0), "Hello World!", Qt::DisplayRole);

model->setData(model->index(1, 0), lv->style()->standardIcon(QStyle::SP_FileIcon), Qt::DecorationRole);
model->setData(model->index(1, 0), "Goodbye World!", Qt::DisplayRole);

lv->setViewMode(QListView::IconMode);
lv->setModel(model);
lv->show();

return a.exec();
}

xiongxiongchuan
3rd June 2010, 14:50
Thanks ,you are so kind ,but when i do this as you taught me ,and i found the lable is editable ,how to set the label is not allowed to be edited;:o

tbscope
3rd June 2010, 14:55
Ohh come one, read the documentation
http://doc.qt.nokia.com/4.6/index.html

xiongxiongchuan
5th June 2010, 03:24
i have looked up for this problem in the QT Document ,but i can't find any solutions ,plz help me:cool:

tbscope
5th June 2010, 08:13
What label?

numbat
5th June 2010, 09:25
You have to set the item flags, as needed:


model->item(0,0)->setFlags(Qt::ItemIsEnabled | Qt::ItemIsSelectable);

xiongxiongchuan
5th June 2010, 12:43
i set the lable model as this below ,however ,the lable still can be edit,i can't find any properties to stop this!

when i double clicked the item ,the lable can be renamed

model->setData(model->index(0,0),tr("This is input well data file"),Qt::WhatsThisRole| Qt::ItemIsEditable);

aamer4yu
6th June 2010, 08:43
If you dont want the item editable, shouldnt you be using NOT of Qt::ItemIsEditable ( ~Qt::ItemIsEditable) :rolleyes:

xiongxiongchuan
8th June 2010, 17:15
model->setData(model->index(0, 0), tr("Well Data"), Qt::DisplayRole|~Qt::ItemIsEditable);
i using the code as this ,however,it did't display the lable ,and still can be edited:p

Ginsengelf
9th June 2010, 07:18
You have to set flags, not data.

Ginsengelf

aamer4yu
9th June 2010, 07:53
As far as I remember, I guess the flags are stored in standard item with (Qt::UserRole - 1)

xiongxiongchuan
9th June 2010, 17:29
could you give me some more details ,thanks !

saa7_go
10th June 2010, 05:21
try this code:



yourListView->setEditTriggers(QAbstractItemView::NoEditTriggers) ;