PDA

View Full Version : resizing a qgraphicstextitem



dreamer
21st May 2008, 10:54
i have a QgraphicsTextItem containing a QTextTable.
How can i resize the QGraphicsTextitem(like the object QGraphicsRectItem that use the function setRect())????

i know it has the function boundingRect() but i don't know how to resize it......

patrik08
21st May 2008, 11:25
you must only chance hi = hight & wi = widht

and update item



QRectF FloatDiagram::boundingRect() const
{
QRectF now = QGraphicsTextItem::boundingRect(); /* actual setting hi - wi */
return QGraphicsTextItem::boundingRect().adjusted(0,0, wi - now.width(), hi - now.height());
}



but alert you can not go smaller a qgraphicstextitem document rect!



QRectF TextWriter::boundingRect() const
{
if (_d) { /* _d = qtextdocument */
_d->adjustSize();
QAbstractTextDocumentLayout *Layout = _d->documentLayout();
return Layout->frameBoundingRect(_d->rootFrame());
} else {
return QRectF();
}
}



and qgraphicstextitem can only move xy if you point the mouse on document boundingRect()

this is one reason for my rewrite from qgraphicstextitem
http://www.qt-apps.org/content/show.php/GraphicsViewEdit+Layer?content=80234

remember parent event from mouse comming only if qtextdocument rect containt this area...

dreamer
21st May 2008, 16:59
i write the code to set the column width, but it isn't applied to the table....just the width applied in the constructor works......

this is my constructor:


for(int i=0;i<2;i++)
columnWidthVector.append(QTextLength(QTextLength:: FixedLength,30));
tableformat = new QTextTableFormat();
tableformat->setBorder(1);
tableformat->setCellSpacing(0);
tableformat->setCellPadding(0);
tableformat->setColumnWidthConstraints(columnWidthVector);
table = cursor->insertTable(2,2,*tableformat);


then i update the column width:


int pos=X;//some position
QVector<QTextLength> constraints =tableformat->columnWidthConstraints();
for (int i = 0; i < constraints.size(); i++)
if (i == currCell.column())
constraints.replace(i,QTextLength(QTextLength::Fix edLength,new_width));
tableformat->setColumnWidthConstraints(constraints);


Why i don't see the updated column width in my view???
I'm using the table into a QGraphicsTextItem.............thx

patrik08
22nd May 2008, 00:27
Here this run...

grep table on page
http://fop-miniscribus.googlecode.com/svn/trunk/GraphicsViewEdit/include/mimedataeditor.cpp







void Layoutpainter::SetColumLarge()
{
if (textCursor().currentTable()) {
QTextTableCell existingcell = textCursor().currentTable()->cellAt(textCursor());
QTextTableFormat tbforms = textCursor().currentTable()->format();
int cellcoolcursoris = existingcell.column(); /* int value start from zero */
bool ok;
int LargeSet = QInputDialog::getInteger(0, tr("Set Cell Width"),
tr("Point Length:"),Get_Cell_Width(tbforms,cellcoolcursoris), 1, 2000, 1, &ok);
if (ok && LargeSet > 0) {
QVector<QTextLength> constraints = tbforms.columnWidthConstraints();
for (int i = 0; i < constraints.size(); ++i) {
if (i == cellcoolcursoris) {
constraints.replace(i,QTextLength(QTextLength::Fix edLength, LargeSet));
}
}
tbforms.setColumnWidthConstraints(constraints);
textCursor().currentTable()->setFormat(tbforms);
}
}
}


qreal Layoutpainter::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;
}

dreamer
22nd May 2008, 09:53
I have this three doubt:

1)How can i set the height of each cell? I guess i have to change the cursor font character....

2)Is there any option that gives me the possibility to select a cell in a QTextTable but doesn't give me the possibility to insert Text?

3)I insert a QTextTable into a QGraphicsTextItem throw the function set document. My textItem's bounding rectangle is almost coincident with the textTable inside it. If a select all the table, the cursor position goes out of the table and if I insert a character, the table disappears.
How resolve?

I think that solving the second problem, i will also solve the third one.

patrik08
22nd May 2008, 10:35
1)How can i set the height of each cell? I guess i have to change the cursor font character....


You can set the widht from cell and widht from table , the height from cell depend from block format font and margin left,right,top,bottom QT set default margin 12point any direction ( i dont know why && i not found config to reset 0point ) but you can stop this margin on keyPressEvent check QTextBlock block = C_cursor.block(); margin and reformat
your own margin e padding...




2)Is there any option that gives me the possibility to select a cell in a QTextTable but doesn't give me the possibility to insert Text?



sure ...
keyPressEvent

if (textCursor().currentTable()) {
return;

and
mousepress & release edit cell rectancle like code from
http://www.qt-apps.org/content/show.php/GraphicsViewEdit+Layer?content=80234




3)I insert a QTextTable into a QGraphicsTextItem throw the function set document. My textItem's bounding rectangle is almost coincident with the textTable inside it. If a select all the table, the cursor position goes out of the table and if I insert a character, the table disappears.
How resolve?.

if you resize _doc to table rectangle also QTextDocument + only one table you can only select table

+ _doc->documentLayout()->frameBoundingRect(_doc->rootFrame()) show you rect to stay on item..







_doc = new QTextDocument();
_doc->setHtml(tr("<p>Table<p>"));

QTextFrame *Tframe = _doc->rootFrame();
QTextFrameFormat rootformats = Tframe->frameFormat();
rootformats.setWidth(wi);
rootformats.setBorder(0); /* + margin padding 0*/
Tframe->setFrameFormat(rootformats);
_doc->setPageSize(QSizeF(wi,hi));
DLayout = _doc->documentLayout();



But i suppose you can draw table only on a QGraphicsItem not on QGraphicsTextItem

have you a model QStandardItemModel from table data?
if yes expand this model to Qtextable like
http://doc.trolltech.com/4.2/richtext-calendar.html and draw it on QGraphicsItem paintevent ...

what for data contain this table to not edit?

dreamer
22nd May 2008, 15:43
Doubt dealing with the color of a cell.

When i color a cell's background i call this function:


void QTextCharFormat::setBackground(QBrush);


while i call this other for the table background:


void QTextTableFormat::setBackground(QBrush);


If I color a cell before coloring the whole table, the cell manteins its color....How change this behaviour??(This also happens in your(PATRIk08) program...)