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. [Solved] Understanding Model/View programming
QtWS25 Last Chance

[Solved] Understanding Model/View programming

Scheduled Pinned Locked Moved General and Desktop
8 Posts 2 Posters 2.3k 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.
  • B Offline
    B Offline
    Baphomet
    wrote on last edited by
    #1

    Hi there
    I read "this article":http://qt-project.org/doc/qt-4.8/model-view-programming.html which explained using models, views, delegates and so on. But I don't understand something about delegates and therefore I create this topic.
    When user double-clicked on view item the delegate for this item creates an editor and then he is placed in item rect. But I don't understand when invokes the setModelData() function and when the editor closed after editing.
    Is it happens when user press Enter button or click on another cell(in my case I use QTableView) or at another time?

    Thank you in advance.

    C/C++, Qt

    1 Reply Last reply
    0
    • B Offline
      B Offline
      Baphomet
      wrote on last edited by
      #2

      Hi,
      I am apologize if put it is not clear. I mean next.
      This is my code
      @void FloatDelegate::setModelData(QWidget *editor, QAbstractItemModel model, const QModelIndex &index) const {
      QVariant text = (static_cast<QLineEdit
      >(editor))->text();

      if (model->setData(index, text, Qt::EditRole)) {
          closeEditor(editor);
      }
      // show tooltip
      else {
      }
      

      }@

      There I call model's function setData(). In case of valid data emits signal dataChanged() and function return true.
      @bool CriteriaTableModel::setData(const QModelIndex &index, const QVariant &value, int role) {
      if (!index.isValid()) {
      return false;
      }

      if ( mSM->suggestData(mParams.value( index.column() ), value) ) {
          emit dataChanged(index, index);
      
          return true;
      }
      else {
          return false;
      }
      

      }@

      mSM - object of class that contains app logic, don't pay attention to it.
      If setData() responses true so I emit signal closeEditor() otherwise I want show to user a tooltip which prompts him what going wrong.
      So I want ask if I do all properly?

      C/C++, Qt

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

        AFAIK, setModelData is not the place for validation, you should have that part in your editor logic.

        Also, note that your model setData doesn't do anything related to the data your are giving it so you won't have anything saved.

        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
        • B Offline
          B Offline
          Baphomet
          wrote on last edited by
          #4

          [quote author="SGaist" date="1416698916"]so you won't have anything saved.[/quote]
          Hi
          What this phrase means? I have to save editor data in setModelData() of delegate?
          In my app I need do some verification of data entered by user, therefore i invoke suggestData().
          If I don't must verify in the setModelData() where I can do it?

          C/C++, Qt

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

            That phrase mean that your implementation of CriteriaTableModel::setData doesn't save anything in your model unless suggestData store something, does it ?

            The idea of setModelData is to take your data from your editor and put them in the model. However it's not the place for validation, that's your editor job.

            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
            • B Offline
              B Offline
              Baphomet
              wrote on last edited by
              #6

              But not always editor can verify data. I explain.
              One of the columns consists of strings that must be unique. I check if entered by user string is really unique by mSM->suggestData(). Or you propose to perform validation when user changes the data of editor?
              Now if string is already exists I can't add it to underlying data structure.
              Now my CriteriaTableModel::setData() looks like this
              @bool CriteriaTableModel::setData(const QModelIndex &index, const QVariant &value, int role) {
              if (!index.isValid() || role != Qt::EditRole) {
              return false;
              }

              if ( _sm->setCriterionData( mParams.value(index.column()), value.toString() ) ) {
                  if (index.column() == mNameItemColumn) {
                      int row = _rows.values().indexOf(mSelectedCriterionName);
                      mSelectedCriterionName = index.data(Qt::EditRole).toString();
                      _rows.insert(row, mSelectedCriterionName);
                  }
              
                  emit dataChanged(index, index);
                  return true;
              }
              else {
                  return false;
              }
              

              }@
              I stored only data from the mNameItemColumn in my model. _rows is a QHash which is an internal model data.

              C/C++, Qt

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

                You can either give your editor access to your model to ensure no invalid input is given or give it a list of invalid input based on your model content.

                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
                • SGaistS Offline
                  SGaistS Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  You're welcome !

                  If this answers your question, then please update the thread title prepending [solved] so other forum users may know a solution has been found :)

                  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

                  • Login

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