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. (Porting old code - 5.14.2) Custom model derived from QAbstractItemModel receiving the wrong information?

(Porting old code - 5.14.2) Custom model derived from QAbstractItemModel receiving the wrong information?

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 2 Posters 220 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.
  • L Offline
    L Offline
    linuxkid
    wrote on last edited by
    #1

    Hey all, I'm under the need to add some functions on the fly, so I started looking at this old code which derives from QAbstractItemModel, theres a vector which holds the color of the row, setData looks like this:

    bool ModeloComprobantes::setData(const QModelIndex &index, const QVariant &value, int role)
    {
        if (index.row() > datos.size() || index.column()>titulos.size())
            return false;
    
        if (!index.isValid())
            return false;
    
        QVector<int> roles;
    
        if (role == Qt::EditRole)
        {
            datos[index.row()].insert(titulos[index.column()],value);
            roles.append(role);
        }
        if (role == Qt::BackgroundColorRole)
        {
            qDebug() << "ModeloComprobantes::setData(backgroundcolorrole):" << value;
            colorFila[index.row()] = QColor(value.toInt());
            roles.append(role);
        }
        emit dataChanged(index,index,roles);
        return true;
    }
    

    However, even though setData sends QColor("green") through the following sentence:

    model->setData(model->index(index.row(),2/* column*/,QColor("green"),Qt::BackgroundColorRole);
    

    The qDebug() line in the custom model tells me that the received color is QColor(ARGB 1, 0, 0, 0.501961, 0) which is seen black on the QTableView.

    I do realize that the Role name has changed, so adding this feature in the old codebase having to migrate it over seems like a lot of lost time, however I wish to know what I'm doing wrong for the sake of completeness

    Can anybody point me out?

    Christian EhrlicherC 1 Reply Last reply
    0
    • L linuxkid

      Hey all, I'm under the need to add some functions on the fly, so I started looking at this old code which derives from QAbstractItemModel, theres a vector which holds the color of the row, setData looks like this:

      bool ModeloComprobantes::setData(const QModelIndex &index, const QVariant &value, int role)
      {
          if (index.row() > datos.size() || index.column()>titulos.size())
              return false;
      
          if (!index.isValid())
              return false;
      
          QVector<int> roles;
      
          if (role == Qt::EditRole)
          {
              datos[index.row()].insert(titulos[index.column()],value);
              roles.append(role);
          }
          if (role == Qt::BackgroundColorRole)
          {
              qDebug() << "ModeloComprobantes::setData(backgroundcolorrole):" << value;
              colorFila[index.row()] = QColor(value.toInt());
              roles.append(role);
          }
          emit dataChanged(index,index,roles);
          return true;
      }
      

      However, even though setData sends QColor("green") through the following sentence:

      model->setData(model->index(index.row(),2/* column*/,QColor("green"),Qt::BackgroundColorRole);
      

      The qDebug() line in the custom model tells me that the received color is QColor(ARGB 1, 0, 0, 0.501961, 0) which is seen black on the QTableView.

      I do realize that the Role name has changed, so adding this feature in the old codebase having to migrate it over seems like a lot of lost time, however I wish to know what I'm doing wrong for the sake of completeness

      Can anybody point me out?

      Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @linuxkid said in (Porting old code - 5.14.2) Custom model derived from QAbstractItemModel receiving the wrong information?:

      QColor(value.toInt());

      This is wrong - you convert a QVariant to an integer and assign it a QColor.

      QColor(value.value<QColor>());

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      L 1 Reply Last reply
      1
      • Christian EhrlicherC Christian Ehrlicher

        @linuxkid said in (Porting old code - 5.14.2) Custom model derived from QAbstractItemModel receiving the wrong information?:

        QColor(value.toInt());

        This is wrong - you convert a QVariant to an integer and assign it a QColor.

        QColor(value.value<QColor>());

        L Offline
        L Offline
        linuxkid
        wrote on last edited by
        #3

        @Christian-Ehrlicher Christian, thanks again, I edited the code to send a QVariant(color.rgba()) and on the receiving end take it in as QColor(value.toUint())

        Thanks again for your time, I'm marking this one as solved!

        Christian EhrlicherC 1 Reply Last reply
        0
        • L linuxkid has marked this topic as solved on
        • L linuxkid

          @Christian-Ehrlicher Christian, thanks again, I edited the code to send a QVariant(color.rgba()) and on the receiving end take it in as QColor(value.toUint())

          Thanks again for your time, I'm marking this one as solved!

          Christian EhrlicherC Offline
          Christian EhrlicherC Offline
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @linuxkid said in (Porting old code - 5.14.2) Custom model derived from QAbstractItemModel receiving the wrong information?:

          nd take it in as QColor(value.toUint())

          Use my solution as this is the correct one.

          Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
          Visit the Qt Academy at https://academy.qt.io/catalog

          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