自定义项委托[Qt]呈现的项目中的富文本高度(QTextDocument)错误

时间:2017-03-27 08:19:12

标签: qt richtext qitemdelegate qtextdocument sizehint

我的树视图项委托中的QTextDocument的文本高度存在问题。

我的paint()和sizeHint()方法几乎完全相同,但计算文本的不同高度(相同的文本)。

高度的差异取决于字体。如果一切都是用一种尺寸书写的,那么高度是正确的,但如果我使用不同的尺码,则高度不匹配。

我读了一个关于使用字体指标或计算paiter.boundingRect字体高度的主题,但我不知道如何在QTextDocument中使用它。

我认为问题出在字体上,所以我将字体全局变为一个,代码如下。它没有帮助。

 //in main():
     QFont font("MS Shell Dlg 2",8.25,50);
     font.setPointSizeF(8.25);
     QApplication::setFont(font);

我的自定义itemDelegate中的代码是:

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

    }
 }

和绘画方法几乎完全一样。

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

sizeHint中的文本用于确定项目高度,所以现在看起来如下图: picture

我在Windows上使用Qt 5.7.1 MSVC 2015 64位,但在使用gcc的Ubuntu 16.04上问题是一样的。

1 个答案:

答案 0 :(得分:0)

(代表提问者发布)

事实证明这是我的错。

sizeHint()方法中负责的以下行负责:

textDoc.setDocumentMargin(0);