Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Why does QPainter::boundingRect returns 0 0?
Forum Updated to NodeBB v4.3 + New Features

Why does QPainter::boundingRect returns 0 0?

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 2 Posters 461 Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    deleted385
    wrote on last edited by deleted385
    #1

    Here's what I've in the window:

    x1.gif

    and here's the code which generates that:

    Window::Window(QWidget *parent) : QWidget(parent){
        printer = new QPrinter();
        printer->setPageSize(QPageSize::A4);
        printer->setPageMargins(QMarginsF(20,20,20,20), QPageLayout::Millimeter);
        document = new QTextDocument(this);
        auto rect = printer->pageRect(QPrinter::Point).size();
        //qDebug() << rect.width() << rect.height();
        document->setPageSize(QSizeF(rect.width(), rect.height()));
        createDocument();
        auto preview = new QPrintPreviewWidget(printer);
        preview->setZoomMode(QPrintPreviewWidget::ZoomMode::FitToWidth);
        connect(preview, &QPrintPreviewWidget::paintRequested, [=]{document->print(printer);});
        auto lay = new QVBoxLayout(this);
        lay->addWidget(preview);
    }
    void Window::createDocument(){
        QTextCursor cursor(document);
        QTextTableFormat tableFormat;
        tableFormat.setBorder(0);
        tableFormat.setCellSpacing(0);
        tableFormat.setCellPadding(3);
        QVector<QTextLength> columnLengths;
        columnLengths.append(QTextLength(QTextLength::PercentageLength, 40));
        columnLengths.append(QTextLength(QTextLength::PercentageLength, 20));
        columnLengths.append(QTextLength(QTextLength::PercentageLength, 20));
        columnLengths.append(QTextLength(QTextLength::PercentageLength, 20));
        tableFormat.setColumnWidthConstraints(columnLengths);
        int row = 10, column = 4;
        cursor.insertTable(row, column, tableFormat);
        for (int i = 0; i < column; i++) {
            auto cell = cursor.currentTable()->cellAt(0, i);
            QTextTableCellFormat cellFormat;
            cellFormat.setTopBorder(1);
            cellFormat.setBottomBorder(1);
            cellFormat.setBorderBrush(Qt::black);
            cellFormat.setTopBorderStyle(QTextFrameFormat::BorderStyle_Solid);
            cellFormat.setBottomBorderStyle(QTextFrameFormat::BorderStyle_Solid);
            cell.setFormat(cellFormat);
            QTextCharFormat textFormat;
            textFormat.setFontWeight(QFont::Bold);
            QTextBlockFormat blockFormat;
            blockFormat.setAlignment(Qt::AlignCenter);
            cursor.setBlockFormat(blockFormat);
            cursor.insertText("Column " + QString::number(i + 1), textFormat);
            cursor.movePosition(QTextCursor::NextCell);
        }
        QFontMetrics fm(font());
        // w: 481 h:728
        QRectF col1Rect(0,0, 481 * 0.40, fm.boundingRect("tg").height());
        QPainter painter;
        for (int r = 1; r < row; r++) {
            for (int c = 0; c < column; c++) {
                if(c == 0){
                    auto col1Text = "Column1 Text Column1 Text Column1 Text Column1";
                    auto reqRect = painter.boundingRect(col1Rect, Qt::AlignLeft|Qt::TextWordWrap, col1Text);
                    qDebug() << col1Rect.width() << col1Rect.height() << reqRect.width() << reqRect.height();
                    cursor.insertText(col1Text);
                    cursor.movePosition(QTextCursor::NextCell);
                    continue;
                }
                cursor.insertText("Row " + QString::number(r) + " Column " + QString::number(c));
                cursor.movePosition(QTextCursor::NextCell);
            }
        }
    }
    Window::~Window(){}
    

    in the second for loop of createDocument function, I've

    qDebug() << col1Rect.width() << col1Rect.height() << reqRect.width() << reqRect.height();
    

    and it always outputs:

    192.4 16 0 0
    192.4 16 0 0
    ...
    

    How to get the correct required height of the desired text?

    1 Reply Last reply
    0
    • M Offline
      M Offline
      mpergand
      wrote on last edited by mpergand
      #2

      @Emon-Haque said in Why does QPainter::boundingRect returns 0 0?:

      QPainter painter; ----> ?????
      auto reqRect = painter.boundingRect(col1Rect, Qt::AlignLeft|Qt::TextWordWrap, col1Text);

      Use instead:
      QRect QFontMetrics::boundingRect(const QRect &rect, int flags, const QString &text, int tabStops = 0, int *tabArray = nullptr) const

      D 1 Reply Last reply
      2
      • M mpergand

        @Emon-Haque said in Why does QPainter::boundingRect returns 0 0?:

        QPainter painter; ----> ?????
        auto reqRect = painter.boundingRect(col1Rect, Qt::AlignLeft|Qt::TextWordWrap, col1Text);

        Use instead:
        QRect QFontMetrics::boundingRect(const QRect &rect, int flags, const QString &text, int tabStops = 0, int *tabArray = nullptr) const

        D Offline
        D Offline
        deleted385
        wrote on last edited by
        #3

        @mpergand, yes now I get 192 16 148 32.

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved