Memory leak using QListView - qt

I'm working on an applications launcher and decided to use a QListView to display the Items. I wrote a Model and an ItemDelegate but it seems to be wasting memory somewhere I can't find.
The issue is that even scrolling on the app list makes my program to consume more and more memory.
I think it is related to the way I paint but I cant see where the mistake is.
Paint method on the QStyledItemDelegate
void DashViewItemDelegate::paint(QPainter * painter, const QStyleOptionViewItem & option, const QModelIndex & index) const {
QString name = index.model()->data(index, Qt::DisplayRole).toString();
QIcon icon = index.model()->data(index, Qt::DecorationRole).value<QIcon>();
QRect iconRect = QRect(option.rect.x() + 10, option.rect.y(), option.rect.width() - 20, option.rect.height() - 43);
QRect textRect = QRect(option.rect.x(), option.rect.y() + 60, option.rect.width(), option.rect.height() - 60);
if (option.state & QStyle::State_Selected)
painter->fillRect(option.rect, option.palette.highlight());
icon.paint(painter, iconRect, Qt::AlignCenter | Qt::AlignTop);
QTextLayout textLayout(name);
textLayout.setFont(option.font);
int widthUsed = 0;
int lineCount = 0;
textLayout.beginLayout();
while (++lineCount < 3) {
QTextLine line = textLayout.createLine();
if (!line.isValid())
break;
line.setLineWidth(option.rect.width());
widthUsed += line.naturalTextWidth();
}
textLayout.endLayout();
widthUsed += option.rect.width();
QString newText = painter->fontMetrics().elidedText(name, Qt::ElideRight, widthUsed);
painter->drawText(textRect, Qt::AlignCenter | Qt::AlignTop | Qt::TextWordWrap , newText);
painter->save();
}
help will be really appreciated

Related

How to add curve path to connect flow widgets from two columns in qt?

