0

我的树视图项目委托中的 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);

    }
 }

和paint方法几乎一模一样。

 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 中的文本用于确定项目高度,所以现在看起来如下图所示: 图片

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

4

1 回答 1

0

(以提问者名义发布)

原来一直都是我的错。

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

textDoc.setDocumentMargin(0);
于 2017-03-27T16:09:14.603 回答