QTableView vs subclassing QAbstractTableModel



  • I don't know if I should use QTableView or if I should subclass QAbstractTableModel for my application;or even QTreeView. It would be nice if someone went over the reasons why someone should choose one over the other.

    All my Qt development experience comes from my (so far unmerged) contributions to QSynth. QSynth uses a few QTreeWidgets which all look something like the one in this image below. I wonder if it's use of QTreeWidget is unconventional, as none of the QTreeWidgets in QSynth have the drop-down lists that I see on images online. Is QTableView typically used for this purpose?
    0_1508027222027_Screenshot_20171014_162914.png

    But anyway, I know that I can't use either QTreeWidget or QTableWidget for what I want to do now. I want to make a musical scale editor. It will have a table (looking similar to the one the image), but only some columns will be editable. The table will be a list of notes in the scale. Entering a value in the cents column (a logarithmic measure of frequency) will change the value in the hertz column, and vice versa. There will be a button next to the table to add an additional note to the scale.

    So should I use QTableView, make a subclass of QAbstractTableModel, or something else?


  • Lifetime Qt Champion

    Hi and welcome to devnet,

    QTableView needs a model, so you have to make your own model anyway. From your description, it looks like a good option.



  • @Electric-Gecko
    QTreeWidget can show dropdown menu as you can see here

    QSynth simply may not created child items. So if you don't need dropdown menu, QTableWidget would be a good choice for simplicity.
    You can edit or add new row on table in both QTableWidget or QTableView(by changing QAbstractTable Model). Model-View approach has some advantage compared to QTableView or QTreeView for it uses the same model to show on QTreeView and QTableView.



  • You can use either QTreeWidget, QTableWidget, QTreeView+QStandardItemModel, QTableView+QStandardItemModel, QTreeView+QAbstactTableModel subclass, QTableView+QAbstactTableModel subclass for what you are trying to do.

    I would go with a QT*View+QStandardItemModel (tree or table doesn't matter).

    The functionalities you describe are:

    only some columns will be editable

    use QStandardItem::setFlag

    Entering a value in the cents column (a logarithmic measure of frequency) will change the value in the hertz column, and vice versa

    connect a slot to QStandardItemModel::dataChanged signal

    There will be a button next to the table to add an additional note to the scale

    connect a slot that calls QStandardItemModel::insertRows to the QPushButton::clicked signal



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