I know this question is somehow hard to describe, so a picture may help, as following:
I'm developing application in qt-5.15.x. Say I have a window or widget or something else which can layout two columns of widgets(customized, call it rectangle here), I want to:
drag and drop one item up or down within same column by mouse
connect two widgets from two different columns with a customized curve path by mouse
all elements(rectangle and curve item) have mouse events: move, hover, click and key event
the contents of window or widget can scroll vertically due to more and more items will be added
Before this, I have implemented bazier curve, graphicsview with customized widgets and connecting these widgets with bazier curve, but all these widgets don't lay on line(horizontally or vertically).
So my question is not about how to implement it in detail, but just a guide - what widget , layout or event something else in qt I can use, or which document I can refer to. I have searched a lot, but with no results.
OPs requirement can be achieved by simply deriving a widget from QWidget and overriding the paintEvent().
Beside of that, the derived widget still can contain children and layout managers, so that both can be combined.
Sample code testQWidgetPaintOverChildren.cc to demonstrate:
#include <QtWidgets>
class Canvas: public QWidget {
private:
using Link = std::pair<int, int>;
std::vector<Link> _links;
public:
void addLink(int from, int to)
{
_links.emplace_back(from, to);
update();
}
protected:
virtual void paintEvent(QPaintEvent *pQEvent) override;
};
void Canvas::paintEvent(QPaintEvent* pQEvent)
{
QWidget::paintEvent(pQEvent);
const int lenTan = 64;
QPainter qPainter(this);
auto drawCurve = [&](const QPoint& qPosFrom, const QPoint& qPosTo) {
QPainterPath qPath;
qPath.moveTo(qPosFrom);
qPath.cubicTo(qPosFrom + QPoint(lenTan, 0), qPosTo - QPoint(lenTan, 0), qPosTo);
qPainter.drawPath(qPath);
};
const QObjectList& pQChildren = children();
for (const Link& link : _links) {
const int from = link.first;
if (from >= pQChildren.size()) continue; // invalid from index
const QWidget* pQWidgetFrom
= dynamic_cast<const QWidget*>(pQChildren[from]);
if (!pQWidgetFrom) continue; // shouldn't happen
const int to = link.second;
if (to >= pQChildren.size()) continue; // invalid to index
const QWidget* pQWidgetTo
= dynamic_cast<const QWidget*>(pQChildren[to]);
if (!pQWidgetTo) continue; // shouldn't happen
const QPoint qPosFrom
= pQWidgetFrom->pos() + QPoint(3 * pQWidgetFrom->width() / 4, pQWidgetFrom->height() / 2);
const QPoint qPosTo
= pQWidgetTo->pos() + QPoint(1 * pQWidgetTo->width() / 4, pQWidgetTo->height() / 2);
//qPainter.drawLine(qPosFrom, qPosTo);
drawCurve(qPosFrom, qPosTo);
}
}
int main(int argc, char** argv)
{
qDebug() << "Qt Version:" << QT_VERSION_STR;
QApplication app(argc, argv);
// setup GUI
Canvas qCanvas;
qCanvas.setWindowTitle("Test Draw Over Children");
qCanvas.resize(320, 240);
QGridLayout qGrid;
qGrid.setSpacing(16);
#define ITEM(ROW, COL) \
QLabel qLbl##ROW##COL("txt "#ROW", "#COL); \
qLbl##ROW##COL.setAlignment(Qt::AlignCenter); \
qLbl##ROW##COL.setFrameStyle(QFrame::Box | QFrame::Plain); \
qGrid.addWidget(&qLbl##ROW##COL, ROW - 1, COL - 1)
ITEM(1, 1); ITEM(1, 2);
ITEM(2, 1); ITEM(2, 2);
ITEM(3, 1); ITEM(3, 2);
ITEM(4, 1); ITEM(4, 2);
qCanvas.setLayout(&qGrid);
qCanvas.addLink(1, 8);
qCanvas.addLink(5, 2);
qCanvas.show();
// runtime loop
return app.exec();
}
Output:
Qt Version: 5.15.1
The drawback of this first attempt is that the links are drawn under the children. In the case of the QLabel, this isn't visible as QLabels are mostly transparent. Replacing them by QPushButtons makes this obvious.
This can be solved by using two widgets, one as container for children, the other for drawing the links, and ensuring that the second widget has the same position and size than the first.
Improved sample code testQWidgetPaintOverChildren.cc:
#include <QtWidgets>
class Widget: public QWidget {
private:
using Link = std::pair<int, int>;
struct Canvas: public QWidget {
std::vector<Link> links;
using QWidget::QWidget;
virtual void paintEvent(QPaintEvent *pQEvent) override;
} _canvas;
public:
explicit Widget(QWidget* pQParent = nullptr):
QWidget(pQParent),
_canvas(this)
{ }
public:
void addLink(int from, int to)
{
_canvas.links.emplace_back(from, to);
_canvas.update();
_canvas.setParent(nullptr);
_canvas.setParent(this);
}
protected:
virtual void resizeEvent(QResizeEvent* pQEvent) override
{
QWidget::resizeEvent(pQEvent);
_canvas.resize(size());
}
};
void Widget::Canvas::paintEvent(QPaintEvent* pQEvent)
{
QWidget::paintEvent(pQEvent);
const int lenTan = 64;
QPainter qPainter(this);
auto drawCurve = [&](const QPoint& qPosFrom, const QPoint& qPosTo) {
QPainterPath qPath;
qPath.moveTo(qPosFrom);
qPath.cubicTo(qPosFrom + QPoint(lenTan, 0), qPosTo - QPoint(lenTan, 0), qPosTo);
qPainter.drawPath(qPath);
};
const QObjectList& pQChildren = parent()->children();
for (const Link& link : links) {
const int from = link.first;
if (from >= pQChildren.size()) continue; // invalid from index
const QWidget* pQWidgetFrom
= dynamic_cast<const QWidget*>(pQChildren[from]);
if (!pQWidgetFrom) continue; // shouldn't happen
const int to = link.second;
if (to >= pQChildren.size()) continue; // invalid to index
const QWidget* pQWidgetTo
= dynamic_cast<const QWidget*>(pQChildren[to]);
if (!pQWidgetTo) continue; // shouldn't happen
const QPoint qPosFrom
= pQWidgetFrom->pos() + QPoint(3 * pQWidgetFrom->width() / 4, pQWidgetFrom->height() / 2);
const QPoint qPosTo
= pQWidgetTo->pos() + QPoint(1 * pQWidgetTo->width() / 4, pQWidgetTo->height() / 2);
//qPainter.drawLine(qPosFrom, qPosTo);
drawCurve(qPosFrom, qPosTo);
}
}
int main(int argc, char** argv)
{
qDebug() << "Qt Version:" << QT_VERSION_STR;
QApplication app(argc, argv);
// setup GUI
Widget qWinMain;
qWinMain.setWindowTitle("Test Draw Over Children");
qWinMain.resize(320, 240);
QGridLayout qGrid;
qGrid.setSpacing(16);
#define ITEM(ROW, COL) \
QPushButton qBtn##ROW##COL("txt "#ROW", "#COL); \
qGrid.addWidget(&qBtn##ROW##COL, ROW - 1, COL - 1)
ITEM(1, 1); ITEM(1, 2);
ITEM(2, 1); ITEM(2, 2);
ITEM(3, 1); ITEM(3, 2);
ITEM(4, 1); ITEM(4, 2);
#undef ITEM
qWinMain.setLayout(&qGrid);
qWinMain.addLink(1, 8);
qWinMain.addLink(5, 2);
qWinMain.show();
// runtime loop
return app.exec();
}
Output:
Qt Version: 5.15.1

Wrong rich text height (QTextDocument) in item rendered by custom item delegate [Qt]

I have a problem with the text height of a QTextDocument in my tree views item delegate.
My paint() and sizeHint() methods are almost exactly the same, but calculate different height of the text (the same text).
The difference of height varies depending on the font. If everything is written in one size, then the height is correct, but if I use different sizes, the heights doesn't match.
I read a topic about using font metrics, or calcuating the font height form paiter.boundingRect, but I don't know how to use this in a QTextDocument.
I thought that the problem is with the font, so I changed the font globaly to one with the code below. It didn't help.
//in main():
QFont font("MS Shell Dlg 2",8.25,50);
font.setPointSizeF(8.25);
QApplication::setFont(font);
My code in my custom itemDelegate is:
QSize MyItemDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
{
QStyleOptionViewItem options = option;
initStyleOption(&options, index);
int scrollBarrWidth = -2;
if(scrollBarVisible){
scrollBarrWidth = 10; //options.widget->style()->pixelMetric(QStyle::PM_ScrollBarExtent);
}
int checkboxWidth = 0;
if(checkboxesForConferenceActive){
checkboxWidth = 18;
}
QRect rectLeft(checkboxWidth,0,26,26);
QRect rectRight((options.widget->rect().right()-30-scrollBarrWidth),0,26,26);
QRect rectCenter((rectLeft.right()+5),-2,(options.widget->rect().width()-(rectLeft.width()+rectRight.width()+scrollBarrWidth+checkboxWidth+8+4)),26);
QRect boundingRect;
QString nameText(index.data(Qt::UserRole).toString());
QString extNoText(index.data(Qt::UserRole+1).toString());
QString phoneText(index.data(Qt::UserRole+100).toString());
QTextDocument textDoc;
textDoc.setDefaultFont(options.font);
textDoc.setTextWidth(rectCenter.width());
textDoc.setDocumentMargin(0);
QTextCursor textCursor(&textDoc);
QString wholeText("");
QImage statusIcon(*icoChooser->choseCustomStateImageForSubscriber(index.data(Qt::UserRole+12).toInt()));
bool wczytajfote = !statusIcon.isNull();
wholeText.append("<b>");
wholeText.append(nameText);
wholeText.append("</b>");
textCursor.insertHtml(wholeText);
if(wczytajfote){
textCursor.movePosition(QTextCursor::EndOfLine, QTextCursor::MoveAnchor);
textCursor.insertImage(statusIcon.scaledToWidth(12));
textCursor.movePosition(QTextCursor::End, QTextCursor::MoveAnchor);
} else {
textCursor.insertHtml("&nbsp");
}
wholeText.clear();
if(!extNoText.isEmpty()){
wholeText.append("<font color = #005c99>");
wholeText.append(QString(" ("+extNoText+") "));
wholeText.append("</font>");
wholeText.append("<br><font style=\"font-size:10px; color: #6d6d78\">");
wholeText.append(index.data(Qt::UserRole+4).toString());
wholeText.append("</font>");
textCursor.insertHtml(wholeText);
if(textDoc.idealWidth() < rectCenter.width() ){
QRect clip(0,0,textDoc.idealWidth(), textDoc.size().height());
return QSize(textDoc.idealWidth(), textDoc.size().height()+4);
} else {
QRect clip(0,0,textDoc.idealWidth(), textDoc.size().height());
return QSize(textDoc.idealWidth(), textDoc.size().height()+4);
}
} else {
wholeText.append("<br>");
wholeText.append("<font size = 2 color = #005c99>");
wholeText.append(QString(" ("+phoneText+") "));
wholeText.append("</font>");
textCursor.insertHtml(wholeText);
QRect clip(0,0,textDoc.idealWidth(), textDoc.size().height());
return QSize(textDoc.idealWidth(), textDoc.size().height()+6);
}
}
and the paint method is almost exactly the same.
void MyItemDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
QStyleOptionViewItem options = option;
initStyleOption(&options, index);
painter->save();
painter->setRenderHint(QPainter::Antialiasing, true);
painter->setRenderHint(QPainter::SmoothPixmapTransform, true);
painter->setRenderHint(QPainter::TextAntialiasing, true);
options.text = "";
int scrollBarrWidth = -2;
if(scrollBarVisible){
scrollBarrWidth = 10; //options.widget->style()->pixelMetric(QStyle::PM_ScrollBarExtent);
}
int checkboxWidth = 0;
if(checkboxesForConferenceActive){
checkboxWidth = 18;
}
QRect rectLeft(checkboxWidth,0,26,26);
QRect rectRight((options.widget->rect().right()-30-scrollBarrWidth),0,26,26); //obliczam na podstawie prostokąta, który jest przed nim i który ma dynamicznie ustawiane wymiary
QRect rectCenter((rectLeft.right()+5),-2,(options.widget->rect().width()-(rectLeft.width()+rectRight.width()+scrollBarrWidth+checkboxWidth+8+4)),26); //obliczam prostokąt na podstawie wcześniejszego prostokąta i prostokąta wymiarów okna
QRect boundingRect;
QString nameText(index.data(Qt::UserRole).toString());
QString extNoText(index.data(Qt::UserRole+1).toString());
QString phoneText(index.data(Qt::UserRole+100).toString());
painter->translate(options.widget->rect().left(), options.rect.top());
QTextDocument textDoc;
textDoc.setTextWidth(rectCenter.width());
QTextCursor textCursor(&textDoc);
QString wholeText("");
QImage statusIcon(*icoChooser->choseCustomStateImageForSubscriber(index.data(Qt::UserRole+12).toInt()));
bool wczytajfote = !statusIcon.isNull();
wholeText.append("<b>");
wholeText.append(nameText);
wholeText.append("</b>");
textCursor.insertHtml(wholeText);
if(wczytajfote){
textCursor.movePosition(QTextCursor::EndOfLine, QTextCursor::MoveAnchor);
textCursor.insertImage(statusIcon.scaledToWidth(12));
textCursor.movePosition(QTextCursor::End, QTextCursor::MoveAnchor);
} else {
textCursor.insertHtml("&nbsp");
}
wholeText.clear();
if(!extNoText.isEmpty()){
wholeText.append("<font size = 2 color = #005c99>");
wholeText.append(QString(" ("+extNoText+") "));
wholeText.append("</font>");
wholeText.append("<br><font style=\"font-size:10px; color: #6d6d78\">");
wholeText.append(index.data(Qt::UserRole+4).toString());
wholeText.append("</font>");
} else {
wholeText.append("<br>");
wholeText.append("<font size = 2 color = #005c99>");
wholeText.append(QString(" ("+phoneText+") "));
wholeText.append("</font>");
}
textCursor.insertHtml(wholeText);
painter->translate(rectCenter.left(), rectCenter.top());
QRect clip(0,0,textDoc.size().width(), textDoc.size().height());
textDoc.drawContents(painter, clip);
painter->restore();
}
}
The text in sizeHint is used to determine the item height, so now it looks like the pic below:
I'm using Qt 5.7.1 MSVC 2015 64 bit on windows, but the problem is the same on Ubuntu 16.04 with gcc.
(Posted on behalf of the asker)
It turned out to be my fault all along.
The following line responsible in the sizeHint() method was responsible:
textDoc.setDocumentMargin(0);

