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. QTableView & CheckBox delegate alignmnent
QtWS25 Last Chance

QTableView & CheckBox delegate alignmnent

Scheduled Pinned Locked Moved General and Desktop
43 Posts 4 Posters 37.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.
  • G Offline
    G Offline
    giesbert
    wrote on last edited by
    #41

    No, you are to slow, and I'm at home :-))

    Nokia Certified Qt Specialist.
    Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

    1 Reply Last reply
    0
    • A Offline
      A Offline
      andre
      wrote on last edited by
      #42

      [quote author="harry" date="1301564361"]I tried to post the source as a wiki ( I created 2 separate pages - one for the delegate and one for the sortproxy thing), but I'm sure I messed it up.
      Am I supposed to add them to a category, or does an admin do that?

      I tried adding the [[Category... thing but it appeared as plain text.[/quote]

      I have modified your wiki page to add a link to "another solution":https://developer.qt.nokia.com/wiki/QSortFilterProxyModel_subclass_to_add_a_checkbox for adding checkboxes to an abstract item model. The use cases for both approaches are different, and I think they nicely supplement each other. I hope you don't mind.

      Edit:
      I have opened "this topic":https://developer.qt.nokia.com/forums/viewthread/5544/ to discuss the API of the class you posted in the snippet, as I think there is room for improvement.

      1 Reply Last reply
      0
      • C Offline
        C Offline
        confused
        wrote on last edited by
        #43

        [quote author="harry" date="1301486015"]Ok I found something that seems to work.
        (based on this http://www.qtcentre.org/archive/index.php/t-18675.html)

        I'm putting the code here, in case it helps someone else:

        The basic idea is that there is something like a QList that holds the indexes of the columns that you want to have checkboxes (I'm already thinking of adding another QList for readonly columns).
        Actual usage would be
        @
        CheckableSortFilterProxyModel *cfpm = new CheckableSortFilterProxyModel(this);
        QList<int> boolCols;
        boolCols.append( usrModel->fieldIndex("isActive") );
        boolCols.append( usrModel->fieldIndex("isOk") );
        cfpm->setParameters(boolCols);
        cfpm->setSourceModel( mySqlTableModel );
        myTableView->setModel(cfpm);
        @

        Here is the code:

        checkablesortfilterproxymodel.h:
        @
        #ifndef CHECKABLESORTFILTERPROXYMODEL_H
        #define CHECKABLESORTFILTERPROXYMODEL_H

        #include <QSortFilterProxyModel>

        class CheckableSortFilterProxyModel : public QSortFilterProxyModel
        {
        Q_OBJECT
        public:
        explicit CheckableSortFilterProxyModel(QObject *parent = 0);

        void setParameters(QList<int> boolCols);
        

        protected:
        QVariant data(const QModelIndex &index, int role) const;
        bool setData(const QModelIndex &index, const QVariant &value, int role);
        Qt::ItemFlags flags ( const QModelIndex & index ) const;

        signals:

        public slots:

        private:
        QList<int> booleanSet;

        };

        #endif // CHECKABLESORTFILTERPROXYMODEL_H
        @

        checkablesortfilterproxymodel.cpp:
        @
        #include "checkablesortfilterproxymodel.h"

        CheckableSortFilterProxyModel::CheckableSortFilterProxyModel(QObject *parent) :
        QSortFilterProxyModel(parent)
        {
        }

        void CheckableSortFilterProxyModel::setParameters(QList<int> boolCols) {
        booleanSet.clear();
        if (!boolCols.isEmpty()) {
        foreach(int column , boolCols)
        {
        booleanSet.append(column);
        }
        }
        }

        QVariant CheckableSortFilterProxyModel::data(const QModelIndex &index, int role) const {
        if(!index.isValid())
        return QVariant();

        if(booleanSet.contains(index.column()) && (role == Qt::CheckStateRole || role == Qt::DisplayRole)) {
            if (role == Qt::CheckStateRole)
                return index.data(Qt::EditRole).toBool() ? QVariant(Qt::Checked) : QVariant(Qt::Unchecked);
            else if (role == Qt::DisplayRole)
                return QVariant();
        }
        else
            return QSortFilterProxyModel::data(index,role);
        

        }

        bool CheckableSortFilterProxyModel::setData(const QModelIndex &index, const QVariant &value, int role) {
        if(!index.isValid())
        return false;

        if(booleanSet.contains(index.column()) && role==Qt::CheckStateRole)
        {
            QVariant data = (value.toInt()==Qt::Checked) ? QVariant(1) : QVariant (0);
            return QSortFilterProxyModel::setData(index, data, Qt::EditRole);
        }
        else
            return QSortFilterProxyModel::setData(index,value,role);
        

        }

        Qt::ItemFlags CheckableSortFilterProxyModel::flags ( const QModelIndex & index ) const {
        if(!index.isValid())
        return Qt::ItemIsEnabled;

        if(booleanSet.contains(index.column()))
            return Qt::ItemIsUserCheckable | Qt::ItemIsSelectable | Qt::ItemIsEnabled;
        else
            return QSortFilterProxyModel::flags(index);
        

        }
        @
        [/quote]

        I know its been over a year since this was las posted but I just wanted to say thank you to Harry for solving this because I have been struggeling with making a check box in a column for a while!

        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