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. Data type comparison problem
QtWS25 Last Chance

Data type comparison problem

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

    Hi,
    I'm trying to align data in QTableView columns based on their data type. Data are retrieved from MySQL database through QSqlTableModel. I tried to compare the type of data at the data function subclassed from QSqlTableModel, with the following code:
    @
    QVariant TableModel::data(const QModelIndex &index, int role) const
    {
    QVariant dtype = QSqlTableModel::data(index, role).type();
    qDebug() << "Data Type:" << dtype;

    if (role == Qt::TextAlignmentRole)
    {
        if (dtype == QVariant::String) return Qt::AlignLeft + Qt::AlignVCenter;
        else return Qt::AlignRight + Qt::AlignVCenter;
    }
    return QSqlTableModel::data(index, role);
    

    }
    @

    The comparison doesn't seem to work as expected, making all columns appear as right aligned.
    Where did I go wrong? Can anyone help, please?

    1 Reply Last reply
    0
    • K Offline
      K Offline
      koahnig
      wrote on last edited by
      #2

      welcome to devnet

      You check dtype, but not role. So, there could be the problem. Otherwise you probably have to use the debugger and look in more detail what is there.

      Vote the answer(s) that helped you to solve your issue(s)

      1 Reply Last reply
      0
      • 8 Offline
        8 Offline
        8majkel8
        wrote on last edited by
        #3

        maybe

        @QVariant dtype = QSqlTableModel::data(index, role).type();@

        should be

        @QVariant::Type dtype = QSqlTableModel::data(index, role).type();@

        QVariant::type() returns enum not another QVairant.

        PS: QVariant::QVariant ( Type type ) Constructs a null variant of type type. From docs.

        1 Reply Last reply
        0
        • JeroentjehomeJ Offline
          JeroentjehomeJ Offline
          Jeroentjehome
          wrote on last edited by
          #4

          The QVariant::Type might be any kind of variable able to hold the enumarator value. An int might be enough ;-)
          Maybe remove the local variable at all? KISS is always a good advice. so maybe more like:
          @
          data()
          {
          qDebug() << QSqlTableModel::data(index.role).type();
          if (role == Qt::TextAlignmentRole)
          {
          if (QSqlTableModel::data(index, role).type() == Qt::TextAlignmentRole)
          {
          // do your return stuff
          }
          }
          }
          @

          Greetz, Jeroen

          1 Reply Last reply
          0
          • D Offline
            D Offline
            djmig
            wrote on last edited by
            #5

            Thank you all for the replies. Changed the code with:
            @
            QVariant::Type dtype = QSqlTableModel::data(index, role).type();
            @
            as it should, then added more qDebug between the lines, and found out that Qt spits out empty data while role == Qt::TextAlignmentRole. So I went back to the Docs and decided to reimplement views rendering using delegates. More in line to MVC pattern, I think.

            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