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. How to set QComboxItem in a QStyledItemDelegate
QtWS25 Last Chance

How to set QComboxItem in a QStyledItemDelegate

Scheduled Pinned Locked Moved General and Desktop
16 Posts 4 Posters 5.7k Views
  • 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.
  • D Offline
    D Offline
    DBoosalis
    wrote on last edited by
    #1

    I have a QStyleItemDelegate assigned to a QTableView column. This delegate has both QLineEdit and QComboBox editors depending on the data. THis delegate works fine operating on user input, but I would like to know how to programatically set the value of item when the editor is a QComboBox. Is there an easy way to do this ? Any help is very much appreciated

    1 Reply Last reply
    1
    • C Offline
      C Offline
      ckakman
      wrote on last edited by
      #2

      Hi,

      I am not experienced with Qt's model/view framework so I almost don't know what I am talking about, but can "this":http://doc.qt.io/qt-5/qstyleditemdelegate.html#setEditorData be what you are looking for?

      Since you are using a QTableView and not a QTableWidget, I guess you need to manipulate the model programmatically so that the data shows up in the UI.

      1 Reply Last reply
      0
      • D Offline
        D Offline
        DBoosalis
        wrote on last edited by
        #3

        Thanks for the hint, I looked at that call QAbstractItemDelegate::setEditorData() but not sure how to call it, as I do not have a handle to the first argument it requires (QWidget *editor), also not sure if it will work as it sets the value of the Editor, I want to set the value of the QStandardItem to a value found in a QStyledItemDelegate->Editor->QComboBox. Maybe QAbstractItemDelegate::setModelData() is the way to go, but again I do not have a handle to all the arguments this method requires.

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Hi,

          setEditorData is called for you when you activate the edition mode of your cell. You can reimplement it when you have custom editor. In that function you receive the index corresponding to the cell you are going to edit. With that you can retrieve the value and update your editor accordingly.

          setModelData is called for you when the editor is closed so you can store back the value to your model, so in that one you can call your QComboBox::currentIndex/text and store it in the model.

          Hope it helps

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0
          • D Offline
            D Offline
            DBoosalis
            wrote on last edited by
            #5

            Hi

            Still not sure how to call setModelData() from outside the Delegate. I have a Custom delegate which inheerits from QStyedItemDelegate and basically just establishes the createEdtior() method. What I want to be set an index of QComboBox that is part of createEditor method. Any more suggestions on how I can set comboBox->setCurrentIndex(1) for example

            1 Reply Last reply
            1
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              You don't call setModelData outside the delegate. If you want to modify the model outside the delegate you call the model's setData.

              Do it in setEditorData, however the content of the combo box should reflect the the content of the model

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              0
              • D Offline
                D Offline
                DBoosalis
                wrote on last edited by
                #7

                Could not get it to work. item->setData() and model->setData() for every role (UserRole,EditRole, etc) would never trigger a QStyledItemDelegate::setModelData();

                Maybe it is a bug

                1 Reply Last reply
                1
                • SGaistS Offline
                  SGaistS Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  Neither will, setModelData is called after you closed the editor

                  Interested in AI ? www.idiap.ch
                  Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                  1 Reply Last reply
                  0
                  • D Offline
                    D Offline
                    DBoosalis
                    wrote on last edited by
                    #9

                    Yeah I know now. So do you have any advise on how I can achieve my goal here

                    1 Reply Last reply
                    0
                    • SGaistS Offline
                      SGaistS Offline
                      SGaist
                      Lifetime Qt Champion
                      wrote on last edited by
                      #10

                      You wrote: comboBox->setCurrentIndex(1);

                      Where does this "1" come from ? Is it a fixed value ?

                      Interested in AI ? www.idiap.ch
                      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                      1 Reply Last reply
                      0
                      • D Offline
                        D Offline
                        DBoosalis
                        wrote on last edited by
                        #11

                        Just forcing the index to be one so I can see something. The combobox has at least two entries

                        1 Reply Last reply
                        0
                        • A Offline
                          A Offline
                          alex_malyu
                          wrote on last edited by
                          #12

                          Subclass and reimplement QStyledItemDelegate::createEditor

                          call QStyledItemDelegate::createEditor version there then check
                          if widget returned from there is combobox, like below

                          @QWidget* MyStyledItemDelegate::createEditor( QWidget parent,
                          const QStyleOptionViewItem &option, const QModelIndex &index) const
                          {
                          QWidget
                          w = QStyledItemDelegate::createEditor( parent, option, index);
                          if( w->inherits("QComboBox") )
                          {
                          QComboBox* edit = static_cast<QComboBox*>( w );
                          ... do smth

                          }
                          return w;
                          }
                          @
                          .

                          1 Reply Last reply
                          0
                          • D Offline
                            D Offline
                            DBoosalis
                            wrote on last edited by
                            #13

                            Yes I have implemented createEditor(), it is used to create a combo box when a user clicks on a cell item, but how to call it from outside as you suggest is the question ? What do I use for the arguments. For instance what do I supply for the second argument -QStyeOptionViewItem

                            1 Reply Last reply
                            0
                            • SGaistS Offline
                              SGaistS Offline
                              SGaist
                              Lifetime Qt Champion
                              wrote on last edited by
                              #14

                              just do it in setEditorData

                              @
                              void MyDelegate::​setEditorData(QWidget * editor, const QModelIndex & index) const
                              {
                              QComboBox * comboBox = qobject_cast<QComboBox*>(editor);
                              if (comboBox) {
                              comboBox.setCurrentIndex(1);
                              }
                              }
                              @

                              Again, these functions are called for you, you don't call them

                              Interested in AI ? www.idiap.ch
                              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                              1 Reply Last reply
                              0
                              • A Offline
                                A Offline
                                alex_malyu
                                wrote on last edited by
                                #15

                                You do not call createEditor yourself, it will be called with appropriate parameters. All you need is replace base class version with yours and
                                add the behavior you want.

                                Also MyDelegate::​setEditorData mentioned above might be an option I think you goal was to change the behavior only once, so I think createEditor is better choice, but can't state it without testing.

                                1 Reply Last reply
                                0
                                • D Offline
                                  D Offline
                                  DBoosalis
                                  wrote on last edited by
                                  #16

                                  Well I got it to work for one item, but for two or more items the second item fails. I think the call to edit gets on the event loop and so things are not whre they ought to be. Here is what I have

                                  @// main loop where I want to set the data:
                                  for (int i=i=0;i<childCount;i++) {
                                  valueDestin = (FieldValueItem)destination.child(i,AvailableValueCol);
                                  QModelIndex mi = valueDestin->index();
                                  if ((i == 2) || (i == 3)) {
                                  fieldValueDelegate->setSilent(true,1); // set index to item
                                  availableFieldsTree->edit(mi);
                                  // qApp->processEvents(QEventLoop::WaitForMoreEvents,500);
                                  //availableFieldsTree->closePersistentEditor(mi);
                                  //qApp->processEvents(QEventLoop::WaitForMoreEvents,500);
                                  fieldValueDelegate->setSilent(false,-1);
                                  }
                                  ....
                                  void FieldValueDelegate::setSilent(bool on, int ci)
                                  {
                                  isSilent = on;
                                  comboIndex = ci;
                                  if (!isSilent)
                                  editCB = 0;
                                  }
                                  void FieldValueDelegate::setEditorData(QWidget * editor, const QModelIndex & index) const
                                  {
                                  qDebug() << "Set Editor Data" << FILE << LINE;
                                  QComboBox *cb = qobject_cast <QComboBox *> (editor);
                                  if (cb && isSilent) {
                                  cb->setCurrentIndex(comboIndex);
                                  }
                                  QStyledItemDelegate::setEditorData(editor,index);
                                  }

                                  @

                                  If I just have i set to 2, or i set to 3 (that is one index) in the loop above the index will get set, and I will see the result in my treeview, but when I try to both indexes in it fails on the second pass . There is an error message from Qt on the second iiteration:
                                  editing failed

                                  I guess Qt was not made to do this kind of stuff. If you know a quick fix please let me know, otherwise I will just give this thread a rest. Thanks to all that tried to help

                                  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