QTextDocument를 사용 중입니다. QTextTableCell의 boundingbox를 찾아야하지만, 알아낼 수있는 방법이 없기 때문에 일부 맞춤형 테스트를 수행해야합니다. 누군가가 그것을 얻을 수 있어야하는 방법에 대한 약간의 빛이나 아이디어를 던질 수 있습니까?QTextTableCell의 boundingBox를 얻는 방법
0
A
답변
0
Qt 코드를 변경하지 않고도 아무 것도 할 수 없다는 것을 알았습니다. 여기서해야 할 일이 있습니다.
==== //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);
1
실제로 셀의 rect를 가져 오는 방법이 있습니다. 셀 내용을 반복하고 영역에 참여해야합니다. 마지막으로 셀의 패딩을 추가하십시오. 다음은 작동하는 예제입니다. 그러나 셀에 중첩 된 프레임 또는 여러 행에 걸쳐있는 셀이 포함되어있는 경우이 확장해야 할 수도 있습니다.
QRectF cellBounds(QTextEdit& edit, QTextTable& table, QTextTableCell& cell, bool rowCheck = true) {
QTextTableFormat tableFormat = table.format();
qreal cellExtra = tableFormat.cellPadding() /* + tableFormat.border()*/;
int vOffset = edit.verticalScrollBar()->value();
QTextBlock next = cell.firstCursorPosition().block();
QTextBlock last = cell.lastCursorPosition().block();
QAbstractTextDocumentLayout* layout = edit.document()->documentLayout();
QRectF bounds;
while (next.isValid()) {
bounds = bounds.isNull() ? layout->blockBoundingRect(next) : bounds.united(layout->blockBoundingRect(next));
if (next == last) {
break;
}
next = next.next();
}
if (!bounds.isNull()) {
bounds.adjust(-cellExtra, -cellExtra, cellExtra, cellExtra);
bounds.translate(0, -vOffset);
if(rowCheck) {
for(int col = 0; col < table.columns(); col++) {
if(col != cell.column()) {
QTextTableCell nextCell = table.cellAt(cell.row(), col);
QRectF nextBounds = cellBounds(edit, table, nextCell, false);
if(nextBounds.height() > bounds.height())
bounds.setHeight(nextBounds.height());
}
}
}
}
return bounds;
}