Results 1 to 2 of 2

Thread: QTreeView help

  1. #1
    Join Date
    Aug 2007
    Posts
    6
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default QTreeView help

    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:

    Qt Code:
    1. #include <QtGui>
    2.  
    3. int main(int argc, char *argv[]){
    4.  
    5. QApplication app(argc, argv);
    6.  
    7. QTreeView *qtv = new QTreeView();
    8. qtv->setItemsExpandable(false);
    9. qtv->setRootIsDecorated(false);
    10. qtv->setUniformRowHeights(true);
    11. qtv->setSelectionBehavior(QAbstractItemView::SelectRows);
    12. qtv->setSelectionMode(QAbstractItemView::SingleSelection);
    13. qtv->setSortingEnabled(true);
    14. qtv->sortByColumn(0, Qt::AscendingOrder);
    15.  
    16. itemmodel->setHorizontalHeaderLabels(QStringList() << "First" << "Second" << "Third");
    17. for (int i = 0; i < 5; ++i)
    18. for (int j = 0; j < 3; ++j)
    19. itemmodel->setItem(i, j, new QStandardItem(QString("Row: ") + QString::number(i) + QString(", Column: ") + QString::number(j)));
    20.  
    21. qtv->setModel(itemmodel);
    22. qtv->show();
    23.  
    24. return app.exec();
    25. }
    To copy to clipboard, switch view to plain text mode 

    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

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,360
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QTreeView help

    Quote Originally Posted by bepaald View Post
    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).

Similar Threads

  1. QTreeView repaint
    By graeme in forum Qt Programming
    Replies: 17
    Last Post: 13th March 2012, 13:27
  2. QTreeView: Holding a line on screen
    By gri in forum Qt Programming
    Replies: 1
    Last Post: 7th August 2007, 11:42
  3. background image in QTreeView
    By momesana in forum Qt Programming
    Replies: 2
    Last Post: 11th January 2007, 06:25
  4. paint QTreeView item !!
    By mcenatie in forum Qt Programming
    Replies: 2
    Last Post: 19th March 2006, 14:24
  5. QSortFilterProxyModel & QTreeView
    By Bear in forum Qt Programming
    Replies: 3
    Last Post: 31st January 2006, 15:04

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.