Important: Please read the Qt Code of Conduct -

Tableview style and delegate behavior in Qt

  • Hi,

    I got Tableview and a delegate based on qcombobox set for a certain column. When the tableview is shown I don't see the delegate. I only see it when I start editing the value. Is it possible to see the combobox all the time and not just when editing?


  • The code is something like this:

    @class comboBoxDelegate : public QItemDelegate
    comboBoxDelegate(QObject *parent = 0);

     QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option,
                           const QModelIndex &index) const;
     void setEditorData(QWidget *editor, const QModelIndex &index) const;
     void setModelData(QWidget *editor, QAbstractItemModel *model,
                       const QModelIndex &index) const;
     void updateEditorGeometry(QWidget *editor,
         const QStyleOptionViewItem &option, const QModelIndex &index) const;


    comboBoxDelegate *pComboBoxDelegate = new ComboBoxDelegate();
    ui->tableView->setItemDelegateForColumn(1, pComboBoxDelegate);@

  • Don't know if it'll help to you.I'm doing same thing but in another way. When editor isn't visible then just draw combobox in paintEvent of QTableWidget viewport in cells where combobox is editor, by using QStyle,QStyleOption and friends. It's much faster than show real combobox in each cell.

  • I look into it. My intention was to use a tableview rather than a tablewidget since I expect we have to address a large amount of data.

  • You can pull the same trick with QTableView, only you'll need to use a delegate to do the painting.

    However, note that it will take some effort to make it work like a combo box. Clicking on it will only activate the cell for editing, but the click won't be a click on the real combobox as that does not exist yet. Your users may get frustrated by objects in your table that look like a combobox, but don't act like one.

  • After consultation with my project manager we decided to change the setup a bit so in effect this is not an issue any more, but I think it can easily turn up again. I still have some problems with delegates. The non-edit state, still showed in the background when the editor was active, also the row height did not change when it should have leaving some objects clipped. But that's a story for another time.

Log in to reply