QSqlRelationalDelegate-subclass-for-QSqlRelationalTableModel: Difference between revisions

From Qt Wiki
Jump to navigation Jump to search
No edit summary
 
No edit summary
Line 1: Line 1:
=QSqlRelationalDelegate subclass that works with QSqlRelationalTableModel=
[[Category:Snippets]]
 
= QSqlRelationalDelegate subclass that works with QSqlRelationalTableModel =


When you are using a QSqlRelationalTableModel in combination with QSortFilterProxyModel, you lose the automatic combobox that is displayed in a QTableView.
When you are using a QSqlRelationalTableModel in combination with QSortFilterProxyModel, you lose the automatic combobox that is displayed in a QTableView.
Line 5: Line 7:
This is a subclass of QSqlRelationalDelegate, that works:
This is a subclass of QSqlRelationalDelegate, that works:


<span class="caps">COMMENT</span>: To use it with another proxy model (my own QXTreeProxyModel that converts a table to a tree that can be displayed by QTreeView), I replaced QSortFilterProxyModel by QAbstractProxyModel throughout the code in mysqlrelationaldelegate.cpp; no other changes were necessary. <span class="caps">BTW</span>, QXTreeProxyModel is available on https://github.com/Al-/QXTreeProxyModel
COMMENT: To use it with another proxy model (my own QXTreeProxyModel that converts a table to a tree that can be displayed by QTreeView), I replaced QSortFilterProxyModel by QAbstractProxyModel throughout the code in mysqlrelationaldelegate.cpp; no other changes were necessary. BTW, QXTreeProxyModel is available on https://github.com/Al-/QXTreeProxyModel
 
== Header: ==
 
<code><br />#ifndef MYSQLRELATIONALDELEGATE_H<br />#define MYSQLRELATIONALDELEGATE_H
 
#include &lt;QSqlRelationalDelegate&amp;gt;
 
class mySqlRelationalDelegate : public QSqlRelationalDelegate<br />{<br /> Q_OBJECT<br />public:<br /> explicit mySqlRelationalDelegate(QObject *parent = 0);
 
QWidget *createEditor(QWidget *aParent, const QStyleOptionViewItem &amp;option, const QModelIndex &amp;index) const;<br /> void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &amp;index) const;<br /> void setEditorData(QWidget *editor, const QModelIndex &amp;index) const;
 
signals:
 
public slots:
 
};
 
#endif // MYSQLRELATIONALDELEGATE_H<br /></code>
 
== Source: ==
 
<code><br />#include &quot;mysqlrelationaldelegate.h&amp;quot;<br />#include &lt;QSqlRelationalTableModel&amp;gt;<br />#include &lt;QSortFilterProxyModel&amp;gt;
 
#include &lt;QDebug&amp;gt;<br />#include &lt;QSqlRecord&amp;gt;
 
mySqlRelationalDelegate::mySqlRelationalDelegate(QObject *parent) :<br /> QSqlRelationalDelegate (parent)<br />{<br />}
 
QWidget *mySqlRelationalDelegate::createEditor(QWidget *aParent, const QStyleOptionViewItem &amp;option, const QModelIndex &amp;index) const {
 
const QSqlRelationalTableModel '''sqlModel = qobject_cast&amp;lt;const QSqlRelationalTableModel'''&gt;(index.model());<br /> QSqlTableModel '''childModel = sqlModel ? sqlModel-&gt;relationModel(index.column()) : 0;
<br /> if (!childModel )<br /> {<br /> const QSortFilterProxyModel''' proxyModel = qobject_cast&amp;lt;const QSortFilterProxyModel '''&gt;(index.model());<br /> if (proxyModel)<br /> {<br /> sqlModel = qobject_cast&amp;lt;const QSqlRelationalTableModel'''&gt;(proxyModel-&gt;sourceModel());<br /> childModel = sqlModel ? sqlModel-&gt;relationModel(index.column()) : 0;<br /> }<br /> }
 
if (!childModel)<br /> {<br /> return QItemDelegate::createEditor(aParent, option, index);<br /> }
 
QComboBox '''combo = new QComboBox(aParent);<br /> combo-&gt;setModel(childModel);<br /> combo-&gt;setModelColumn(childModel-&gt;fieldIndex(sqlModel-&gt;relation(index.column()).displayColumn()));<br /> combo-&gt;installEventFilter(const_cast&amp;lt;mySqlRelationalDelegate'''&gt;(this));
 
