QTableView or QTableWidget ?



  • 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]



  • 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.



  • 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.



  • 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



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



  • 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?)



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



  • 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.



  • 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



  • [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.





  • [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.


Log in to reply
 

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