Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Forum Updated on Feb 6th

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

    General and Desktop
    3
    6
    7013
    Loading More Posts
    • 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.
    • S
      scarleton last edited by

      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

      1 Reply Last reply Reply Quote 0
      • G
        giesbert last edited by

        You can change the editTrigger of the view.

        Nokia Certified Qt Specialist.
        Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

        1 Reply Last reply Reply Quote 0
        • S
          scarleton last edited by

          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?

          1 Reply Last reply Reply Quote 0
          • S
            Sam last edited by

            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 :)

            1 Reply Last reply Reply Quote 0
            • S
              scarleton last edited by

              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;
              }@

              1 Reply Last reply Reply Quote 0
              • S
                Sam last edited by

                You are welcome!!!!

                1 Reply Last reply Reply Quote 0
                • First post
                  Last post