QTextDocument in QAbstractItemDelegate paint method

I have a class which inherits QAbstractItemDelegate and I use QTextDocument inside the paint() method. My model contains two items, but when I run my qt application, the items are drawn in the first item of QListView.
CODE
void ProductItemDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option,
const QModelIndex &index) const
{
bool selected = (option.state & QStyle::State_Selected) == QStyle::State_Selected;
if (selected)
{
painter->fillRect(option.rect, option.palette.highlight());
}
painter->save();
painter->setRenderHint(QPainter::Antialiasing, true);
if (selected)
{
painter->setPen(option.palette.highlightedText().color());
}
else
{
painter->setPen(option.palette.text().color());
}
mTextDocument.drawContents(painter);
painter->restore();
}
QSize ProductItemDelegate::sizeHint(const QStyleOptionViewItem &option,
const QModelIndex &index) const
{
MyItem *myItem = index.data(Qt::UserRole + 1).value<MyItem *>();
mTextDocument->clear();
mTextDocument->setDefaultFont(option.font);
mTextDocument->setPageSize(QSizeF(option.rect.width(), -1));
QTextCursor cursor = QTextCursor(mTextDocument);
QVector<QTextLength> columnConstraints;
columnConstraints << QTextLength(QTextLength::PercentageLength, 60);
columnConstraints << QTextLength(QTextLength::PercentageLength, 30);
columnConstraints << QTextLength(QTextLength::PercentageLength, 10);
QTextTableFormat tableFormat;
tableFormat.setBorder(1);
tableFormat.setBorderBrush(QBrush(Qt::black));
tableFormat.setColumnWidthConstraints(columnConstraints);
QTextTable *table = cursor.insertTable(2, 3, tableFormat);
table->mergeCells(0, 0, 1, 3);
QTextCursor cellCursor;
QTextTableCell cell00 = table->cellAt(0, 0);
cellCursor = cell00.firstCursorPosition();
cellCursor.insertText(myItem->name());
QTextTableCell cell10 = table->cellAt(1, 0);
cellCursor = cell10.firstCursorPosition();
cellCursor.insertText(myItem->text1());
QTextTableCell cell11 = table->cellAt(1, 1);
cellCursor = cell11.firstCursorPosition();
cellCursor.insertText(myItem->text2());
return mTextDocument->size().toSize();
}
These are the result of the code above.
The item was not drawn in second entry.
Both items are painted in the first entry.
You should place your painter to the right spot before painting with it.
After the first painter->save() add :
painter->resetTransform();
painter->translate(option.rect.topLeft());

