Difference between revisions of "QSortFilterProxyModel subclass for text alignment -and readonly columns"

From Qt Wiki
Jump to: navigation, search
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:<br /># enable or disable different columns of the table<br /># set text alignment for different columns<br />For this I created the following code after a thread I posted &quot;link&amp;quot;:http://developer.qt.nokia.com/forums/viewthread/13895/. Here it is the code:
+
When using the model/view framework it is not possible to:<br /># enable or disable different columns of the table<br /># set text alignment for different columns<br />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 ==
Line 9: Line 9:
 
<code>#ifndef PROXYMODEL_HPP<br />#define PROXYMODEL_HPP
 
<code>#ifndef PROXYMODEL_HPP<br />#define PROXYMODEL_HPP
  
#include &lt;QSortFilterProxyModel&amp;gt;<br />#include &lt;QMap&amp;gt;
+
#include <QSortFilterProxyModel><br />#include <QMap>
  
class ProxyModel : public QSortFilterProxyModel {<br />Q_OBJECT<br />// PRIVATE VARIABLES<br />QMap&amp;lt;int,Qt::Alignment&amp;gt; alignMap;<br />QMap&amp;lt;int,Qt::ItemFlags&amp;gt; flagMap;
+
class ProxyModel : public QSortFilterProxyModel {<br />Q_OBJECT<br />// PRIVATE VARIABLES<br />QMap<int,Qt::Alignment> alignMap;<br />QMap<int,Qt::ItemFlags> flagMap;
  
 
public:<br />// CONSTRUCTOR<br />explicit ProxyModel(QObject *parent = 0) : QSortFilterProxyModel(parent) {}
 
public:<br />// CONSTRUCTOR<br />explicit ProxyModel(QObject *parent = 0) : QSortFilterProxyModel(parent) {}
Line 27: Line 27:
 
== Source file ==
 
== Source file ==
  
<code>#include &quot;proxymodel.hpp&amp;quot;
+
<code>#include "proxymodel.hpp"
  
 
//  DATA =
 
//  DATA =
Line 45: Line 45:
 
void ProxyModel::setFlag(unsigned int index, Qt::ItemFlags flag) {<br /> // checks if setFlagToAll() was called and, if so, rests flagMap<br /> if(flagMap.contains(–1))<br /> clearFlag();<br /> // inserts flag<br /> flagMap.insert(index, flag);<br />}
 
void ProxyModel::setFlag(unsigned int index, Qt::ItemFlags flag) {<br /> // checks if setFlagToAll() was called and, if so, rests flagMap<br /> if(flagMap.contains(–1))<br /> clearFlag();<br /> // inserts flag<br /> flagMap.insert(index, flag);<br />}
  
void ProxyModel::setFlagToAll(Qt::ItemFlags flag) {<br /> // reset QMap<br /> clearFlag();<br /> // inserts flag<br /> flagMap.insert(<s>1, flag);<br />}
+
void ProxyModel::setFlagToAll(Qt::ItemFlags flag) {<br /> // reset QMap<br /> clearFlag();<br /> // inserts flag<br /> flagMap.insert(-1, flag);<br />}
 
<br />void ProxyModel::clearFlag() {<br /> // deletes all items<br /> flagMap.clear();<br />}
 
<br />void ProxyModel::clearFlag() {<br /> // deletes all items<br /> flagMap.clear();<br />}
 
<br />void ProxyModel::setEnabled(unsigned int index, bool FLAG) {<br /> if(FLAG)<br /> setFlag(index, Qt::NoItemFlags | Qt::ItemIsSelectable | Qt::ItemIsEditable | Qt::ItemIsDragEnabled | Qt::ItemIsDropEnabled | Qt::ItemIsEnabled);<br /> else<br /> setFlag(index, Qt::NoItemFlags);<br />}
 
<br />void ProxyModel::setEnabled(unsigned int index, bool FLAG) {<br /> if(FLAG)<br /> setFlag(index, Qt::NoItemFlags | Qt::ItemIsSelectable | Qt::ItemIsEditable | Qt::ItemIsDragEnabled | Qt::ItemIsDropEnabled | Qt::ItemIsEnabled);<br /> else<br /> setFlag(index, Qt::NoItemFlags);<br />}
Line 51: Line 51:
 
<br />h2. Example
 
<br />h2. Example
 
<br /><code>table_model = new QSqlTableModel(this, QSqlDatabase::database(connection_name));<br />proxyModel = new ProxyModel(this);
 
<br /><code>table_model = new QSqlTableModel(this, QSqlDatabase::database(connection_name));<br />proxyModel = new ProxyModel(this);
<br />// TABLE'S PROXY SET-UP<br />proxyModel</s>&gt;setAlignment(0, Qt::AlignCenter); // Align center in both vertical and horizontal the first column<br />proxyModel-&gt;setAlignment(1, Qt::AlignCenter);<br />…
+
<br />// TABLE'S PROXY SET-UP<br />proxyModel->setAlignment(0, Qt::AlignCenter); // Align center in both vertical and horizontal the first column<br />proxyModel->setAlignment(1, Qt::AlignCenter);<br />…
  
proxyModel-&gt;setEditable(0, 0); // set the column enabled and selectable but not editable<br />proxyModel-&gt;setEnabled(1, 0); // set the column disabled<br />…
+
proxyModel->setEditable(0, 0); // set the column enabled and selectable but not editable<br />proxyModel->setEnabled(1, 0); // set the column disabled<br />…
  
// TABLE MODEL SET-UP<br />table_model-&gt;setTable(&quot;account&amp;quot;);<br />table_model-&gt;select(); // query execution
+
// TABLE MODEL SET-UP<br />table_model->setTable("account");<br />table_model->select(); // query execution
  
// TABLE VIEW SET-UP<br />proxyModel-&gt;setSourceModel(table_model);<br />ui-&gt;tableView-&gt;setModel(proxyModel); // the model used for the view is the proxymodel </code>
+
// TABLE VIEW SET-UP<br />proxyModel->setSourceModel(table_model);<br />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 &quot;here&amp;quot;: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 14:32, 24 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<br />#define PROXYMODEL_HPP

#include <QSortFilterProxyModel><br />#include <QMap>

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

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

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

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

void setFlag(unsigned int, Qt::ItemFlags);<br />void setFlagToAll(Qt::ItemFlags);<br />void clearFlag();

void setEnabled(unsigned int index, bool FLAG);<br />void setEditable(unsigned int index, bool FLAG);<br />};

#endif // PROXYMODEL_HPP

Source file

#include "proxymodel.hpp"

//  DATA =

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

void ProxyModel::setAlignment(unsigned int index, Qt::Alignment flag) {<br /> // checks if setAlignmentToAll() was called and, if so, rests alignMap<br /> if(alignMap.contains(–1))<br /> clearAlignment();<br /> // inserts flag<br /> alignMap.insert(index, flag);<br />}

void ProxyModel::setAlignmentToAll(Qt::Alignment flag) {<br /> // reset QMap<br /> clearAlignment();<br /> // inserts flag<br /> alignMap.insert(–1, flag);<br />}

void ProxyModel::clearAlignment() {<br /> // deletes all items<br /> alignMap.clear();<br />}

//  FLAGS 

Qt::ItemFlags ProxyModel::flags(const QModelIndex &amp;index) const {<br /> // if the column alignment was set-up for all columns<br /> if(flagMap.contains(–1))<br /> return flagMap.value(–1);<br /> // searches if the column alignment was set-up and returns the flag<br /> if(flagMap.contains(index.column())) {<br /> return flagMap.value(index.column());<br /> }<br /> return QSortFilterProxyModel::flags(index);<br />}

void ProxyModel::setFlag(unsigned int index, Qt::ItemFlags flag) {<br /> // checks if setFlagToAll() was called and, if so, rests flagMap<br /> if(flagMap.contains(–1))<br /> clearFlag();<br /> // inserts flag<br /> flagMap.insert(index, flag);<br />}

void ProxyModel::setFlagToAll(Qt::ItemFlags flag) {<br /> // reset QMap<br /> clearFlag();<br /> // inserts flag<br /> flagMap.insert(-1, flag);<br />}
<br />void ProxyModel::clearFlag() {<br /> // deletes all items<br /> flagMap.clear();<br />}
<br />void ProxyModel::setEnabled(unsigned int index, bool FLAG) {<br /> if(FLAG)<br /> setFlag(index, Qt::NoItemFlags | Qt::ItemIsSelectable | Qt::ItemIsEditable | Qt::ItemIsDragEnabled | Qt::ItemIsDropEnabled | Qt::ItemIsEnabled);<br /> else<br /> setFlag(index, Qt::NoItemFlags);<br />}
<br />void ProxyModel::setEditable(unsigned int index, bool FLAG) {<br /> if(FLAG)<br /> setFlag(index, Qt::NoItemFlags | Qt::ItemIsSelectable | Qt::ItemIsEditable | Qt::ItemIsDragEnabled | Qt::ItemIsDropEnabled | Qt::ItemIsEnabled);<br /> else<br /> setFlag(index, Qt::NoItemFlags | Qt::ItemIsSelectable | Qt::ItemIsDragEnabled | Qt::ItemIsEnabled);<br />}


h2. Example


table_model = new QSqlTableModel(this, QSqlDatabase::database(connection_name));<br />proxyModel = new ProxyModel(this);
<br />// TABLE'S PROXY SET-UP<br />proxyModel->setAlignment(0, Qt::AlignCenter); // Align center in both vertical and horizontal the first column<br />proxyModel->setAlignment(1, Qt::AlignCenter);<br />…

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

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

// TABLE VIEW SET-UP<br />proxyModel->setSourceModel(table_model);<br />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.