Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Simple Table View
Forum Updated to NodeBB v4.3 + New Features

Simple Table View

Scheduled Pinned Locked Moved General and Desktop
10 Posts 6 Posters 6.3k Views 1 Watching
  • 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.
  • rudagR Offline
    rudagR Offline
    rudag
    wrote on last edited by
    #1

    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
    0
    • G Offline
      G Offline
      giesbert
      wrote on last edited by
      #2

      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
      0
      • D Offline
        D Offline
        dangelog
        wrote on last edited by
        #3

        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
        0
        • S Offline
          S Offline
          Scylla
          wrote on last edited by
          #4

          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
          0
          • G Offline
            G Offline
            goetz
            wrote on last edited by
            #5

            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
            0
            • A Offline
              A Offline
              andre
              wrote on last edited by
              #6

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

              1 Reply Last reply
              0
              • G Offline
                G Offline
                giesbert
                wrote on last edited by
                #7

                [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
                0
                • D Offline
                  D Offline
                  dangelog
                  wrote on last edited by
                  #8

                  [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
                  0
                  • rudagR Offline
                    rudagR Offline
                    rudag
                    wrote on last edited by
                    #9

                    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
                    0
                    • G Offline
                      G Offline
                      giesbert
                      wrote on last edited by
                      #10

                      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
                      0

                      • Login

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • Users
                      • Groups
                      • Search
                      • Get Qt Extensions
                      • Unsolved