QStandardItemModel header with widget and text

i need to use checkbox with text, like this "Check all":
in header of QStanndardItemModel. I tried like this
QStandardItem* item0 = new QStandardItem("some text");
item0->setCheckable(true);
item0->setCheckState(Qt::Checked);
item0->setText("some text");
_model->setHorizontalHeaderItem(1, item0);
This way only works for items not for header, i mean for items if i use
_model->setItem(new QStandardItem(some_item);
I heard about writing my own class which inherit QHeaderView but i dont know if this can help in my problem. I would ask if there is a easy way to achieve this?
Regards
i did something like this:
QStandardItemModel *model = new QStandardItemModel(this);
ui->tableView->setModel(model);
QtCheckHeaderView *header = new QtCheckHeaderView(Qt::Horizontal, ui->tableView);
QtCheckHeaderView *vheader = new QtCheckHeaderView(Qt::Vertical, ui->tableView);
ui->tableView->setHorizontalHeader(header);
ui->tableView->setVerticalHeader(vheader);
QStandardItem *root = model->invisibleRootItem();
QList<QList<QStandardItem *> > items;
for (int i = 0; i < 10; ++i)
{
QList<QStandardItem *> res;
for (int j = 0; j < 1000; ++j)
{
res.append(new QStandardItem(QString("%1;%2").arg(j).arg(i)));
vheader->addCheckable(j);
}
items.append(res);
root->appendColumn(res);
header->addCheckable(i);
}
this works great. Draws checkbox with text in vertical and horizontal header.
But this works great only if i put this code to MainWindow constructor. If i put this code to for example pushbutton function this wont work. Datas are fine but headers are invisible.
ui->tableView->repaint();
wont work. Someone maybe knows the answer why this is happening and/or how to solve this?
thanks for answer
Qt models don't support item flags for headers. Setting item flags on items that are used as headers has no effect. QHeaderView doesn't support drawing checkboxes. I'm afraid subclassing QHeaderView is the simpliest way.
I wrote an example of how it can be implemented based on this FAQ page. This class assumes that you use QStandardItemModel and uses flags of its header items. If someone would want to use other model class, subclassing QAbstractItemModel and implementing missing interface and changing headerview implementation would be needed.
class MyHeader : public QHeaderView
{
public:
MyHeader(Qt::Orientation orientation, QWidget * parent = 0);
protected:
QStandardItemModel* standard_model;
virtual void paintSection(QPainter *painter, const QRect &rect, int logicalIndex) const;
virtual void mousePressEvent(QMouseEvent *event);
virtual void setModel(QAbstractItemModel* model);
};
MyHeader::MyHeader(Qt::Orientation orientation, QWidget *parent) : QHeaderView(orientation, parent)
{
standard_model = 0;
}
void MyHeader::paintSection(QPainter *painter, const QRect &rect, int logical_index) const {
painter->save();
QHeaderView::paintSection(painter, rect, logical_index);
painter->restore();
if (standard_model && orientation() == Qt::Horizontal) {
QStandardItem* item = standard_model->horizontalHeaderItem(logical_index);
if (item && item->isCheckable()) {
int offset = (height() - style()->pixelMetric(QStyle::PM_IndicatorHeight))/2;
int pos = sectionViewportPosition(logical_index);
QStyleOptionButton option;
option.rect = QRect(offset + pos, offset,
style()->pixelMetric(QStyle::PM_IndicatorWidth),
style()->pixelMetric(QStyle::PM_IndicatorHeight));
if (item->checkState() == Qt::Checked) {
option.state = QStyle::State_On;
} else {
option.state = QStyle::State_Off;
}
option.state |= QStyle::State_Enabled;
style()->drawPrimitive(QStyle::PE_IndicatorCheckBox, &option, painter);
}
}
}
void MyHeader::mousePressEvent(QMouseEvent *event) {
int offset = (height() - style()->pixelMetric(QStyle::PM_IndicatorHeight))/2;
if (standard_model && orientation() == Qt::Horizontal) {
for(int logical_index = 0; logical_index < count(); logical_index++) {
int pos = sectionViewportPosition(logical_index);
QRect rect(offset + pos, offset,
style()->pixelMetric(QStyle::PM_IndicatorWidth),
style()->pixelMetric(QStyle::PM_IndicatorHeight));
if (rect.contains(event->pos())) {
QStandardItem* item = standard_model->horizontalHeaderItem(logical_index);
if (item && item->isCheckable()) {
item->setCheckState(item->checkState() == Qt::Checked ? Qt::Unchecked : Qt::Checked);
return;
}
}
}
}
QHeaderView::mousePressEvent(event);
}
void MyHeader::setModel(QAbstractItemModel *model) {
QHeaderView::setModel(model);
standard_model = qobject_cast<QStandardItemModel*>(model);
}
//usage
QTableView view;
QStandardItemModel model;
model.setColumnCount(5);
QStandardItem* item0 = new QStandardItem("some text");
item0->setCheckable(true);
item0->setCheckState(Qt::Checked);
item0->setText("some text");
model.setHorizontalHeaderItem(0, item0);
view.setModel(&model);
MyHeader *myHeader = new MyHeader(Qt::Horizontal, &view);
view.setHorizontalHeader(myHeader);
view.show();

Custom ItemDelegate for QListView State_Selected

I created CustomItemDelegate from QStyledItemDelegate and i'm using the paint() method to give a better look to my QListView.
If I click on an item, option.state never has State_Selected, why is that?
I have a selection model, single, row, and the selection rectangle is visible.
qDebug only prints out these:
QStyle::State( "Active | Enabled" )
QStyle::State( "Active | Enabled | MouseOver" )
void SyrupItemDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
QRect rect = option.rect;
qDebug() << option.state;
if (option.state & QStyle::State_Selected)
{
painter->drawRoundedRect(option.rect,5,5);
painter->setPen(QPen(QPalette::HighlightedText ) );
if (option.state & QStyle::State_Active)
{
painter->setBrush(QBrush(QPalette().highlight()));
} else
{
//painter->setBrush(QBrush(QPalette().color(QPalette::Inactive,
//QPalette::Highlight)));
QLinearGradient gradient(0, 0, 0, 100);
gradient.setColorAt(0.0, QColor(0,0,230));
gradient.setColorAt(1.0, QColor(250,250,250));
painter->setBrush(gradient);
}
} else
painter->setPen(QPen(QPalette::Text));
if ( !index.isValid() )
return;
int row = index.row();
// painter->save();
// painter->setRenderHint(QPainter::Antialiasing,true);
QString res = index.sibling(row,SyrupsSQLModel::SYRUP_NM_COL_INDEX).data().toString();
QRectF rc(rect);
rc.setTop(rc.top()+ PADDING);
rc.setLeft(rc.left()+ 2* PADDING + IMG_WIDTH);
QFont font = option.font;
font.setPointSize(font.pointSize()+4);
painter->setFont(font);
painter->drawText(rc,res);
res = index.sibling(row,SyrupsSQLModel::SYRUP_GRP_COL_INDEX).data().toString().toLower();
rc.setTop(rect.top()+PADDING );
rc.setLeft(rect.left()+PADDING );
painter->drawPixmap(rc.topLeft(),QIcon(":/prodgrp/"+res).pixmap(QSize(IMG_WIDTH,IMG_HEIGHT)));
//SyrupsSQLModel::FORMULA_COL_INDEX:
//SyrupsSQLModel::SYRUP_ID_COL_INDEX:
//Painter->restore();
}
I'm using Qt 5.0.2 32bit (Win).
The problem was in the delegate class I reimplemented editorEvent() function badly. I commented out that sections and it works.

Resources