return combo;
 
}
 
void mySqlRelationalDelegate::setEditorData(QWidget *editor, const QModelIndex &amp;index) const<br />{<br /> QString strVal = &quot;&quot;;<br /> const QSqlRelationalTableModel '''sqlModel = qobject_cast&amp;lt;const QSqlRelationalTableModel'''&gt;(index.model());<br /> if (!sqlModel )<br /> {<br /> const QSortFilterProxyModel* proxyModel = qobject_cast&amp;lt;const QSortFilterProxyModel *&gt;(index.model());<br /> if (proxyModel) {<br /> strVal = proxyModel-&gt;data(index).toString();<br /> }<br /> } else {<br /> strVal = sqlModel-&gt;data(index).toString();<br /> }
 
QComboBox '''combo = qobject_cast&amp;lt;QComboBox'''&gt;(editor);<br /> if (strVal.isEmpty() || !combo) {<br /> QItemDelegate::setEditorData(editor, index);<br /> return;<br /> }
 
combo-&gt;setCurrentIndex(combo-&gt;findText(strVal));<br />}
 
void mySqlRelationalDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &amp;index) const<br />{<br /> if (!index.isValid())<br /> return;


==Header:==
QSqlRelationalTableModel '''sqlModel = qobject_cast&amp;lt;QSqlRelationalTableModel'''&gt;(model);<br /> QSortFilterProxyModel* proxyModel = NULL;<br /> if (!sqlModel )<br /> {<br /> proxyModel = qobject_cast&amp;lt;QSortFilterProxyModel '''&gt;(model);<br /> if (proxyModel)<br /> sqlModel = qobject_cast&amp;lt;QSqlRelationalTableModel'''&gt;(proxyModel-&gt;sourceModel());<br /> }


==Source:==
QSqlTableModel *childModel = sqlModel ? sqlModel-&gt;relationModel(index.column()) : 0;<br /> QComboBox '''combo = qobject_cast&amp;lt;QComboBox'''&gt;(editor);<br /> if ([[Image:sqlModel || |]]childModel || !combo) {<br /> QItemDelegate::setModelData(editor, model, index);<br /> return;<br /> }


===Categories:===
int currentItem = combo-&gt;currentIndex();<br /> int childColIndex = childModel-&gt;fieldIndex(sqlModel-&gt;relation(index.column()).displayColumn());<br /> int childEditIndex = childModel-&gt;fieldIndex(sqlModel-&gt;relation(index.column()).indexColumn());


* [[:Category:snippets|snippets]]
if (proxyModel) {<br /> proxyModel-&gt;setData(index, childModel-&gt;data(childModel-&gt;index(currentItem, childColIndex), Qt::DisplayRole), Qt::DisplayRole);<br /> proxyModel-&gt;setData(index, childModel-&gt;data(childModel-&gt;index(currentItem, childEditIndex), Qt::EditRole), Qt::EditRole);<br /> } else {<br /> sqlModel-&gt;setData(index, childModel-&gt;data(childModel-&gt;index(currentItem, childColIndex), Qt::DisplayRole), Qt::DisplayRole);<br /> sqlModel-&gt;setData(index, childModel-&gt;data(childModel-&gt;index(currentItem, childEditIndex), Qt::EditRole), Qt::EditRole);<br /> }<br />}

Revision as of 10:37, 24 February 2015


QSqlRelationalDelegate subclass that works with QSqlRelationalTableModel

When you are using a QSqlRelationalTableModel in combination with QSortFilterProxyModel, you lose the automatic combobox that is displayed in a QTableView.

This is a subclass of QSqlRelationalDelegate, that works:

COMMENT: To use it with another proxy model (my own QXTreeProxyModel that converts a table to a tree that can be displayed by QTreeView), I replaced QSortFilterProxyModel by QAbstractProxyModel throughout the code in mysqlrelationaldelegate.cpp; no other changes were necessary. BTW, QXTreeProxyModel is available on https://github.com/Al-/QXTreeProxyModel

Header:

<br />#ifndef MYSQLRELATIONALDELEGATE_H<br />#define MYSQLRELATIONALDELEGATE_H

#include &lt;QSqlRelationalDelegate&amp;gt;

