QTextDocument in QAbstractItemDelegate paint method - qt

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());

Related

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);

How to set icon center in QTableView?

NetworkPageForm::NetworkPageForm(QWidget *parent) :
QWidget(parent),
ui(new Ui::NetworkPageForm),
devicesModel(NULL)
{
ui->setupUi(this);
devicesModel = new QStandardItemModel(0, 4, parent);
devicesModel->setHeaderData(0, Qt::Horizontal, QObject::tr("IP"));
devicesModel->setHeaderData(1, Qt::Horizontal, QObject::tr("Name"));
devicesModel->setHeaderData(2, Qt::Horizontal, QObject::tr("Last Online"));
devicesModel->setHeaderData(3, Qt::Horizontal, QObject::tr("Status"));
ui->devicesTableView->setModel(devicesModel);
ui->devicesTableView->resizeColumnsToContents();
}
void NetworkPageForm::addDevice(const QString &ip, int device_type) {
bool haveSameItem = false;
for(int i=0; i<devicesModel->rowCount(); i++) {
QStandardItem * ipItem = devicesModel->item(i, 0);
QStandardItem * nameItem = devicesModel->item(i, 1);
if(QString::compare(ipItem->text(), ip)== 0 && QString::compare(nameItem->text(), deviceStr)==0) {
devicesModel->setData(devicesModel->index(i, 2), BaseModel::now());
haveSameItem = true;
}
}
if(!haveSameItem)
{
int last = devicesModel->rowCount();
devicesModel->insertRow(last);
devicesModel->setData(devicesModel->index(last, 0), ip);
devicesModel->setData(devicesModel->index(last, 1), device_type);
devicesModel->setData(devicesModel->index(last, 2), BaseModel::now());
devicesModel->setData(devicesModel->index(last, 3), QIcon(":/res/images/online_icon.png"), Qt::DecorationRole);
// This function does not work, the icon is algin left.
// devicesModel->item(last, 3)->setTextAlignment(Qt::AlignCenter);
}
ui->devicesTableView->resizeColumnsToContents();
}
Is there a way to set QIcon item center in QTableView?
Update:
I create my own QStyledItemDelegate sub class as #RazrFalcon answered.
#include <QStyledItemDelegate>
class MyDelegate : public QStyledItemDelegate
{
Q_OBJECT
public:
MyDelegate(QWidget *parent = 0) : QStyledItemDelegate(parent) {}
void paint(QPainter *painter, const QStyleOptionViewItem &option,
const QModelIndex &index) const;
QSize sizeHint(const QStyleOptionViewItem &option,
const QModelIndex &index) const;
void setModelData(QWidget *editor, QAbstractItemModel *model,
const QModelIndex &index) const;
private slots:
};
void MyDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const {
if(index.column() == 3) {
// TODO
} else {
QStyledItemDelegate::paint(painter, option, index);
}
}
QSize MyDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const {
if(index.column() == 3) {
// TODO
} else {
return QStyledItemDelegate::sizeHint(option, index);
}
}
void MyDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const {
QStyledItemDelegate::setModelData(editor, model, index);
}
And set ui->devicesTableView->setItemDelegate(new MyDelegate);
Could someone help me how to set icon column center in QTableView?
There is no default way. You should implement your own QStyledItemDelegate.
UPD: example added
void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
Q_ASSERT(index.isValid());
QStyleOptionViewItemV4 opt = option;
initStyleOption(&opt, index);
// disable default icon
opt.icon = QIcon();
// draw default item
QApplication::style()->drawControl(QStyle::CE_ItemViewItem, &opt, painter, 0);
const QRect r = option.rect;
// get pixmap
QIcon icon = qvariant_cast<QIcon>(index.data(Qt::DecorationRole));
QPixmap pix = icon.pixmap(r.size());
// draw pixmap at center of item
const QPoint p = QPoint((r.width() - pix.width())/2, (r.height() - pix.height())/2);
painter->drawPixmap(r.topLeft() + p, pix);
}
It's no need to do it in delegate. The delegate will make your style sheet unavailable.
You can paint the icon in the middle of a rectangle pixmap which is as same size as the cell, and return it in the QAbstractItemModel::data() function with Qt::DecorationRole. Like this:
Qt::DecoratoinRole:
QPixmap pixmap(w, h); //w=cell width, h=cell
pixmap.fill(Qt::transparent); // draw a transparent rectangle
QPixmap iconPixmap(":/xx.png");
QPainter painter(&pixmap);
//Calculate the center coordinate x,y for iconPixmap
painter.draw(x, y, iconWidth, iconHeight, iconPixmap);
return pixmap;

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.

Qt delegate not calling createEditor()

