QSortFilterProxyModel subclass for text alignment -and readonly columns: Difference between revisions
No edit summary |
No edit summary |
||
Line 3: | Line 3: | ||
= QSortFilterProxyModel subclass for text alignment and readonly columns = | = QSortFilterProxyModel subclass for text alignment and readonly columns = | ||
When using the model/view framework it is not possible to: | When using the model/view framework it is not possible to: | ||
# enable or disable different columns of the table | |||
# set text alignment for different columns | |||
For this I created the following code after a thread I posted "link":http://developer.qt.nokia.com/forums/viewthread/13895/. Here it is the code: | |||
== Header == | == Header == | ||
<code>#ifndef PROXYMODEL_HPP | <code>#ifndef PROXYMODEL_HPP | ||
#define PROXYMODEL_HPP | |||
#include <QSortFilterProxyModel> | #include <QSortFilterProxyModel> | ||
#include <QMap> | |||
class ProxyModel : public QSortFilterProxyModel { | class ProxyModel : public QSortFilterProxyModel { | ||
Q_OBJECT | |||
// PRIVATE VARIABLES | |||
QMap<int,Qt::Alignment> alignMap; | |||
QMap<int,Qt::ItemFlags> flagMap; | |||
public: | public: | ||
// CONSTRUCTOR | |||
explicit ProxyModel(QObject *parent = 0) : QSortFilterProxyModel(parent) {} | |||
// VIRTUAL FUNCTIONS INHERITED FROM QT SORT FILTER MODEL | // 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 | // PUBLIC FUNCTIONS | ||
void setAlignment(unsigned int, Qt::Alignment); | |||
void setAlignmentToAll(Qt::Alignment); | |||
void clearAlignment(); | |||
void setFlag(unsigned int, Qt::ItemFlags); | void setFlag(unsigned int, Qt::ItemFlags); | ||
void setFlagToAll(Qt::ItemFlags); | |||
void clearFlag(); | |||
void setEnabled(unsigned int index, bool FLAG); | void setEnabled(unsigned int index, bool FLAG); | ||
void setEditable(unsigned int index, bool FLAG); | |||
}; | |||
#endif // PROXYMODEL_HPP </code> | #endif // PROXYMODEL_HPP </code> | ||
Line 31: | Line 51: | ||
// DATA = | // DATA = | ||
QVariant ProxyModel::data(const QModelIndex &index, int role) const { | 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) { | 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) { | void ProxyModel::setAlignmentToAll(Qt::Alignment flag) { | ||
// reset QMap | |||
clearAlignment(); | |||
// inserts flag | |||
alignMap.insert(–1, flag); | |||
} | |||
void ProxyModel::clearAlignment() { | void ProxyModel::clearAlignment() { | ||
// deletes all items | |||
alignMap.clear(); | |||
} | |||
// FLAGS | // FLAGS | ||
Qt::ItemFlags ProxyModel::flags(const QModelIndex &index) const { | 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) { | 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) { | 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); | |||
} | |||
// TABLE | 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); | |||
} </code> | |||
h2. Example | |||
<code>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 </code> | |||
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. | 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":http://developer.qt.nokia.com/wiki/QSqlRelationalDelegate_subclass_that_works_with_QSqlRelationalTableModel. | 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":http://developer.qt.nokia.com/wiki/QSqlRelationalDelegate_subclass_that_works_with_QSqlRelationalTableModel. |
Revision as of 11:36, 25 February 2015
QSortFilterProxyModel subclass for text alignment and readonly columns
When using the model/view framework it is not possible to:
- enable or disable different columns of the table
- set text alignment for different columns
For this I created the following code after a thread I posted "link":http://developer.qt.nokia.com/forums/viewthread/13895/. Here it is the code:
Header
#ifndef PROXYMODEL_HPP
#define PROXYMODEL_HPP
#include <QSortFilterProxyModel>
#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);
};
#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);
}
h2. 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":http://developer.qt.nokia.com/wiki/QSqlRelationalDelegate_subclass_that_works_with_QSqlRelationalTableModel.