PDA

View Full Version : tab stops ?



ixM
26th May 2009, 19:13
Hi there,

I'm having some trouble with QTextDocument, QTextCursor,...

I'm trying to generate a QTextDocument using data from a database. So far it easy but it becomes harder when I try to use tab stops (maybe it's not the right name) to achieve something like this : www.ixamaxi.be/images/problem4.png


QTextDocument * TextDocument = new QTextDocument;
TextDocument->setTextWidth(200.0);
TextDocument->setPageSize(QSize(200, 200));

QSqlQuery Query = GetSql()->Query();

QString QueryString;

QueryString += "SELECT books.book_author, books.book_title, books.book_price ";
QueryString += "FROM catalog_book_list ";
QueryString += "LEFT JOIN books ON books.book_id = catalog_book_list.book_id ";
QueryString += "WHERE catalog_book_list.catalog_id = 1 ";
QueryString += "ORDER BY catalog_book_list.cat_book_id ASC";

Query.exec(QueryString);

QTextCursor TextCursor(TextDocument->rootFrame());

QTextList * TextList = TextCursor.insertList(QTextListFormat::ListDecimal );

QTextBlock TextBlock = TextList->item(0);

TextCursor = QTextCursor(TextBlock);

QTextCharFormat Default;
Default.setProperty(QTextFormat::CssFloat, "right");

QTextCharFormat Author;
Author.setFontWeight(QFont::Bold);

QTextCharFormat Title;
Title.setFontUnderline(true);

QTextBlockFormat BlockFormat = TextBlock.blockFormat();

QTextOption TextOption = TextDocument->defaultTextOption();
TextOption.setFlags(QTextOption::ShowLineAndParagr aphSeparators | QTextOption::ShowTabsAndSpaces);

TextOption.setWrapMode(QTextOption::WrapAtWordBoun daryOrAnywhere);

QList<QTextOption::Tab> List;

QTextOption::Tab Tab = QTextOption::Tab();
Tab.type = QTextOption::DelimiterTab;
Tab.delimiter = QChar::ParagraphSeparator;
List.append(Tab);

TextDocument->setDefaultTextOption(TextOption);

while(Query.next()) {
TextCursor.setBlockFormat(BlockFormat);
QTextFrame * mainFrame = TextCursor.currentFrame();

TextCursor.setCharFormat(Default);
TextCursor.insertText(" ");

TextCursor.setCharFormat(Author);
TextCursor.insertText(Query.value(0).toString());

TextCursor.setCharFormat(Default);
TextCursor.insertText(" ");

TextCursor.setCharFormat(Title);
TextCursor.insertText(Query.value(1).toString());

TextCursor.setCharFormat(Default);
TextCursor.insertText("\t");
TextCursor.insertText(Query.value(2).toString() +" "+ QString(QChar(8364)));

TextCursor.setCharFormat(Default);

TextCursor = mainFrame->lastCursorPosition();

if(Query.at() != Query.size() - 1)
TextCursor.insertBlock();
}

This is the result I'm getting : http://www.ixamaxi.be/images/problem7.png

The documentation is not very "big" concerning this particular subject.

Any help would be appreciated!

Thank you very much,

Regards,

ixM

fullmetalcoder
27th May 2009, 14:01
I think QTextOption::tabArray() (http://doc.trolltech.com/latest/qtextoption.html#tabArray) would be more interesting to achieve the effect of your first picture, if I understood it well.
(http://doc.trolltech.com/latest/qtextoption.html#tabArray)

ixM
27th May 2009, 17:19
Hi,

Thank you for your answer.

Could you maybe explain me what those distance are ? I've already tried that function to and well, if I put 300 for the first tab for example, the first tab will be longer than the others (which are 80 I think). But this doesn't help since, it doesn't allow me to clampse the text to the right border of the text.

Plus I wasn't able to figure out how to set the QTextDocument format in order that it would have the dimension of an A4 sheet.

Thanks a lot !

ixM

fullmetalcoder
27th May 2009, 19:05
But this doesn't help since, it doesn't allow me to clampse the text to the right border of the text.
I'm afraid this is not the purpose of tabs. You'll probably have to play with tables/frames and alignment attributes to achieve that or you'll have to work at a lower level (QAbstractTextDocumentLayout for instance) and tweak the behavior of the document framework so that tabs mean what you want them to but that won't be easy.


Plus I wasn't able to figure out how to set the QTextDocument format in order that it would have the dimension of an A4 sheet.
QTextDocument::setPageSize(const QSize&) maybe?

ixM
28th May 2009, 14:11
Hi,

Thank you for your answer, I'll probably have to do what you've suggested.

Concerning setPageSize() (which I had already spotted) I can not figure out what units it uses (like a lot of functions using "device units" for example...). Any idea ?

Thank you :)