Qt Forum

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

    Update: Forum Guidelines & Code of Conduct


    Qt World Summit: Early-Bird Tickets

    [Solved] Understanding Model/View programming

    General and Desktop
    2
    8
    2001
    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.
    • B
      Baphomet last edited by

      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 Reply Quote 0
      • B
        Baphomet last edited by

        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 Reply Quote 0
        • SGaist
          SGaist Lifetime Qt Champion last edited by

          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 Reply Quote 0
          • B
            Baphomet last edited by

            [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 Reply Quote 0
            • SGaist
              SGaist Lifetime Qt Champion last edited by

              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 Reply Quote 0
              • B
                Baphomet last edited by

                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 Reply Quote 0
                • SGaist
                  SGaist Lifetime Qt Champion last edited by

                  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 Reply Quote 0
                  • SGaist
                    SGaist Lifetime Qt Champion last edited by

                    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 Reply Quote 0
                    • First post
                      Last post