QSortFilterProxyModel subclass for text alignment -and readonly columns

From Qt Wiki
Jump to: navigation, search

QSortFilterProxyModel subclass for text alignment and readonly columns

When using the model/view framework it is not possible to:

  1. enable or disable different columns of the table
  2. set text alignment for different columns

For this I created the following code after a thread I posted link. Here it is the code:

Header

#ifndef PROXYMODEL_HPP

  1. define PROXYMODEL_HPP
  1. include <QSortFilterProxyModel>
  2. include <QMap>

class ProxyModel : public QSortFilterProxyModel { Q_OBJECT // PRIVATE VARIABLES QMap<int,Qt::Alignment> alignMap; QMap<int,Qt::ItemFlags> flagMap;

public: // CONSTRUCTOR explicit ProxyModel(QObject *parent = 0) : QSortFilterProxyModel(parent) {}

// VIRTUAL FUNCTIONS INHERITED FROM QT SORT FILTER MODEL QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const; Qt::ItemFlags flags(const QModelIndex &index) const;

// PUBLIC FUNCTIONS void setAlignment(unsigned int, Qt::Alignment); void setAlignmentToAll(Qt::Alignment); void clearAlignment();

void setFlag(unsigned int, Qt::ItemFlags); void setFlagToAll(Qt::ItemFlags); void clearFlag();

void setEnabled(unsigned int index, bool FLAG); void setEditable(unsigned int index, bool FLAG); };

  1. endif // PROXYMODEL_HPP

Source file

#include "proxymodel.hpp"

// DATA =

QVariant ProxyModel::data(const QModelIndex &index, int role) const {

// if text alignment is asked for enters
if(role == Qt::TextAlignmentRole) {
// if the column alignment was set-up for all columns
if(alignMap.contains(–1))
return QVariant(alignMap.value(–1));
// searches if the column alignment was set-up and returns the flag
if(alignMap.contains(index.column())) {
return QVariant(alignMap.value(index.column()));
}
}
// if the column wasn't found or if alignment wasn't requested returns default QVariant
return QSortFilterProxyModel::data(index, role);

}

void ProxyModel::setAlignment(unsigned int index, Qt::Alignment flag) {

// checks if setAlignmentToAll() was called and, if so, rests alignMap
if(alignMap.contains(–1))
clearAlignment();
// inserts flag
alignMap.insert(index, flag);

}

void ProxyModel::setAlignmentToAll(Qt::Alignment flag) {

// reset QMap
clearAlignment();
// inserts flag
alignMap.insert(–1, flag);

}

void ProxyModel::clearAlignment() {

// deletes all items
alignMap.clear();

}

// FLAGS

Qt::ItemFlags ProxyModel::flags(const QModelIndex &index) const {

// if the column alignment was set-up for all columns
if(flagMap.contains(–1))
return flagMap.value(–1);
// searches if the column alignment was set-up and returns the flag
if(flagMap.contains(index.column())) {
return flagMap.value(index.column());
}
return QSortFilterProxyModel::flags(index);

}

void ProxyModel::setFlag(unsigned int index, Qt::ItemFlags flag) {

// checks if setFlagToAll() was called and, if so, rests flagMap
if(flagMap.contains(–1))
clearFlag();
// inserts flag
flagMap.insert(index, flag);

}

void ProxyModel::setFlagToAll(Qt::ItemFlags flag) {

// reset QMap
clearFlag();
// inserts flag
flagMap.insert(-1, flag);

}

void ProxyModel::clearFlag() {

// deletes all items
flagMap.clear();

}

void ProxyModel::setEnabled(unsigned int index, bool FLAG) {

if(FLAG)
setFlag(index, Qt::NoItemFlags | Qt::ItemIsSelectable | Qt::ItemIsEditable | Qt::ItemIsDragEnabled | Qt::ItemIsDropEnabled | Qt::ItemIsEnabled);
else
setFlag(index, Qt::NoItemFlags);

}

void ProxyModel::setEditable(unsigned int index, bool FLAG) {

if(FLAG)
setFlag(index, Qt::NoItemFlags | Qt::ItemIsSelectable | Qt::ItemIsEditable | Qt::ItemIsDragEnabled | Qt::ItemIsDropEnabled | Qt::ItemIsEnabled);
else
setFlag(index, Qt::NoItemFlags | Qt::ItemIsSelectable | Qt::ItemIsDragEnabled | Qt::ItemIsEnabled);

}

Example

table_model = new QSqlTableModel(this, QSqlDatabase::database(connection_name)); proxyModel = new ProxyModel(this);

// TABLE'S PROXY SET-UP proxyModel->setAlignment(0, Qt::AlignCenter); // Align center in both vertical and horizontal the first column proxyModel->setAlignment(1, Qt::AlignCenter); …

proxyModel->setEditable(0, 0); // set the column enabled and selectable but not editable proxyModel->setEnabled(1, 0); // set the column disabled …

// TABLE MODEL SET-UP table_model->setTable("account"); table_model->select(); // query execution

// TABLE VIEW SET-UP proxyModel->setSourceModel(table_model); ui->tableView->setModel(proxyModel); // the model used for the view is the proxymodel

Note that you do not need to connect the proxy with the source model before starting to configure it. It is independent. You can assign infinite (limited by hardware) column properties and apply them to any model (to be tested). You can find all the existant Qt::Alignment and Qt::ItemFlags in Qt documentation.

In my program, I am using a QSqlRelationalTableModel and when this proxymodel was applied the combo boxes were replaced with text edit boxes on the QTableView… the solution for this you can find it here.