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. Model/View Tutorial role question
QtWS25 Last Chance

Model/View Tutorial role question

Scheduled Pinned Locked Moved Solved General and Desktop
modelviewtutorial
8 Posts 3 Posters 3.0k 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.
  • M Offline
    M Offline
    mmikeinsantarosa
    wrote on 22 Mar 2018, 21:20 last edited by VRonin
    #1

    The model/view tutorial in the docs loads data into a table.
    http://doc.qt.io/qt-5/modelview.html

    src @ examples/widgets/tutorials/modelview/2_formatting/mymodel.cpp

    To get a better idea of how this works, I placed a qDebug() in the data method to see what role value was being passed in

    QVariant HTCModel::data(const QModelIndex &index, int role) const
    {
    
        int row = index.row();
            int col = index.column();
    
            switch(role){
            case Qt::DisplayRole:
                qDebug() << "role received = Qt::DisplayRole #" << role;
                if (row == 0 && col == 1) return QString("<--left");
                if (row == 1 && col == 1) return QString("right-->");
    
                return QString("Row%1, Column%2")
                        .arg(row + 1)
                        .arg(col +1);
                break;
            case Qt::FontRole:
                if (row == 0 && col == 0) //change font only for cell(0,0)
                {
    
                    qDebug() << "role received = Qt::FontRole #" << role;
                    QFont boldFont;
                    boldFont.setBold(true);
                    return boldFont;
                }
                break;
            case Qt::BackgroundRole:
    
                if (row == 1 && col == 2)  //change background only for cell(1,2)
                {
                    qDebug() << "role received = Qt::BackgroundRole #" << role;
                    QBrush redBackground(Qt::red);
                    return redBackground;
                }
                break;
            case Qt::TextAlignmentRole:
    
                if (row == 1 && col == 1) //change text alignment only for cell(1,1)
                {
                    qDebug() << "role received = Qt::TextAlignmentRole #" << role;
                    return Qt::AlignRight + Qt::AlignVCenter;
                }
                break;
            case Qt::CheckStateRole:
    
                if (row == 1 && col == 0) //add a checkbox to cell(1,0)
                {
                    qDebug() << "role received = Qt::CheckStateRole: #" << role;
                    return Qt::Checked;
                }
            }
            return QVariant();
    }
    

    role values of 0, 1, 6, 7, 8, 9, 10 are being passed in.
    Where is this value coming from?
    Is there an invisible random number generator running in this tutorial?

    1 Reply Last reply
    0
    • V Offline
      V Offline
      VRonin
      wrote on 22 Mar 2018, 21:25 last edited by VRonin
      #2

      Is there an invisible random number generator running in this tutorial?

      No, it's the delegate that has to interrogate the model to know what to paint:

      • 0 = Qt::DisplayRole is the text in the cell
      • 1 = Qt::DecorationRole is the icon in the cell
      • 6 = Qt::FontRole is the font to use in the cell
      • 7 = Qt::TextAlignmentRole is the alignment of the text in the cell
      • 8 = Qt::BackgroundRole is the QBrush used to paint the background of the cell
      • 9 = Qt::ForegroundRole is the QBrush used to paint the text of the cell
      • 10 = Qt::CheckStateRole is the status of the checkbox in the cell

      See also: http://doc.qt.io/qt-5/qt.html#ItemDataRole-enum

      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
      ~Napoleon Bonaparte

      On a crusade to banish setIndexWidget() from the holy land of Qt

      1 Reply Last reply
      2
      • M Offline
        M Offline
        mmikeinsantarosa
        wrote on 22 Mar 2018, 22:10 last edited by
        #3

        I did a lookup as well to see what the enumerated values were for. The method is being called from somewhere as the arguments are being set. That's what I don't understand. What is calling MyModel::data with those argument values?
        I don't see that function called from anywhere in the tutorial and I don't see a connect signal/slot expression to set a delegate up.

        • mike
        1 Reply Last reply
        0
        • S Offline
          S Offline
          SGaist
          Lifetime Qt Champion
          wrote on 22 Mar 2018, 22:12 last edited by
          #4

          Hi,

          The view(s) on which you set 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
          2
          • V Offline
            V Offline
            VRonin
            wrote on 22 Mar 2018, 22:21 last edited by VRonin
            #5

            What is calling MyModel::data

            It's called in QStyledItemDelegate::initStyleOption (index.data(Role) is the same as index.model()->data(index,Role) if index.isValid()==true)

            I don't see that function called from anywhere in the tutorial and I don't see a connect signal/slot expression to set a delegate up.

            Any view has a defult delegate it's set in the constructor of the view: link to Qt Source

            "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
            ~Napoleon Bonaparte

            On a crusade to banish setIndexWidget() from the holy land of Qt

            1 Reply Last reply
            0
            • M Offline
              M Offline
              mmikeinsantarosa
              wrote on 22 Mar 2018, 23:16 last edited by
              #6

              i don't get it. Here's what's in main
              int main(int argc, char *argv[])
              {
              QApplication a(argc, argv);
              QTableView tableView;
              MyModel myModel(0);
              tableView.setModel( &myModel );
              tableView.show();
              return a.exec();
              }

              The only methods I see that would do anything are the initialization of the mymodel and the tableview.show() function.

              I have to assume that as the QAbstractTableModel's implemented functions for rowCount, columnCount are called as the view is created. and similarly data is also called from the view. So where's the iterator that determines what role is set to for each of the data calls?

              V 1 Reply Last reply 22 Mar 2018, 23:28
              0
              • M mmikeinsantarosa
                22 Mar 2018, 23:16

                i don't get it. Here's what's in main
                int main(int argc, char *argv[])
                {
                QApplication a(argc, argv);
                QTableView tableView;
                MyModel myModel(0);
                tableView.setModel( &myModel );
                tableView.show();
                return a.exec();
                }

                The only methods I see that would do anything are the initialization of the mymodel and the tableview.show() function.

                I have to assume that as the QAbstractTableModel's implemented functions for rowCount, columnCount are called as the view is created. and similarly data is also called from the view. So where's the iterator that determines what role is set to for each of the data calls?

                V Offline
                V Offline
                VRonin
                wrote on 22 Mar 2018, 23:28 last edited by VRonin
                #7

                The only methods I see that would do anything are the initialization of the mymodel and the tableview.show() function.

                Quite the opposite:

                1. QTableView tableView; calls the constructor of QTableView
                2. the costructor calls the base class constructor
                3. that construcor calls the init() method that creates the delegate q->setItemDelegate(new QStyledItemDelegate(q));
                4. When the view needs to repaint it calls QStyledItemDelegate::paint
                5. paint() calls QStyledItemDelegate::initStyleOption
                6. inside that fuction you can see the calls to index.data(Qt::FontRole);, index.data(Qt::TextAlignmentRole);, etc.

                So where's the iterator that determines what role is set to for each of the data calls?

                There's no iterator they are hard coded.

                "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                ~Napoleon Bonaparte

                On a crusade to banish setIndexWidget() from the holy land of Qt

                1 Reply Last reply
                2
                • M Offline
                  M Offline
                  mmikeinsantarosa
                  wrote on 23 Mar 2018, 00:35 last edited by
                  #8

                  So it's built into the Qt base library then.

                  I noticed also that any time the table gets clicked that method must get called again because I get another complete set of qDebug() messages. So paint() makes sense then as to why that happens.

                  Thanks

                  1 Reply Last reply
                  0

                  1/8

                  22 Mar 2018, 21:20

                  • Login

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