Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/qt/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/wcf/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
QAbstratemDelegate绘制方法中的QTextDocument_Qt_Qt5.4_Qtextdocument - Fatal编程技术网

QAbstratemDelegate绘制方法中的QTextDocument

QAbstratemDelegate绘制方法中的QTextDocument,qt,qt5.4,qtextdocument,Qt,Qt5.4,Qtextdocument,我有一个继承了qabstractemdelegate的类,我在paint()方法中使用QTextDocument。我的模型包含两个项目,但当我运行qt应用程序时,这些项目将在QListView的第一个项目中绘制 代码 void ProductItemDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex

我有一个继承了
qabstractemdelegate
的类,我在
paint()
方法中使用
QTextDocument
。我的模型包含两个项目,但当我运行qt应用程序时,这些项目将在
QListView
的第一个项目中绘制

代码

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();
}
void ProductItemDelegate::paint(QPaint*painter,const qStyleOption视图项和选项,
常数QModelIndex和索引)常数
{
bool selected=(option.state&QStyle::state\u selected)=QStyle::state\u selected;
如果(选定)
{
painter->fillRect(option.rect,option.palete.highlight());
}
画师->保存();
painter->setRenderHint(QPainter::抗锯齿,true);
如果(选定)
{
painter->setPen(option.palete.highlightedText().color());
}
其他的
{
painter->setPen(option.palete.text().color());
}
mTextDocument.drawContents(画师);
画师->还原();
}
QSize ProductItemDelegate::sizeHint(常量QStyleOptionViewItem&option,
常数QModelIndex和索引)常数
{
MyItem*MyItem=index.data(Qt::UserRole+1.value();
mTextDocument->clear();
mTextDocument->setDefaultFont(option.font);
mTextDocument->setPageSize(QSizeF(option.rect.width(),-1));
QTextCursor=QTextCursor(mTextDocument);
矢量约束;
columnConstraints name());
QTextTableCell10=表格->单元格(1,0);
cellCursor=cell10.firstCursorPosition();
插入文本(myItem->text1());
QTextTableCell11=表格->单元格(1,1);
cellCursor=cell11.firstCursorPosition();
插入文本(myItem->text2());
返回mTextDocument->size().toSize();
}
这些是上述代码的结果。

第二个条目中未绘制项目。

两个项目都在第一个条目中绘制。

在使用油漆前,应将油漆工放置在正确的位置

在第一次
painter->save()之后添加:

painter->resetTransform();
painter->translate(option.rect.topLeft());

这解决了我的问题。你能解释一下你的答案吗?我查看了Qt文档中关于
qabstractemdelegate
,但我从未见过这两行代码。据我所知,QPainter的初始位置位于父对象的左上角()。(我想是树景吧?)