PDA

View Full Version : QTreeView help



bepaald
15th August 2007, 14:31
Hello,

I need a little help getting a QTreeView to display some characters. Let me first say that I'm not a very good programmer and pretty new to QT.
I am trying to make a QTreeView which behaves like a multi-column QListView used to in qt3 (btw, why are columns no longer available in QListView? It doesn't seem necessary for the model/view-thing to remove them). My first question is just if this is basically correct. Here is my QTreeView code, I mostly pieced it together from examples:


#include <QtGui>

int main(int argc, char *argv[]){

QApplication app(argc, argv);

QTreeView *qtv = new QTreeView();
qtv->setItemsExpandable(false);
qtv->setRootIsDecorated(false);
qtv->setUniformRowHeights(true);
qtv->setSelectionBehavior(QAbstractItemView::SelectRows );
qtv->setSelectionMode(QAbstractItemView::SingleSelectio n);
qtv->setSortingEnabled(true);
qtv->sortByColumn(0, Qt::AscendingOrder);

QStandardItemModel *itemmodel = new QStandardItemModel();
itemmodel->setHorizontalHeaderLabels(QStringList() << "First" << "Second" << "Third");
for (int i = 0; i < 5; ++i)
for (int j = 0; j < 3; ++j)
itemmodel->setItem(i, j, new QStandardItem(QString("Row: ") + QString::number(i) + QString(", Column: ") + QString::number(j)));

qtv->setModel(itemmodel);
qtv->show();

return app.exec();
}

Now (though it might not be the proper or most efficient way to do it) this seems to do what I want it to. But when I put a string in like "Eléna" in the model using itemmodel->setItem(i, j, new QStandardItem(QString("Eléna"))), the treeview shows "Eléna" instead. If I access the text in the model using itemmodel->item(i,j)->text() it does show the correct text. I had this problem in qt3 as well, I just thought while I was learning qt4, I might as well get this out of the way too. So could someone please help me fix this?

Thanks,
bepaald

wysota
15th August 2007, 22:22
btw, why are columns no longer available in QListView? It doesn't seem necessary for the model/view-thing to remove them.
Qt4 assumes that lists have one dimension. More dimensions (2 or 3) are available in QTableView and QTreeView respectively.


Now (though it might not be the proper or most efficient way to do it) this seems to do what I want it to. But when I put a string in like "Eléna" in the model using itemmodel->setItem(i, j, new QStandardItem(QString("Eléna"))), the treeview shows "Eléna" instead.

You are using an incorrect encoding. Try QString::fromUtf8("Eléna") instead (if you are using a UTF-8 locale).