I have ComboBox filled with CheckBoxes when the ComboBox is opened the first item's delegate createEditor is not called but when you move to second item it is called and delegate is properly created. After this when you move back to first item then delegate is working. The problem is only with selecting first item for first time. If you have got only one item in comboBox this item is unselectable.
This is my delegate's code:
#include "checkboxlistdelegate.h"
CheckBoxListDelegate::CheckBoxListDelegate(QObject *parent)
: QItemDelegate(parent) {
}
void CheckBoxListDelegate::paint(QPainter *painter,
const QStyleOptionViewItem &option,
const QModelIndex &index) const {
//Get item data
bool value = index.data(Qt::UserRole).toBool();
QString text = index.data(Qt::DisplayRole).toString();
// fill style options with item data
const QStyle *style = QApplication::style();
QStyleOptionButton opt;
opt.state |= value ? QStyle::State_On : QStyle::State_Off;
opt.state |= QStyle::State_Enabled;
opt.text = text;
opt.rect = option.rect;
opt.palette = QPalette(Qt::white);
// draw item data as CheckBox
style->drawControl(QStyle::CE_CheckBox,&opt,painter);
}
QWidget* CheckBoxListDelegate::createEditor(QWidget *parent,
const QStyleOptionViewItem& option,
const QModelIndex & index ) const {
QCheckBox *editor = new QCheckBox(parent);
editor->setStyleSheet("QCheckBox {background-color: #aaaaaa; color: white;}");
return editor;
}
void CheckBoxListDelegate::setEditorData(QWidget *editor,
const QModelIndex &index) const {
QCheckBox *myEditor = static_cast<QCheckBox*>(editor);
myEditor->setText(index.data(Qt::DisplayRole).toString());
myEditor->setChecked(index.data(Qt::UserRole).toBool());
}
void CheckBoxListDelegate::setModelData(QWidget *editor,
QAbstractItemModel *model,
const QModelIndex &index) const {
//get the value from the editor (CheckBox)
QCheckBox *myEditor = static_cast<QCheckBox*>(editor);
bool value = myEditor->isChecked();
//set model data
QMap<int,QVariant> data;
data.insert(Qt::DisplayRole,myEditor->text());
data.insert(Qt::UserRole,value);
model->setItemData(index,data);
emit indexChanged(index);
}
void CheckBoxListDelegate::updateEditorGeometry(QWidget *editor,
const QStyleOptionViewItem &option,
const QModelIndex &index ) const {
Q_UNUSED(index);
editor->setGeometry(option.rect);
}
And this is code of my QComboBox subclass:
CheckBoxList::CheckBoxList(QWidget *widget)
: QComboBox(widget),title(""),selection() {
// set delegate items view
view()->setItemDelegate(new CheckBoxListDelegate(this));
view()->setStyleSheet("QAbstractItemView {background-color:white;}");
// Enable editing on items view
view()->setEditTriggers(QAbstractItemView::AllEditTriggers);
connect(view()->model(), SIGNAL(dataChanged(QModelIndex,QModelIndex)),
this, SLOT(onItemClicked(QModelIndex)));
}
void CheckBoxList::paintEvent(QPaintEvent *) {
QStylePainter painter(this);
painter.setPen(palette().color(QPalette::Text));
QStyleOptionComboBox opt;
initStyleOption(&opt);
opt.currentText = title;
painter.drawComplexControl(QStyle::CC_ComboBox, opt);
painter.drawControl(QStyle::CE_ComboBoxLabel, opt);
}
void CheckBoxList::setDisplayText(QString text) {
title = text;
}
QString CheckBoxList::getDisplayText() const {
return title;
}
bool CheckBoxList::isChecked(const QModelIndex& index) const {
return view()->model()->itemData(index).value(Qt::UserRole).toBool();
}
Thank you.

QTableView's cells are empty, but headers show up

I created a delegate pretty much copied from Qt's Spin Box Delegate Example and I'm trying to fill a QTableView. However, I'm getting a strange problem where the table headers show up but the cells are empty and cannot be clicked on.
Code for the delegate:
#include "double_spinbox_delegate.h"
DoubleSpinBoxDelegate::DoubleSpinBoxDelegate(QObject *parent) : QItemDelegate(parent){}
QWidget *DoubleSpinBoxDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const{
QDoubleSpinBox *editor = new QDoubleSpinBox(parent);
editor->setValue(0);
// if (index.column() == 0){
// editor->setMinimum(0);
// editor->setMaximum(255);
// }
// else{
// editor->setMinimum(0);
// editor->setMaximum(1);
// }
return editor;
}
void DoubleSpinBoxDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const{
double value = index.model()->data(index, Qt::EditRole).toDouble();
QDoubleSpinBox *doubleSpinBox = static_cast<QDoubleSpinBox*>(editor);
doubleSpinBox->setValue(value);
}
void DoubleSpinBoxDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const{
QDoubleSpinBox *doubleSpinBox = static_cast<QDoubleSpinBox*>(editor);
doubleSpinBox->interpretText();
double value = doubleSpinBox->value();
model->setData(index, value, Qt::EditRole);
}
void DoubleSpinBoxDelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const{
editor->setGeometry(option.rect);
}
and the function I'm calling in the form constructor
void MainWindow::InitializeColorTable(){
QTableView *tableColor = ui->tableColor;
QStandardItemModel *model = new QStandardItemModel(4, 4, ui->tableColor);
// QStandardItemModel *model = this->colorTableModel;
tableColor->setModel(model);
DoubleSpinBoxDelegate delegate;
tableColor->setItemDelegate(&delegate);
model->setHorizontalHeaderLabels(QStringList() << tr("Value") << tr("R") << tr("G") << tr("B"));
for (int row = 0; row < model->rowCount(); ++row){
for (int col = 0; col < model->columnCount(); ++col){
QModelIndex index = model->index(row, col, QModelIndex());
model->setData(index, QVariant((row + 1.0) * (col + 1.0)), Qt::EditRole);
}
}
}
Your delegate is allocated on the stack, and it is deleted after it goes out of scope.
DoubleSpinBoxDelegate delegate;
tableColor->setItemDelegate(&delegate);
create your delegate with new instead.

Resources