class mySqlRelationalDelegate : public QSqlRelationalDelegate<br />{<br /> Q_OBJECT<br />public:<br /> explicit mySqlRelationalDelegate(QObject *parent = 0);

QWidget *createEditor(QWidget *aParent, const QStyleOptionViewItem &amp;option, const QModelIndex &amp;index) const;<br /> void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &amp;index) const;<br /> void setEditorData(QWidget *editor, const QModelIndex &amp;index) const;

signals:

public slots:

};

#endif // MYSQLRELATIONALDELEGATE_H<br />

Source:


#include "mysqlrelationaldelegate.h&quot;
#include <QSqlRelationalTableModel&gt;
#include <QSortFilterProxyModel&gt;

  1. include <QDebug&gt;
    #include <QSqlRecord&gt;

mySqlRelationalDelegate::mySqlRelationalDelegate(QObject *parent) :
QSqlRelationalDelegate (parent)
{
}

QWidget *mySqlRelationalDelegate::createEditor(QWidget *aParent, const QStyleOptionViewItem &option, const QModelIndex &index) const {

const QSqlRelationalTableModel sqlModel = qobject_cast&lt;const QSqlRelationalTableModel>(index.model());
QSqlTableModel childModel = sqlModel ? sqlModel->relationModel(index.column()) : 0;
if (!childModel )
{
const QSortFilterProxyModel proxyModel = qobject_cast&lt;const QSortFilterProxyModel >(index.model());
if (proxyModel)
{
sqlModel = qobject_cast&lt;const QSqlRelationalTableModel>(proxyModel->sourceModel());
childModel = sqlModel ? sqlModel->relationModel(index.column()) : 0;
}
}

if (!childModel)
{
return QItemDelegate::createEditor(aParent, option, index);
}

QComboBox combo = new QComboBox(aParent);
combo->setModel(childModel);
combo->setModelColumn(childModel->fieldIndex(sqlModel->relation(index.column()).displayColumn()));
combo->installEventFilter(const_cast&lt;mySqlRelationalDelegate
>(this));

return combo;

}

void mySqlRelationalDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
{
QString strVal = "";
const QSqlRelationalTableModel sqlModel = qobject_cast&lt;const QSqlRelationalTableModel>(index.model());
if (!sqlModel )
{
const QSortFilterProxyModel* proxyModel = qobject_cast&lt;const QSortFilterProxyModel *>(index.model());
if (proxyModel) {
strVal = proxyModel->data(index).toString();
}
} else {
strVal = sqlModel->data(index).toString();
}

QComboBox combo = qobject_cast&lt;QComboBox>(editor);
if (strVal.isEmpty() || !combo) {
QItemDelegate::setEditorData(editor, index);
return;
}

combo->setCurrentIndex(combo->findText(strVal));
}

void mySqlRelationalDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
{
if (!index.isValid())
return;

QSqlRelationalTableModel sqlModel = qobject_cast&lt;QSqlRelationalTableModel>(model);
QSortFilterProxyModel* proxyModel = NULL;
if (!sqlModel )
{
proxyModel = qobject_cast&lt;QSortFilterProxyModel >(model);
if (proxyModel)
sqlModel = qobject_cast&lt;QSqlRelationalTableModel
>(proxyModel->sourceModel());
}

QSqlTableModel *childModel = sqlModel ? sqlModel->relationModel(index.column()) : 0;
QComboBox combo = qobject_cast&lt;QComboBox>(editor);
if (File:SqlModelchildModel || !combo) {
QItemDelegate::setModelData(editor, model, index);
return;
}

int currentItem = combo->currentIndex();
int childColIndex = childModel->fieldIndex(sqlModel->relation(index.column()).displayColumn());
int childEditIndex = childModel->fieldIndex(sqlModel->relation(index.column()).indexColumn());

if (proxyModel) {
proxyModel->setData(index, childModel->data(childModel->index(currentItem, childColIndex), Qt::DisplayRole), Qt::DisplayRole);
proxyModel->setData(index, childModel->data(childModel->index(currentItem, childEditIndex), Qt::EditRole), Qt::EditRole);
} else {
sqlModel->setData(index, childModel->data(childModel->index(currentItem, childColIndex), Qt::DisplayRole), Qt::DisplayRole);
sqlModel->setData(index, childModel->data(childModel->index(currentItem, childEditIndex), Qt::EditRole), Qt::EditRole);
}
}