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. TableView cell showing data and image

TableView cell showing data and image

Scheduled Pinned Locked Moved General and Desktop
5 Posts 2 Posters 1.6k Views 1 Watching
  • 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
    mookie
    wrote on last edited by
    #1

    Im new to QT so theres probably something that i am missing here, but i have created the following class

    @
    // Log for database
    // DateTime, CurrentUser, LogType, Description

    LogModel::LogModel(QSqlDatabase database, QObject *parent)
    : QSqlRelationalTableModel(parent, database)
    {
    connect(this, SIGNAL(rowsInserted(const QModelIndex&, int, int)), SIGNAL(countChanged()));
    connect(this, SIGNAL(rowsRemoved(const QModelIndex&, int, int)), SIGNAL(countChanged()));

    }

    QVariant LogModel::data(const QModelIndex& idx, int role) const
    {
    QVariant value= QSqlQueryModel::data(idx, role);

    // Column 1 is for icon
    if (idx.column()==1 && role==Qt::DecorationRole)
    {
         QString iconstring = QString("%1").arg(idx.data().toString());
         QIcon icon = QIcon(QPixmap(iconstring));
         value= icon;
    
    
    }
    
      return value;
    

    }
    @

    in my main class I set the header data as follows

    @
    m_logModel->setTable(QLatin1String("Logs"));
    if (m_logModel->lastError().isValid())
    return false;
    m_logModel->setHeaderData(1,Qt::Horizontal,"",Qt::DecorationRole);
    m_logModel->setHeaderData(2,Qt::Horizontal,QObject::tr("Date"));
    m_logModel->setHeaderData(3,Qt::Horizontal,QObject::tr("User"));
    m_logModel->setHeaderData(4,Qt::Horizontal,QObject::tr("Description"));
    @

    The problem i am having is that cell 1 is showing both the returned icon as well as the result from that database.
    what am i doing incorrect?

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

      Hi and welcome to devnet,

      Do you mean that you get both the text from the database and the icon your are providing ? If so it's normal, the decoration doesn't hide the text.

      You would need to either not return the text when asks for the display role, add a QIdentityProxyModel that does that (keeps your model clean) or create a QStyledItemDelegate that doesn't paint the text.

      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
      • M Offline
        M Offline
        mookie
        wrote on last edited by
        #3

        Ill try that, i suppose thats why it comes across as data a second time but with a different role? can i just return null?

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

          Yes you can, but are you sure you won't need it elsewhere ? That's why you should rather use QIdentityProxyModel or a custom delegate. Doing so your model will be easily reusable.

          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
          • M Offline
            M Offline
            mookie
            wrote on last edited by
            #5

            Yes, im not using this data for anything other than for logging purposes. I will look into QIdentityProxyModel though. thank you for your 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