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. Unwanted checkbox in QTableView

Unwanted checkbox in QTableView

Scheduled Pinned Locked Moved Solved General and Desktop
qtableviewmacos
7 Posts 3 Posters 1.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.
  • A Offline
    A Offline
    artwaw
    wrote on 12 May 2021, 12:20 last edited by
    #1

    Good afternoon,
    I just have the weirdest problem I ever encountered.
    I am running macOS, with Qt 15.15.2 clang x64, QtCreator latest stable, all from online installer.

    So - one of my projects, MDI application, has logger as a feature. MDI window's widget is QWidget on it I have QTableView for logs and QLineEdit and QComboBox for filtering.
    Data source is QSqlTableModel + QSortFilterProxyModel - it works flawlessly. Database connection and log write from other MDI sub windows are all ok, read in this view is also ok.
    Second column is int with id of the service that wrote the log entry.

    The annoying part: with QSqlTableModel+QSortFilterProxyModel the data is displayed properly.
    But I wanted to change int to string with the name of the service, so I wrote quick class based on QSqlTableModel:
    Header file:

    #ifndef LOGMODEL_H
    #define LOGMODEL_H
    
    #include <QSqlTableModel>
    #include <QObject>
    
    const QStringList services={"System","and","other","services","used"};
    
    class LogModel : public QSqlTableModel
    {
        Q_OBJECT
    public:
        LogModel(QObject *parent = nullptr, QSqlDatabase db = QSqlDatabase());
        QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
    };
    
    #endif // LOGMODEL_H
    

    Code:

    #include "logmodel.h"
    
    LogModel::LogModel(QObject *parent, QSqlDatabase db) : QSqlTableModel(parent,db) {}
    
    QVariant LogModel::data(const QModelIndex &index, int role) const {
        if (!index.isValid()) { return QVariant(); }
        if (index.column()==1) {
            int val = QSqlTableModel::data(index,role).toInt();
            if (val>-1&&val<5) {
                return services.at(val);
            } else {
                return "Unknown service: "+QString::number(val);
            }
        }
        return QSqlTableModel::data(index,role);
    }
    

    When I replaced QSqlTableModel with this model all of a sudden the altered column has a CHECKBOX on the left side of the text:
    Screenshot 2021-05-12 at 13.17.04.png

    The view is genuine, with select single line options and I didn't touch the delegates. Edit triggers disabled.
    Does any of you have an idea what happened? It's not the first time I use such simple class to alter data returned but the very first time I have something like that.

    For more information please re-read.

    Kind Regards,
    Artur

    R 1 Reply Last reply 12 May 2021, 12:23
    0
    • C Online
      C Online
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on 12 May 2021, 12:22 last edited by
      #2

      @artwaw said in Unwanted checkbox in QTableView:

      Does any of you have an idea what happened?

      You don't respect the role in LogModel::data()

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

      A 1 Reply Last reply 12 May 2021, 12:26
      4
      • A artwaw
        12 May 2021, 12:20

        Good afternoon,
        I just have the weirdest problem I ever encountered.
        I am running macOS, with Qt 15.15.2 clang x64, QtCreator latest stable, all from online installer.

        So - one of my projects, MDI application, has logger as a feature. MDI window's widget is QWidget on it I have QTableView for logs and QLineEdit and QComboBox for filtering.
        Data source is QSqlTableModel + QSortFilterProxyModel - it works flawlessly. Database connection and log write from other MDI sub windows are all ok, read in this view is also ok.
        Second column is int with id of the service that wrote the log entry.

        The annoying part: with QSqlTableModel+QSortFilterProxyModel the data is displayed properly.
        But I wanted to change int to string with the name of the service, so I wrote quick class based on QSqlTableModel:
        Header file:

        #ifndef LOGMODEL_H
        #define LOGMODEL_H
        
        #include <QSqlTableModel>
        #include <QObject>
        
        const QStringList services={"System","and","other","services","used"};
        
        class LogModel : public QSqlTableModel
        {
            Q_OBJECT
        public:
            LogModel(QObject *parent = nullptr, QSqlDatabase db = QSqlDatabase());
            QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
        };
        
        #endif // LOGMODEL_H
        

        Code:

        #include "logmodel.h"
        
        LogModel::LogModel(QObject *parent, QSqlDatabase db) : QSqlTableModel(parent,db) {}
        
        QVariant LogModel::data(const QModelIndex &index, int role) const {
            if (!index.isValid()) { return QVariant(); }
            if (index.column()==1) {
                int val = QSqlTableModel::data(index,role).toInt();
                if (val>-1&&val<5) {
                    return services.at(val);
                } else {
                    return "Unknown service: "+QString::number(val);
                }
            }
            return QSqlTableModel::data(index,role);
        }
        

        When I replaced QSqlTableModel with this model all of a sudden the altered column has a CHECKBOX on the left side of the text:
        Screenshot 2021-05-12 at 13.17.04.png

        The view is genuine, with select single line options and I didn't touch the delegates. Edit triggers disabled.
        Does any of you have an idea what happened? It's not the first time I use such simple class to alter data returned but the very first time I have something like that.

        R Offline
        R Offline
        raven-worx
        Moderators
        wrote on 12 May 2021, 12:23 last edited by
        #3

        @artwaw
        probably the QSqlTableModel's implementation of flags() contains Qt::ItemIsUserCheckable for that column?

        --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
        If you have a question please use the forum so others can benefit from the solution in the future

        1 Reply Last reply
        0
        • C Online
          C Online
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on 12 May 2021, 12:25 last edited by
          #4

          @raven-worx said in Unwanted checkbox in QTableView:

          probably the QSqlTableModel's implementation of flags() contains Qt::ItemIsUserCheckable for that column?

          No, this only tells the view if the user can check or not. The drawing is done via data()

          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
          • C Christian Ehrlicher
            12 May 2021, 12:22

            @artwaw said in Unwanted checkbox in QTableView:

            Does any of you have an idea what happened?

            You don't respect the role in LogModel::data()

            A Offline
            A Offline
            artwaw
            wrote on 12 May 2021, 12:26 last edited by
            #5

            @Christian-Ehrlicher OMG, yes, I forgot. This is one of the most embarrassing mistakes I made.

            Changed condition to index.column()==1&&role==Qt::DisplayRole and now it works, of course.

            Thank you, I am going to sit in the corner now.

            For more information please re-read.

            Kind Regards,
            Artur

            1 Reply Last reply
            0
            • C Online
              C Online
              Christian Ehrlicher
              Lifetime Qt Champion
              wrote on 12 May 2021, 12:27 last edited by
              #6

              The first check I write in my custom model's data() function is the check if the correct role is given because of such things :)

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

              A 1 Reply Last reply 12 May 2021, 12:33
              2
              • C Christian Ehrlicher
                12 May 2021, 12:27

                The first check I write in my custom model's data() function is the check if the correct role is given because of such things :)

                A Offline
                A Offline
                artwaw
                wrote on 12 May 2021, 12:33 last edited by
                #7

                @Christian-Ehrlicher Good habit. I need to work on developing one.

                For more information please re-read.

                Kind Regards,
                Artur

                1 Reply Last reply
                0

                7/7

                12 May 2021, 12:33

                • Login

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