QTableView & CheckBox delegate alignmnent
-
I can easily (I think) make a column readonly with the same logic.
If I wanted to have password fields (in the sense where the actual data is not displayed), would I need to use a delegate, or can it somehow be done with the proxy model?
-
Readonly works fine.
If I add another list (eg. readonlySet)in the flags function, all I need to add is
@
else if (readonlySet.contains(index.column()))
return Qt::ItemIsSelectable;
@For the password thing, all I've been able to do is return '***' when the columns is in the passwordSet list. When the user clicks on it, the actual text is shown.
-
One thing still not working is that the checkboxes are not aligned in the center.
How should I handle that? -
Another issue:
it doesn't work with a QSqlRelationalTableModel (I've been using a QSqlTableModel).The lookup value is displayed correctly, but it is an editable text box, instead of a combo box.
I am using
@
myTableVw->setItemDelegate(new QSqlRelationalDelegate(myTableVw));
@
Any ideas? -
I guess the checkbox can stay to the left if it will save me a delegate :)
The issue with the SqlRelationalTableModel however is much more serious, and a deal breaker if it can't be resolved. -
I can't resist the urge to complain:
It looks like another issue that will never be resolved (like other questions I've asked here and in the mailing list)...
I mean are my questions that stupid? Most things desktop related and especially SQL stuff, seem to be so underdeveloped, you'd think it was developed by a different company.
I've learnt to live with that, but the fact that noone from the Nokia developers (that read these message) bothers to acknowledge, concern or help with desktop issues, is very frustrating.I see pointless, endless discussions about the best way to draw a pixel, or the ill-fated mobile stuff, which would be all good, if such major gaps didn't exist.
Look at this simple class I've almost done here: why doesn't it exist as part of Qt? There are hundreds (if not thousands) of messages/questions of people asking how to do checkboxes in a tableView.
Each implements it differently, depending on Google's mood on the day they search for info. -
Hey hsfougaris,
a checkable cell is described in the docs with the Qt::ItemIsUserCheckable flag. This is also described in the docs and perhaps in the examples. What else should they do? They have a standard way.
And as a side note: this is a community forum, not a nokia developers forum. If spome of the Trolls hang around here, it's in their free time.
-
Well, how can you easily use it when using any QSql** models (which is a very common case one would think)? You have to subclass every time...
I also know this is a community forum, that people like you devote a lot of resources to, and manage to help many people.
What annoys me is they seem to respond to many questions about almost everything, except desktop and database related stuff.thanks,
harry -
Back to the main issue, I found this post http://lists.qt.nokia.com/pipermail/qt-interest/2009-January/001833.html which is pretty much about the same thing.
It seems to be a bug in QSqlRelationalDelegate , and a workaround for the createEditor is discussed.
But I can't seem to find the code.
In src\sql\models\qsqlrelationaldelegate.cpp there is only the following
@
/*!
\fn QWidget *QSqlRelationalDelegate::createEditor(QWidget *parent,
const QStyleOptionViewItem &option,
const QModelIndex &index) const
\reimp
*/
@
Does anyone know where the actual implementation is? -
[quote author="hsfougaris" date="1301549859"]Well, how can you easily use it when using any QSql** models (which is a very common case one would think)? You have to subclass every time...
[/quote]Where should the relational model know from if the cell should be checkable. Depending on the returned value (the tyüpe of the QVariant), the editor widget is choosen. What is a default editor for, lets say a boolean, depends whome you ask. It could be a check box, or a combo with true/false.
-
That's why the QTableView class should be much richer in my opinion (like almost every other grid developed). It could have properties like setColumnWidget(...) ...
The fact of the matter is that trying to use QSql*** with a QTableView with anything other than text is a pain, and there is no straightforward way to handle very common needs and scenarios.
You can't even center align a column without a delegate! -
[quote author="hsfougaris" date="1301550103"]Back to the main issue, I found this post http://lists.qt.nokia.com/pipermail/qt-interest/2009-January/001833.html which is pretty much about the same thing.
It seems to be a bug in QSqlRelationalDelegate , and a workaround for the createEditor is discussed.
But I can't seem to find the code.
In src\sql\models\qsqlrelationaldelegate.cpp there is only the following
@
/*!
\fn QWidget *QSqlRelationalDelegate::createEditor(QWidget *parent,
const QStyleOptionViewItem &option,
const QModelIndex &index) const
\reimp
*/
@
Does anyone know where the actual implementation is?
[/quote]It seems the code is in the header... :)
-
[quote author="hsfougaris" date="1301551606"]That's why the QTableView class should be much richer in my opinion (like almost every other grid developed). It could have properties like setColumnWidget(...) ...
The fact of the matter is that trying to use QSql*** with a QTableView with anything other than text is a pain, and there is no straightforward way to handle very common needs and scenarios.
You can't even center align a column without a delegate![/quote]If you need a richer table view, look at some of the commercial solutions that exist. I also found some things I would like to have in a TableView, but I'm sure, not 90% of the user would like to have it, perhaps on 10 %. And I think, it's the same with the SQL stuff. In our company, we use many tables, but not a single QSqlXXX class. We have custom data providers.
And I think, a QSFPM as "Man in the middle" to achieve this, is not the worst solution.
And via the delegates, you achieve exaclty the same as with setColumnWidget. Create a delegate and use setColumnDelegate. Model - View - Delegate is a very good pattern indeed. It's a derivate of the Model-View-Controler which is very common.
-
Is there anything other that one ICS makes? (because that is priced insanely...)
I had a look at one from DevMachines, which looks promising but is not ready for prime time yet.I haven't been able to find other ones, so I would appreciate any directions.
-
Also if QSFPM means QSortFilterProxyModel, I agree it's not bad as a solution (especially after what I managed to build with a little help); I just wish I could do a few more things with it...
-
Ok, I created my own subclass of QSqlRelationalDelegate and now everyhting works.
Here is the related code:
@QWidget *mySqlRelationalDelegate::createEditor(QWidget *aParent, const QStyleOptionViewItem &option, const QModelIndex &index) const {
const QSqlRelationalTableModel *sqlModel = qobject_cast<const QSqlRelationalTableModel *>(index.model()); QSqlTableModel *childModel = sqlModel ? sqlModel->relationModel(index.column()) : 0; if (!childModel ) { const QSortFilterProxyModel* proxyModel = qobject_cast<const QSortFilterProxyModel *>(index.model()); if (proxyModel) { sqlModel = qobject_cast<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<mySqlRelationalDelegate *>(this)); return combo;
}
void mySqlRelationalDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
{
QString strVal = "";
const QSqlRelationalTableModel *sqlModel = qobject_cast<const QSqlRelationalTableModel >(index.model());
if (!sqlModel )
{
const QSortFilterProxyModel proxyModel = qobject_cast<const QSortFilterProxyModel *>(index.model());
if (proxyModel) {
strVal = proxyModel->data(index).toString();
}
} else {
strVal = sqlModel->data(index).toString();
}QComboBox *combo = qobject_cast<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<QSqlRelationalTableModel *>(model); QSortFilterProxyModel* proxyModel = NULL; if (!sqlModel ) { proxyModel = qobject_cast<QSortFilterProxyModel *>(model); if (proxyModel) sqlModel = qobject_cast<QSqlRelationalTableModel *>(proxyModel->sourceModel()); } QSqlTableModel *childModel = sqlModel ? sqlModel->relationModel(index.column()) : 0; QComboBox *combo = qobject_cast<QComboBox *>(editor); if (!sqlModel || !childModel || !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); }
}
@I'll try making it a Wiki!
-
[quote author="hsfougaris" date="1301553694"]Also if QSFPM means QSortFilterProxyModel, I agree it's not bad as a solution (especially after what I managed to build with a little help); I just wish I could do a few more things with it...[/quote]
Yes, QSFPM is a (here) common abbreveation for QSortFilterProxyModel :-)
Perhaps we should add a wiki page with common abbreveation sused here :-)