HOWTO get a ComboBox in a TableView to activate on a single click?



  • I have placed a ComboBox in a TableView. Currently the user needs to double click to get it into edit mode. What do I need to do get it into Edit mode with a single click?

    Sam



  • You can change the editTrigger of the view.



  • Well, that is a good start, the only problem is that I don't want all cells to be editable. The best option I have found, which I am not a huge fan of, is to subclass the TableView and implement my own custom edit to return false for columns that I don't want to be editable. Is there a better way?



  • Hi,

    You can try the following approach.

    1. For your tableView change the editTrigger

    @ui->tableView->setEditTriggers(QAbstractItemView::AllEditTriggers);@

    1. Then you need to override the Flags function for your model. For eg i have :
      @class MyStandardItemModel : public QStandardItemModel
      {
      public:
      MyStandardItemModel();
      protected:
      Qt::ItemFlags flags(const QModelIndex &index) const;
      };@

    and in the implementation file you can write:

    @Qt::ItemFlags MyStandardItemModel::flags(const QModelIndex &index) const
    {
    Qt::ItemFlags flags = QAbstractItemModel::flags(index);
    if (index.column() == 2) /* here i want column no 2 only to be editable */
    {
    flags |= Qt::ItemIsEditable;
    return flags;
    }
    return QAbstractItemModel::flags(index);
    }@

    This worked for me :)



  • Thank you! You provided me with the road map I needed to get my results. What I did differently was how I handled the flags. The key was to only enable the edit on the columns I wanted to allow editing. Works like a champ, thank you!!!

    @Qt::ItemFlags InvoiceItemSqlModel::flags(const QModelIndex &index ) const
    {
    Qt::ItemFlags flags = QSqlTableModel::flags(index);

    if( index.column() == productIdNo() ||
    index.column() == qtyNo() ||
    index.column() == priceNo())
    flags |= Qt::ItemIsEditable;
    else
    flags &= ~Qt::ItemIsEditable;

    return flags;
    }@



  • You are welcome!!!!


Log in to reply
 

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