Simple Table View



  • I'm translating a little app that I made in PHP-Gtk to Qt. I was going well till I had to do what in Gtk is a GtkTreeView, but in Qt I guess it's a QTableView or a QTableWidget, I don't know which one to use. Basically it has 3 columns (Accounts, Password, Group Id) and must have the option to sort the items asc/desc. Here is a pic of my app:

    !http://img04.imgland.net/aB23Ojk2mE.jpg(pic)!

    If you double-click any item, the entries below will be filled according to the item you cliked.
    If you click Prev/Next buttons, it will clear the list and add the new items.

    I'm really lost because:

    1. I dont know what is better to my case: QTableView or QTableWidget
    2. I couldn't test any of both, since I didn't find any simple example to newbies

    So... could someone show me how to do a simple table like in the pic? All the examples I found were difficult.

    Thanks

    PS: (I just want the table, forget the other widgets)



  • Hi,

    you have two choices:

    QTableView or "QTableWidget":http://doc.qt.nokia.com/4.7/qtablewidget.html , as you already suggested.

    The main difference is: QTableWidget is derived from QTableView.

    For QTableView, you have to create your own "model":http://doc.qt.nokia.com/4.7/model-view-programming.html where as QTableWidget already contains a QStandardItemModel

    QTableWidget (from programmers point of view) is just a widget containg items, which you add "QTableWidgetItem":http://doc.qt.nokia.com/4.7/qtablewidgetitem.html



  • That doesn't seem to be a QTableView, but a QTreeView with an undecorated root.



  • You can use "QDataWidgetMapper":http://doc.trolltech.com/4.7-snapshot/qdatawidgetmapper.html to map the data to the dialog elements.



  • Peppe is right, QTreeView/Widget is the way to go. QTableView would have a "header" to the left too.

    For some simple data you can got with the widget, create the QTreeWidgetItems. You have to enable sorting in the header

    @
    treeWidget- >setSortingEnabled(true);
    @

    Or check the respective property in Qt Designer/Qt Creator's designer component.



  • Or you hide the header on the left, of course... :-)



  • [quote author="Volker" date="1300791943"]Peppe is right, QTreeView/Widget is the way to go. QTableView would have a "header" to the left too.
    [/quote]

    Why use a QTreeWidget, then a QTableWidget is enough? Just hide the headers you don't need.
    If you don't like the grid lines, hide them. Then you've got it.

    If you don't want a tree, you shouldn't use one, right?



  • [quote author="Gerolf" date="1300805576"]
    [quote author="Volker" date="1300791943"]Peppe is right, QTreeView/Widget is the way to go. QTableView would have a "header" to the left too.
    [/quote]

    Why use a QTreeWidget, then a QTableWidget is enough? Just hide the headers you don't need.
    If you don't like the grid lines, hide them. Then you've got it.

    If you don't want a tree, you shouldn't use one, right?[/quote]

    Because of the way of how it's rendered. A table-like view without Excel-like "cells" is done using a tree view, that's it :)



  • Ok, thanks for the help. I'm using QTreeWidget now and I implemented it just to see how it works:

    @QStandardItemModel *model = new QStandardItemModel;
    QStandardItem *parentItem = model->invisibleRootItem();
    for (int i = 0; i < 4; ++i) {
    QStandardItem *item = new QStandardItem(QString("item %0").arg(i));
    parentItem->appendRow(item);
    }

    QTreeView *view = new QTreeView;
    view->setModel(model);
    view->show();
    layout->addWidget(view);@
    

    My question now is:
    How do I add the headers (Account, Password, ...) like in my pic?



  • There is a "QStandardItemModel::setHorizontalHeaderLabels":http://doc.qt.nokia.com/latest/qstandarditemmodel.html#setHorizontalHeaderLabels

    Just read the docs, it's stated there :-)

    by the way: if you want to access the model later on, store the pointer in a member variable :-))


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.