PDA

View Full Version : Getting HTML table cell width



sipahi
31st January 2013, 12:49
I am trying to create HTML table and then get cells width.
Creating table was done:


QString text;
text="<table id=\"tbl1\" width=\"100%\" cellspacing=\"0\" class=\"tbl\">";
text+=("<tr><th colspan=\"2\" rowspan=\"2\">2</th><th>3</th><th>2</th></tr>");
text+=("<tr><th>3</th><th>8</th><th>9</th></tr>");
text+=("<tr><td>4</td><td>5</td><td>6</td></tr>");
text+=("<tr><td>7</td><td>8</td><td>9</td></tr>");
text+=("<tr><td>10</td><td>11</td><td>12</td><td>8</td><td>9</td></tr></table>");

But I can not get cell width. How can I do that?

wysota
31st January 2013, 21:38
Why are you asking this on a Qt forum? This seems like HTML related issue.

alrawab
31st January 2013, 22:48
Why are you asking this on a Qt forum? This seems like HTML related issue.
maybe used the html table to generate a report ...Qt is charming but still missed effective report tool

Added after 5 minutes:


I am trying to create HTML table and then get cells width.
Creating table was done:


QString text;
text="<table id=\"tbl1\" width=\"100%\" cellspacing=\"0\" class=\"tbl\">";
text+=("<tr><th colspan=\"2\" rowspan=\"2\">2</th><th>3</th><th>2</th></tr>");
text+=("<tr><th>3</th><th>8</th><th>9</th></tr>");
text+=("<tr><td>4</td><td>5</td><td>6</td></tr>");
text+=("<tr><td>7</td><td>8</td><td>9</td></tr>");
text+=("<tr><td>10</td><td>11</td><td>12</td><td>8</td><td>9</td></tr></table>");

But I can not get cell width. How can I do that?
http://www.w3schools.com/html/default.asp

sipahi
31st January 2013, 23:40
I am writing a reporting tool with Qt and data are in table. I make table with HTML but I can not get cell width
I found a javascript code for my problem:
document.getElementById('tbl_id').rows[0].cells[0].offsetWidth
Can I use this?

wysota
1st February 2013, 00:04
You can use whatever you wish, just remember the table has no physical attributes until the page is rendered somewhere.

sipahi
1st February 2013, 01:27
Can you explain me how can I use this javascript code. I always get 0 as .

alrawab
1st February 2013, 01:43
if you design a report you dont need to invoke the html table its just a container populated with what you want for printing or exporting it to pdf or any formate

wysota
1st February 2013, 11:19
Can you explain me how can I use this javascript code. I always get 0 as .

I can't, because in my opinion it doesn't make sense to use it in this context. Go and ask the person who told you to use it.

sipahi
2nd February 2013, 07:01
I changed my code because it seems impossible for me.
Now I created table using QTextTable.



QTextDocument *document=new QTextDocument(this);
QTextCursor cursor(document);
cursor.movePosition(QTextCursor::Start);

QTextTableCellFormat cellFormat;
cellFormat.setLeftPadding(7);
cellFormat.setRightPadding(7);

QBrush blackBrush(Qt::SolidPattern);
QTextTableFormat tableFormat;
tableFormat.setAlignment(Qt::AlignLeft);
tableFormat.setBorderBrush(blackBrush);
tableFormat.setBorder(.5);
tableFormat.setCellSpacing(0);
tableFormat.setBorderStyle(QTextFrameFormat::Borde rStyle_Solid);
tableFormat.setAlignment(Qt::AlignLeft);
tableFormat.setWidth(QTextLength(QTextLength::Perc entageLength, 100));

I noticed setColumnWidthConstraints function for column width. But cell width changes according to text.
Is there a way to get cell width in QTextTable?

wysota
2nd February 2013, 09:03
You can't get cell width of any table before the rendering mechanism knows what font is used, what is the content of all other cells in the table and so on. It doesn't matter whether you are using HTML or QTextDocument, the "problem" is the same. You need to trigger the layouting functionality of the engine you are using.

sipahi
2nd February 2013, 10:03
thank you for your patient and assistance @wysota.
I am new in Qt and I do not know completely what Qt can do.

Basically I am trying to create two separate tables which have exactly same size and number of columns.
And then print out two tables. But contents of tables are different. Because of that widths of columns become different.
And also I don not know how can I trigger the layouting functionality.

boudie
2nd February 2013, 12:12
In your first post you created a table in html with a width of 100%.
You can do the same for every cell in a row. Set them all to a fixed pixel value or percentage, except for one column. That one will fill up the remaining space in a row.
Also, you don't have to do it for every row, as all cells in a column will, normally, have the same width.

All this can be done with an element like <td width=100> or using css stylesheets. You know how to use stylesheets as you used it in the original table definition, class="tbl".

The main difference in this approach is that you now set a width instead of trying to get it from a renderer that is not yet in action (as others told you before).

wysota
2nd February 2013, 12:28
I am new in Qt and I do not know completely what Qt can do.
It's not about Qt. HTML holds information about the logical structure of a document and not its physical attributes. Only when the document is displayed in some kind of browser, it gains those physical attributes but only for particular input parameters like the browser's default font, window size, device resolution, CSS definitions and so on.

Basically I am trying to create two separate tables which have exactly same size and number of columns.
And then print out two tables. But contents of tables are different. Because of that widths of columns become different.


And also I don not know how can I trigger the layouting functionality.
It depends on the document engine you are using. For HTML you can use WebKit, for QTextDocument you can use QAbstractTextDocumentLayout and QTextDocument itself. You can iterate over all rows in the two tables you have, find a maximum width cells in your particular column occupy and then set that width on those columns explicitly (if the engine allows it).