Search code examples
qt4

How to get boundingBox of a QTextTableCell


I am using QTextDocument and needs to do some custom hit testing, for that I need to find boundingbox of QTextTableCell but couldn't find any way to figure it out. Can someone throw some light or any ideas on how I should be able to get that.


Solution

  • I found there wasn't anyway to do it without making any change to Qt code, here is diff of what I had to do.

    
        ==== //local/qt5/qtbase/src/gui/text/qabstracttextdocumentlayout.h#2 (text) ====
    
        @@ -92,6 +92,11 @@
             virtual QRectF frameBoundingRect(QTextFrame *frame) const = 0;
             virtual QRectF blockBoundingRect(const QTextBlock &block) const = 0;
    
        +    virtual QRectF cellBoundingRect(QTextTable &table, const QTextTableCell &cell) const
        +    {
        +        return QRectF();
        +    }
        +
             void setPaintDevice(QPaintDevice *device);
             QPaintDevice *paintDevice() const;
    
    
        ==== //local/qt5/qtbase/src/gui/text/qtextdocumentlayout.cpp#3 (text) ====
    
        @@ -3201,6 +3201,17 @@
             return QRectF(pos, data(table)->size.toSizeF());
         }
    
        +QRectF QTextDocumentLayout::cellBoundingRect( QTextTable &table, const QTextTableCell &cell) const
        +{
        +    Q_D(const QTextDocumentLayout);
        +    if (d->docPrivate->pageSize.isNull())
        +        return QRectF();
        +    d->ensureLayoutFinished();
        +    QTextTableData *td = static_cast(data(&table));
        +    QRectF cellRect = td->cellRect(cell);
        +    return cellRect;
        +}
        +
         QRectF QTextDocumentLayout::frameBoundingRect(QTextFrame *frame) const
         {
             Q_D(const QTextDocumentLayout);
    
        ==== //local/qt5/qtbase/src/gui/text/qtextdocumentlayout_p.h#1 (text) ====
    
        @@ -104,6 +104,8 @@
    
             bool contentHasAlignment() const;
    
        +    virtual QRectF cellBoundingRect(QTextTable &table, const QTextTableCell &cell) const;
        +
         protected:
             void documentChanged(int from, int oldLength, int length);
             void resizeInlineObject(QTextInlineObject item, int posInDocument, const QTextFormat &format);