Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Simple Table View

    General and Desktop
    6
    10
    5275
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • rudag
      rudag last edited by

      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)

      1 Reply Last reply Reply Quote 0
      • G
        giesbert last edited by

        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

        Nokia Certified Qt Specialist.
        Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

        1 Reply Last reply Reply Quote 0
        • D
          dangelog last edited by

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

          Software Engineer
          KDAB (UK) Ltd., a KDAB Group company

          1 Reply Last reply Reply Quote 0
          • S
            Scylla last edited by

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

            1 Reply Last reply Reply Quote 0
            • G
              goetz last edited by

              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.

              http://www.catb.org/~esr/faqs/smart-questions.html

              1 Reply Last reply Reply Quote 0
              • A
                andre last edited by

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

                1 Reply Last reply Reply Quote 0
                • G
                  giesbert last edited by

                  [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?

                  Nokia Certified Qt Specialist.
                  Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

                  1 Reply Last reply Reply Quote 0
                  • D
                    dangelog last edited by

                    [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 :)

                    Software Engineer
                    KDAB (UK) Ltd., a KDAB Group company

                    1 Reply Last reply Reply Quote 0
                    • rudag
                      rudag last edited by

                      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?

                      1 Reply Last reply Reply Quote 0
                      • G
                        giesbert last edited by

                        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 :-))

                        Nokia Certified Qt Specialist.
                        Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

                        1 Reply Last reply Reply Quote 0
                        • First post
                          Last post