PDA

View Full Version : QTextEdit and QTextBlock



Garfeild
18th April 2008, 21:52
Hi all.
I want to place two text blocks on one line in QTextEditor. I have tried several combinations with QTextFrame and QTextCursor but it is useless(
Code: http://pastebin.com/d82d4579
I want blabla2 with border and blabla near it.

wysota
20th April 2008, 19:44
Why not wrap them into a table?

Garfeild
21st April 2008, 21:35
So, i have tried code below. But when i use this method columns don't save their width. Any ideas?


QVector<QTextLength> columnWidth;
for(int i=0; i<15; i++)
{
if(i==0)
columnWidth.append(QTextLength::QTextLength(QTextL ength::FixedLength,174));
if(i>0 && i<10)
columnWidth.append(QTextLength::QTextLength(QTextL ength::FixedLength, 42.25));
else if(i>9 && i<12)
columnWidth.append(QTextLength::QTextLength(QTextL ength::FixedLength, 118));
else if(i==12 || i==14)
columnWidth.append(QTextLength::QTextLength(QTextL ength::FixedLength, 100));
else if(i==13)
columnWidth.append(QTextLength::QTextLength(QTextL ength::FixedLength, 70));
}

QTextTableFormat numberTableFormat;
numberTableFormat.setBorder(0);
numberTableFormat.setCellSpacing(-1);
numberTableFormat.setColumnWidthConstraints(column Width);

QTextTable *mainTable = cursor.insertTable(32, 15, numberTableFormat);


cursor = mainTable->cellAt(0,2).firstCursorPosition();
QTextFrameFormat tempFormat = cursor.currentFrame()->frameFormat();
tempFormat.setBorder(1);
cursor.insertFrame(tempFormat);
cursor.insertText("some text");

wysota
21st April 2008, 22:39
Try setting the width also for the whole table.

Garfeild
21st April 2008, 22:43
I cannot find method to do this. Can you give me link?

wysota
21st April 2008, 23:18
The table is a frame so you can use QTextFrameFormat::setWidth() and apply it to the table.

Garfeild
22nd April 2008, 18:40
I have tried this. But it doesn't work.


QVector<QTextLength> columnWidth;
for(int i=0; i<15; i++)
{
if(i==0)
columnWidth.append(QTextLength::QTextLength(QTextL ength::FixedLength,174));
if(i>0 && i<10)
columnWidth.append(QTextLength::QTextLength(QTextL ength::FixedLength, 42.25));
else if(i>9 && i<12)
columnWidth.append(QTextLength::QTextLength(QTextL ength::FixedLength, 118));
else if(i==12 || i==14)
columnWidth.append(QTextLength::QTextLength(QTextL ength::FixedLength, 100));
else if(i==13)
columnWidth.append(QTextLength::QTextLength(QTextL ength::FixedLength, 70));
}

QTextTableFormat numberTableFormat;
numberTableFormat.setBorder(0);
numberTableFormat.setCellSpacing(-1);
numberTableFormat.setColumnWidthConstraints(column Width);
QBrush brush(Qt::black);
numberTableFormat.setBorderBrush(brush);
numberTableFormat.setBorderStyle(QTextFrameFormat: :BorderStyle_Solid);
//UPDATE
numberTableFormat.setWidth(824.25);

QTextTable *mainTable = cursor.insertTable(32, 15, numberTableFormat);
//SECOND OPTION:
//mainTable->format().setWidth(824.25);
cursor = mainTable->cellAt(0,2).firstCursorPosition();
QTextFrameFormat tempFormat = cursor.currentFrame()->frameFormat();
tempFormat.setBorder(1);
cursor.insertFrame(tempFormat);
cursor.insertText("1");

wysota
22nd April 2008, 20:23
Try building on this code:

QTextTableFormat tfmt;
tfmt.setBorder(0);
QTextCursor cursor(doc);
QTextTable *table = cursor.insertTable(1, 2, tfmt);
cursor = table->cellAt(0,0).firstCursorPosition();
tfmt.setBorder(1);
QTextTable *inner = cursor.insertTable(1,1, tfmt);
inner->cellAt(0,0).firstCursorPosition().insertText("blabla2");
table->cellAt(0,1).firstCursorPosition().insertText("blabla");

Garfeild
22nd April 2008, 21:09
Thnx! It's working.

Garfeild
25th April 2008, 20:49
I have new problem. I need to set cell height. I have tried to use QTextFrameFormat::setHeight(qreal height) but cells haven't resized. Also i tried to set height for whole table.

patrik08
26th April 2008, 10:32
I have new problem. I need to set cell height. I have tried to use QTextFrameFormat::setHeight(qreal height) but cells haven't resized. Also i tried to set height for whole table.

is not possibel!
only if you subclass QAbstractTextDocumentLayout and on a protected function


void QAbstractTextDocumentLayout::drawInlineObject ( QPainter * painter, const QRectF & rect, QTextInlineObject object, int posInDocument, const QTextFormat & format ) [virtual protected]
Called to draw the inline object object on the given painter within the rectangle specified by rect using the text format specified by format. posInDocument specifies the position of the object within the document.

you can insert all your elements ... structure and draw method...

only Cell_Width is possibel to set ... if you work on layer based
table are not needet have a look on http://code.google.com/p/fop-miniscribus/
and his xhtml export .... !not IE





qreal QVimedit::Get_Cell_Width( QTextTableFormat TableFormat , int position )
{
qreal notfound = 0;
QVector<QTextLength> constraints = TableFormat.columnWidthConstraints();
for (int i = 0; i < constraints.size(); ++i) {
if (i == position) {
QTextLength langecell = constraints.value(i);
if (langecell.type() == QTextLength::FixedLength) {
return langecell.rawValue();
}

}
}
return notfound;
}





to insert other parameter to QTextBlock, QTextTableFormat , QTextImageFormat ... you can use QTextFormat::property
i use this to save image data .... and i can save doc and image on not GUI QThread whitout use QPixmap.


QTextBlock::iterator de;
.............
Pics = block.toImageFormat();
if (Pics.isValid()) {
const QString hrefadress = Pics.name();
QString titles;
QVariant inline_img_info = Pics.property(100);
if (!inline_img_info.isNull()) {
SPics pic = inline_img_info.value<SPics>();
}
}