Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Regarding QItemDelegate for combobox

Regarding QItemDelegate for combobox

Scheduled Pinned Locked Moved General and Desktop
10 Posts 3 Posters 8.9k Views 1 Watching
  • 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.
  • I Offline
    I Offline
    Indrajeet
    wrote on last edited by
    #1

    Hi

    I am using QTableView & it has 3 columns.
    In the 3rd column I wanted to set ComboBox in alternate cells.

    Below is my CreateEditor function
    @
    QWidget *ComboBoxDelegate::createEditor(QWidget parent,const QStyleOptionViewItem &/ option */,const QModelIndex & index ) const
    {
    QComboBox *editor = new QComboBox(parent);
    editor->setEditable(true);
    editor->addItem("1");
    return editor;
    }
    @
    But this above function will create combobox in all the cells in which user double clicks how to restrict for alternate rows.

    And also is it possible to show combobox on single click instead off user double clicking in each ce

    Regards
    Indrajeet

    1 Reply Last reply
    0
    • S Offline
      S Offline
      Sam
      wrote on last edited by
      #2

      Hi,

      In order to allow single click you can try

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

      and for alternate rows in the createEditor you can try

      @QWidget *ComboBoxDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
      {
      if(!(index.row()%2==0))
      {
      QComboBox editor = new QComboBox(parent);
      editor->setEditable(true);
      editor->addItem("1");
      editor->installEventFilter(const_cast<ComboBoxDelegate
      >(this));
      return editor;
      }
      return 0;
      }@
      This works but i am not sure whether it should return 0 or something else.

      1 Reply Last reply
      0
      • I Offline
        I Offline
        Indrajeet
        wrote on last edited by
        #3

        Hi Sam

        Thanks For your reply.

        Actually i have the same problem of return value.

        I tried with return 0;

        I have set all cells in 3rd column as editable but if I return 0 from createEditor() combobox is not appearing in that cell but that cell is also not able to edit so how to make it editable again.

        1 Reply Last reply
        0
        • S Offline
          S Offline
          Sam
          wrote on last edited by
          #4

          Kindly share some more code. The same code works for my sample application.

          1 Reply Last reply
          0
          • I Offline
            I Offline
            Indrajeet
            wrote on last edited by
            #5

            Hi

            I have created my Item cells for 3rd column in Table View as below
            @
            QStandardItem *temp3 = new QStandardItem("");
            obj->tablemodel->setItem( i, 2 ,temp3 );
            temp3->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled |Qt::ItemIsEditable);
            @

            I have assigned ItemDelegate to my TableView Column As below

            @
            ComboBoxDelegate* mycombodelegate = new ComboBoxDelegate(this);
            ui.tableView->setItemDelegateForColumn(2,mycombodelegate);
            @

            My create Editor Function is as I have posted in my 1st post of this thread.

            So Now If I return 0 from createEditor() the other cells in 3rd column which I have created as editable are no more editable why is it so?

            1 Reply Last reply
            0
            • S Offline
              S Offline
              Sam
              wrote on last edited by
              #6

              -You can download a sample application from here-. The functionality is added to column no 3.

              Check if this works.

              1 Reply Last reply
              0
              • I Offline
                I Offline
                Indrajeet
                wrote on last edited by
                #7

                Hi Sam

                Thanks for send the code.

                But what i meant to say is as we are creating the combobox in alternate cells the cells in which combobox is not there those should be user editable right means user can enter any value in those cells but those cells are becoming non editable why?

                1 Reply Last reply
                0
                • S Offline
                  S Offline
                  Sam
                  wrote on last edited by
                  #8

                  Here is another approach.

                  @QWidget *ComboBoxDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
                  {
                  if(!(index.row()%2==0))
                  {
                  QComboBox editor = new QComboBox(parent);
                  editor->setEditable(true);
                  editor->installEventFilter(const_cast<ComboBoxDelegate
                  >(this));
                  return editor;
                  }
                  else
                  {
                  QLineEdit editor = new QLineEdit(parent);
                  editor->installEventFilter(const_cast<ComboBoxDelegate
                  >(this));
                  return editor;
                  }

                  }
                  void ComboBoxDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
                  {
                  if(!(index.row()%2==0))
                  {
                  QComboBox combo = static_cast<QComboBox>(editor);
                  combo->addItems(Dialog::getCity());
                  // int indx = Dialog::getCity().indexOf(index.data(Qt::DisplayRole).toString());
                  combo->setCurrentIndex(indx);
                  }

                  else
                  {
                      QLineEdit *lineEdit = static_cast<QLineEdit*>(editor);
                      //int indx = Dialog::getCity().indexOf(index.data(Qt::DisplayRole).toString());
                      //lineEdit->setText(Dialog::getCity().at(indx));
                      lineEdit->setText("What ever text you want to display");
                  }
                  

                  }
                  void ComboBoxDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
                  {
                  if(!(index.row()%2==0))
                  {
                  QComboBox combo = static_cast<QComboBox>(editor);
                  model->setData(index,combo->currentText());
                  }
                  else
                  {
                  QLineEdit lineEdit = static_cast<QLineEdit>(editor);
                  model->setData(index,lineEdit->text());
                  }

                  }
                  void ComboBoxDelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const
                  {
                  editor->setGeometry(option.rect);
                  }@

                  1 Reply Last reply
                  0
                  • G Offline
                    G Offline
                    goetz
                    wrote on last edited by
                    #9

                    The correct solution would be to return the base class' editor in case you do not return your own:

                    @
                    QWidget *ComboBoxDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
                    {
                    if(!(index.row()%2==0))
                    {
                    QComboBox editor = new QComboBox(parent);
                    editor->setEditable(true);
                    editor->installEventFilter(const_cast<ComboBoxDelegate
                    >(this));
                    return editor;
                    } else {
                    return QStyledItemDelegate::createEditor(parent, option, index);
                    }
                    }
                    @

                    And the same in the other methods too, of course. The standard implementation takes care of returning the correct widget in resepect to the type of the data, e.g. a QDateEdit for a QDate value, etc.

                    http://www.catb.org/~esr/faqs/smart-questions.html

                    1 Reply Last reply
                    0
                    • I Offline
                      I Offline
                      Indrajeet
                      wrote on last edited by
                      #10

                      Hi Volker

                      Thanks for the reply.

                      I did the same you suggested and it is working fine.
                      But the proble which i am facing is when I return below mentioned line
                      from createEditor(),setEditorData(),setModelData()

                      @
                      return QStyledItemDelegate::createEditor(parent, option, index);
                      @

                      If i have some text already present in my QTableView cell and if user clicks in that cell the text which is there is disappearing and that cell is becoming editable which it should be but i also want to have that text for user to show how to do this?

                      1 Reply Last reply
                      0

                      • Login

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • Users
                      • Groups
                      • Search
                      • Get Qt Extensions
                      • Unsolved