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. QTableView or QTableWidget ?
Forum Updated to NodeBB v4.3 + New Features

QTableView or QTableWidget ?

Scheduled Pinned Locked Moved General and Desktop
12 Posts 4 Posters 22.1k 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.
  • H Offline
    H Offline
    huckfinn
    wrote on last edited by
    #1

    Hello community,

    I have a

    @QHash<qint32, DataItem> myQHash;@

    where value myQHash consists of a few primitive variables (qint32, QString, etc.): "See here":http://developer.qt.nokia.com/forums/viewthread/7419/

    I would like to display all key-value pairs, on a Table.
    I already have worked with QTableWidget, here I have to add each row manually.
    I never have used QTableView, which requires a ModelView concept. I think it would simplify my stuff, when I can intepret myQHash as an ModelView, such that each key-value pair is listed in my table as one row.

    Would you prefer QTableView or QTableWidget?

    Thank you for your hints. Cheers Huck

    [EDIT: update code formatting, @-tags must start at a new line, Volker]

    1 Reply Last reply
    0
    • G Offline
      G Offline
      goetz
      wrote on last edited by
      #2

      I'd prefer QTableWidget in this case. Writing a full blown model would just be too much work IMHO. You must subclass [[Doc:QAbstractItemModel]] and implement a bunch of methods.

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

      1 Reply Last reply
      0
      • A Offline
        A Offline
        andre
        wrote on last edited by
        #3

        I usually prefer QTableView. You can subclass [[Doc:QAbstractTableModel]] as a base. However, it also depends on the expected size of your data set, and the way you manipulate it. One problem is (for either case) that QHash does not suplly notification signals you can use to update your model. Another problem is, that a QHash is in undefined order. That may not be a good basis for any model.

        1 Reply Last reply
        0
        • H Offline
          H Offline
          huckfinn
          wrote on last edited by
          #4

          Ok, I started the QTabeWidget way. Here, I have a method, which inserts/displays given DataItems to the Table. Looks a bit ineffective. Is there a better way? for it?
          @
          /// \Add DataItems to the pTWTable to have all listed
          void MyDockWidget::addDataItemToTable(const DataItem * tDI)
          {
          QTableWidgetItem *idItem = new QTableWidgetItem(tr("%1").arg(tPI->getDataID()));
          QTableWidgetItem *nameItem = new QTableWidgetItem(tr("%1").arg(tPI->getDataName()));
          QTableWidgetItem *catItem = new QTableWidgetItem(tr("%1").arg(tPI->getDataCat()));
          QTableWidgetItem *lonItem = new QTableWidgetItem(tr("%1").arg(tPI->getDataLon()));

          this->pTWDetails->setItem(this->pTWDetails->rowCount()+1, 0, idItem);
          this->pTWDetails->setItem(this->pTWDetails->rowCount()+1, 1, nameItem);
          this->pTWDetails->setItem(this->pTWDetails->rowCount()+1, 2, catItem);
          this->pTWDetails->setItem(this->pTWDetails->rowCount()+1, 3, conItem);

          delete idItem;
          delete nameItem;
          delete catItem;
          delete conItem;
          }
          @

          Anyway, my Table is displayed on a widget, and the HorizintalHeaderLabels etc. are visible, but the items added by that method above are missing. At least I can't see them..

          What did I miss?

          Here the initialisation:
          @
          /// pTWDetails to display myQHash
          this->pTWDetails = new QTableWidget(this);
          QStringList twHeaders;
          twHeaders << "ID" << "Name" << "Categorie" << "Context";
          this->pTWDetails->setColumnCount(twHeaders.length());
          this->pTWDetails->setHorizontalHeaderLabels(twHeaders);
          this->pTWDetails->setColumnWidth(0, 40);
          this->pTWDetails->setColumnWidth(1, 80);
          this->pTWDetails->setColumnWidth(2, 60);
          this->pTWDetails->setColumnWidth(3, 60);
          @

          Cheers Huck

          1 Reply Last reply
          0
          • A Offline
            A Offline
            andre
            wrote on last edited by
            #5

            Well, it seems strange that you first create QTableWidgetItems, and them immediately delete them again...

            1 Reply Last reply
            0
            • L Offline
              L Offline
              ludde
              wrote on last edited by
              #6

              Indeed... and you are also creating a new row for each item (if it's even possible to add rows this way). That's probably not what you intended. (And you do realize all those this-> are not really needed?)

              1 Reply Last reply
              0
              • H Offline
                H Offline
                huckfinn
                wrote on last edited by
                #7

                Ah yes ;)
                @
                int cp = this->pTWDetails->rowCount();
                this->pTWDetails->setItem(cp, 0, idItem);
                this->pTWDetails->setItem(cp, 1, nameItem);
                this->pTWDetails->setItem(cp, 2, catItem);
                this->pTWDetails->setItem(cp, 3, conItem);
                @
                and I removed the deletes.

                Question: Aren't TableWidgets dynamic regarding their size' ? As above I set
                @this->pTWDetails->setColumnCount( twHeaders.length() );@
                , but I haven't set the rowCount. I thought this would grow at runtime?

                1 Reply Last reply
                0
                • L Offline
                  L Offline
                  ludde
                  wrote on last edited by
                  #8

                  I'm sure you can see for yourself if it works or not. If it doesn't, add a
                  @pTWDetails->insertRow(cp)@
                  before setting the items.

                  1 Reply Last reply
                  0
                  • H Offline
                    H Offline
                    huckfinn
                    wrote on last edited by
                    #9

                    Ok thank you. Operates perfect, now.

                    The next step for me is to display a rows content (when I press on that row) in a special Dialogbox. I already made the DIalogBox; nothing special, just a few QLabels and QLineEdits to display DataItems (int id, QString name, int cat, int con).

                    According to the description: "This signal is emitted whenever a cell in the table is clicked. The row and column specified is the cell that was clicked." And I realized this with emitting that signal via
                    @
                    // Display Rowelements on Child Dialog
                    connect( pTWDetails, SIGNAL(cellClicked(int, int)), this, SLOT( getCellItem(int, int)) );
                    @

                    Currently only one cell is marked in an other color, after I have clicked on that cell.
                    I have a small question regarding optical issues. It would look much prettier, if the whole row is marked instead of one single cell, after I have clicked in the table. Users would recognize that the whole row is meant instead of one cell.
                    Is there such a possibility ?(as a signal rowClicked(int row) or a QTableWidgets property?)

                    Cheers Huck

                    1 Reply Last reply
                    0
                    • A Offline
                      A Offline
                      andre
                      wrote on last edited by
                      #10

                      [quote author="huckfinn" date="1310027803"]Ok thank you. Operates perfect, now.

                      The next step for me is to display a rows content (when I press on that row) in a special Dialogbox. I already made the DIalogBox; nothing special, just a few QLabels and QLineEdits to display DataItems (int id, QString name, int cat, int con).

                      According to the description: "This signal is emitted whenever a cell in the table is clicked. The row and column specified is the cell that was clicked." And I realized this with emitting that signal via
                      @
                      // Display Rowelements on Child Dialog
                      connect( pTWDetails, SIGNAL(cellClicked(int, int)), this, SLOT( getCellItem(int, int)) );
                      @
                      [/quote]
                      Fine. So that works ok then?

                      [quote]
                      Currently only one cell is marked in an other color, after I have clicked on that cell.
                      I have a small question regarding optical issues. It would look much prettier, if the whole row is marked instead of one single cell, after I have clicked in the table. Users would recognize that the whole row is meant instead of one cell.
                      Is there such a possibility ?(as a signal rowClicked(int row) or a QTableWidgets property?)
                      [/quote]
                      Yes, there is. Use ::setSelectionBehavior(QAbstractItemView::SelectRows) on your view.

                      1 Reply Last reply
                      0
                      • L Offline
                        L Offline
                        ludde
                        wrote on last edited by
                        #11

                        Try the "selectionBehavior":http://doc.qt.nokia.com/latest/qabstractitemview.html#selectionBehavior-prop property.

                        1 Reply Last reply
                        0
                        • H Offline
                          H Offline
                          huckfinn
                          wrote on last edited by
                          #12

                          [quote author="Andre" date="1310028072"] Fine. So that works ok then? [/quote] Jepp, no problems. [quote] Yes, there is. Use ::setSelectionBehavior(QAbstractItemView::SelectRows) on your view. [/quote] Gracias.

                          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