Existend subclasses of QAbstractItemDelegate in the Qt-Lib
-
Hello !
I fiqure out how to work with ItemDelgates. My idea is, before I begin with my own ItemDelegates from the scratch (with Painting etc.) I subclass some existent and change only some rules. This is enough for my current task.
For example the SqlTableModel uses some automatical: Text, Comboboxes and Spinwidgets.
The problem is I cannot find out what are the names of this special ItemDelegates to subclass from it. Maybe Qt has some other usefull ItemDelegates integrated yet, which I dont know and cannot active.Andreas
-
Hi,
take a look at this piece of documentation: http://doc.qt.io/qt-5/qitemeditorfactory.html. Once you've created and registered your own editor factory you can use it for your delegate with:void QItemDelegate::setItemEditorFactory(QItemEditorFactory * factory)
. -
Ok, I understand much more from Qt now. The Itemeditorfactory is an other elegant way to manage the editor depent on the value type.
Unfortunately, this solves not my problem. I have a Bool column in the Access database, but it has is been delivered by ODBC as an unsignet int. So I get a Spin control.
I want a checkbox or a color indicator. Manage all uints same I dont like. Therfore I must set a concrete Itemdelegate for this column. It seem that I must implement my itemdelegate from scratch. -
In this case I suggest not to implement a custom table widget but to place a translator between the data provider and the table widget that casts the unsigned int fields to bool and the other way round.
-
Thank you for you answer, but the way to set the Edit/Display-Widget only by value type is to unflexible for me. I need different displays for the same type.
For example different images for diffrent bool columns I see now.I am a litttle bit confuse about QItemDelegate and QStyledItemDelegate.
Which I shall use? I need felxible painting!The Styled version has a complete different painting interface (precise none - it goes over the Application) . The most examples I found in the internet and the my book are about the unstyled version.
What is recommended?Nevertheless, nothing works. I want a simple red background.
void TBoolItDel::paint(QPainter* painter, const QStyleOptionViewItem& opt,
const QModelIndex& index) const
{
QStyleOptionViewItem so=opt;
so.backgroundBrush.setColor(Qt::red);
drawDisplay(painter, so, so.rect, OnText);
} -
I'm not an expert on this, but the documentation states that QStyledItemDelegate was added to Qt after the original QItemDelegate and that the only difference is that QStyledItemDelegate gives you access to the QStyle currently used by the application. In other words: One should use QStyledItemDelegate for better platform integration.
I've found a complete example in the official docs: http://doc.qt.io/qt-5/qtwidgets-itemviews-stardelegate-example.html.
Hope this helps!
-
The painting I can manage now.
Now I have a problem with the editor creation. I create a checkbox widget and I can see it but it overwrites not the background from the paininting. So I get a confuse text mix. updateEditorGeometry is triggered.void TBoolItemDelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex & index) const { editor->setGeometry(option.rect); }
What must I do to create a full widget in full cell-size. Must I put in in an Layout ?
If found this code
class BooleanWidget : public QWidget { Q_OBJECT QCheckBox * checkBox; public: BooleanWidget(QWidget * parent = 0) { checkBox = new QCheckBox(this); QHBoxLayout * layout = new QHBoxLayout(this); layout->addWidget(checkBox, 0, Qt::AlignCenter); } };
When I take this widget a separate window pops up out of the cell ?
-
Please show us your reimplementation of
QWidget * QStyledItemDelegate::createEditor(QWidget * parent, const QStyleOptionViewItem & option, const QModelIndex & index) const
.Btw: Have you already seen the spin box delegate example ( http://doc.qt.io/qt-5/qtwidgets-itemviews-spinboxdelegate-example.html ) ?
-
Here is my simple Editorcreator:
QWidget *TBoolItemDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem & opt , const QModelIndex &index ) const { bool on = index.model()->data(index, Qt::EditRole).toBool(); QCheckBox *c = new QCheckBox(parent); c->setChecked(on); c->setText(OnText); return c; }
An additional effect, which gives a hint on the bug maybe:
My paint function draws the text in different colors for on an off,
but when the editor opens, the text in the backgroud gets a black text color.
Therefore an additional drawing has been performed.