What is the difference between QStyledItem and QItemDelegate?
-
Qt's item views use a delegate to implement the look of their items. A delegate is an implementation of QAbstractItemDelegate interface.
QItemDelegate and QStyledItemDelegate are just different implementations of QAbstractItemDelegate interface.QItemDelegate is the older one and no longer used by Qt's view classes, but it's there if you want it for some reason, e.g. compatibility with some older code. Most of the time you shouldn't use it in new code.
QStyledItemDelegate is the newer one and uses current app style. A Qt GUI application uses an instance of a QStyle derived class to implement the look and feel. An application has a global style applied to it. By default it uses a style provided by a platform specific style plugin. You can replace it globally via QApplication::setStyle() or on individual widgets (although that's not common). Supported platforms have their own native style plugins, see QStyle, and there's a platform agnostic "fusion" style, that provides a common look and feel across platforms. You can also modify these styles via proxy style or implement your own if you want to (although it's a lot of work).
So the "current style" is the style set on the application object. If you haven't changed it yourself it's the default platform native looking style provided by a plugin. If you don't deploy style plugin with your app a very basic default fallback style is used.
-
thanks, and that's my guess :
I have a class that deriverd from QStyledItemDelegate,class MyDelegate : public QStyledItemDelegate
and i paint a progress bar,
void MyDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const { if (index.column() == 1) { int progress = index.data().toInt(); QStyleOptionProgressBar progressBarOption; progressBarOption.rect = option.rect; progressBarOption.minimum = 0; progressBarOption.maximum = 100; progressBarOption.progress = progress; progressBarOption.text = QString::number(progress) + "%"; progressBarOption.textVisible = true; QApplication::style()->drawControl(QStyle::CE_ProgressBar, &progressBarOption, painter); } else QStyledItemDelegate::paint(painter, option, index); }
I set the application's style,
QApplication a(argc, argv); QStyle* style = QStyleFactory::create("Windows"); a.setStyle(style);
Then, i change the styleQApplication a(argc, argv); QStyle* style = QStyleFactory::create("windowsvista"); a.setStyle(style);
the progress bar style also changed,
but,when i change theMyDelegate
base class and the application style, progress bar also changed.class MyDelegate : public QItemDelegate
I don't know if my guess is correct, can you show me how to show this difference on the interface?
-
You're bypassing what those classes do with this:
QApplication::style()->drawControl(...
You're just using the current style no matter which class is the base. You won't see a difference like this.
QStyledItemDelegate uses QStyle directly to draw the controls, but it doesn't mean QItemDelegate can't change its looks based on the current style. Some controls might look similar or exactly the same. I don't know the implementation and where it differs by heart, so I can't give you an example of where it diverges.
If you're interested you can just compare the source of QItemDelegate and QStyledItemDelegate and see what's different.
Btw. why do you need to see the difference? Why not just use QStyledItemDelegate as recommended by the docs?
-
Thanks;
I already know what you mean ;
I'm just interested in this, and i read many blogs that introduce the difference between QStyledItemDelegate and QItemDelegate, it just write "use current style". The most important reason is that I have been relatively idle recently,hahaha.😂