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

From Qt Wiki
Jump to navigation Jump to search
No edit summary
(Decode HTML entity names)
 
(5 intermediate revisions by 2 users not shown)
Line 1: Line 1:
{{Cleanup | reason=Auto-imported from ExpressionEngine.}}
[[Category:Snippets]]
[[Category:Snippets]]


= 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:
# 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 [http://developer.qt.nokia.com/forums/viewthread/13895/ link]. Here it is the code:


== Header ==
== Header ==


<code>#ifndef PROXYMODEL_HPP<br />#define PROXYMODEL_HPP
<code>#ifndef PROXYMODEL_HPP
#define PROXYMODEL_HPP


#include &lt;QSortFilterProxyModel&amp;gt;<br />#include &lt;QMap&amp;gt;
#include <QSortFilterProxyModel>
#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 {
Q_OBJECT
// PRIVATE VARIABLES
QMap<int,Qt::Alignment> alignMap;
QMap<int,Qt::ItemFlags> flagMap;


public:<br />// CONSTRUCTOR<br />explicit ProxyModel(QObject *parent = 0) : QSortFilterProxyModel(parent) {}
public:
// CONSTRUCTOR
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;
// 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<br />void setAlignment(unsigned int, Qt::Alignment);<br />void setAlignmentToAll(Qt::Alignment);<br />void clearAlignment();
// PUBLIC FUNCTIONS
void setAlignment(unsigned int, Qt::Alignment);
void setAlignmentToAll(Qt::Alignment);
void clearAlignment();


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


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


#endif // PROXYMODEL_HPP </code>
#endif // PROXYMODEL_HPP </code>
Line 27: Line 49:
== Source file ==
== Source file ==


<code>#include &quot;proxymodel.hpp&amp;quot;
<code>#include "proxymodel.hpp"


//  DATA =
//  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 />}
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) {<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::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) {<br /> // reset QMap<br /> clearAlignment();<br /> // inserts flag<br /> alignMap.insert(–1, flag);<br />}
void ProxyModel::setAlignmentToAll(Qt::Alignment flag) {
// reset QMap
clearAlignment();
// inserts flag
alignMap.insert(–1, flag);
}


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


//  FLAGS  
//  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 />}
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);
} </code>


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 />}
== Example ==
<code>table_model = new QSqlTableModel(this, QSqlDatabase::database(connection_name));
proxyModel = new ProxyModel(this);


void ProxyModel::setFlagToAll(Qt::ItemFlags flag) {<br /> // reset QMap<br /> clearFlag();<br /> // inserts flag<br /> flagMap.insert(<s>1, flag);<br />}
// TABLE'S PROXY SET-UP
<br />void ProxyModel::clearFlag() {<br /> // deletes all items<br /> flagMap.clear();<br />}
proxyModel->setAlignment(0, Qt::AlignCenter); // Align center in both vertical and horizontal the first column
<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 />}
proxyModel->setAlignment(1, Qt::AlignCenter);
<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 />} </code>
<br />h2. Example
<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 />


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
proxyModel->setEnabled(1, 0); // set the column disabled


// TABLE MODEL SET-UP<br />table_model-&gt;setTable(&quot;account&amp;quot;);<br />table_model-&gt;select(); // query execution
// TABLE MODEL SET-UP
table_model->setTable("account");
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
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 &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 [http://developer.qt.nokia.com/wiki/QSqlRelationalDelegate_subclass_that_works_with_QSqlRelationalTableModel here].

Latest revision as of 17:33, 12 March 2015

This article may require cleanup to meet the Qt Wiki's quality standards. Reason: Auto-imported from ExpressionEngine.
Please improve this article if you can. Remove the {{cleanup}} tag and add this page to Updated pages list after it's clean.

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
#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);
}

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.