Results 1 to 13 of 13

Thread: Getting HTML table cell width

  1. #1
    Join Date
    Jan 2013
    Posts
    23
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Getting HTML table cell width

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

    Qt Code:
    1. QString text;
    2. text="<table id=\"tbl1\" width=\"100%\" cellspacing=\"0\" class=\"tbl\">";
    3. text+=("<tr><th colspan=\"2\" rowspan=\"2\">2</th><th>3</th><th>2</th></tr>");
    4. text+=("<tr><th>3</th><th>8</th><th>9</th></tr>");
    5. text+=("<tr><td>4</td><td>5</td><td>6</td></tr>");
    6. text+=("<tr><td>7</td><td>8</td><td>9</td></tr>");
    7. text+=("<tr><td>10</td><td>11</td><td>12</td><td>8</td><td>9</td></tr></table>");
    To copy to clipboard, switch view to plain text mode 

    But I can not get cell width. How can I do that?
    Last edited by sipahi; 31st January 2013 at 14:41.

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Getting HTML table cell width

    Why are you asking this on a Qt forum? This seems like HTML related issue.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    May 2008
    Location
    Tripoli Libya
    Posts
    70
    Thanks
    10
    Thanked 8 Times in 8 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Getting HTML table cell width

    Quote Originally Posted by wysota View Post
    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:


    Quote Originally Posted by sipahi View Post
    I am trying to create HTML table and then get cells width.
    Creating table was done:

    Qt Code:
    1. QString text;
    2. text="<table id=\"tbl1\" width=\"100%\" cellspacing=\"0\" class=\"tbl\">";
    3. text+=("<tr><th colspan=\"2\" rowspan=\"2\">2</th><th>3</th><th>2</th></tr>");
    4. text+=("<tr><th>3</th><th>8</th><th>9</th></tr>");
    5. text+=("<tr><td>4</td><td>5</td><td>6</td></tr>");
    6. text+=("<tr><td>7</td><td>8</td><td>9</td></tr>");
    7. text+=("<tr><td>10</td><td>11</td><td>12</td><td>8</td><td>9</td></tr></table>");
    To copy to clipboard, switch view to plain text mode 

    But I can not get cell width. How can I do that?
    http://www.w3schools.com/html/default.asp
    Last edited by alrawab; 31st January 2013 at 23:48.

  4. #4
    Join Date
    Jan 2013
    Posts
    23
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Getting HTML table cell width

    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:
    Qt Code:
    1. document.getElementById('tbl_id').rows[0].cells[0].offsetWidth
    To copy to clipboard, switch view to plain text mode 
    Can I use this?
    Last edited by sipahi; 1st February 2013 at 02:24.

  5. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Getting HTML table cell width

    You can use whatever you wish, just remember the table has no physical attributes until the page is rendered somewhere.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  6. #6
    Join Date
    Jan 2013
    Posts
    23
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Getting HTML table cell width

    Can you explain me how can I use this javascript code. I always get 0 as .

  7. #7
    Join Date
    May 2008
    Location
    Tripoli Libya
    Posts
    70
    Thanks
    10
    Thanked 8 Times in 8 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Getting HTML table cell width

    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

  8. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Getting HTML table cell width

    Quote Originally Posted by sipahi View Post
    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.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  9. #9
    Join Date
    Jan 2013
    Posts
    23
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Getting HTML table cell width

    I changed my code because it seems impossible for me.
    Now I created table using QTextTable.

    Qt Code:
    1. QTextDocument *document=new QTextDocument(this);
    2. QTextCursor cursor(document);
    3. cursor.movePosition(QTextCursor::Start);
    4.  
    5. QTextTableCellFormat cellFormat;
    6. cellFormat.setLeftPadding(7);
    7. cellFormat.setRightPadding(7);
    8.  
    9. QBrush blackBrush(Qt::SolidPattern);
    10. QTextTableFormat tableFormat;
    11. tableFormat.setAlignment(Qt::AlignLeft);
    12. tableFormat.setBorderBrush(blackBrush);
    13. tableFormat.setBorder(.5);
    14. tableFormat.setCellSpacing(0);
    15. tableFormat.setBorderStyle(QTextFrameFormat::BorderStyle_Solid);
    16. tableFormat.setAlignment(Qt::AlignLeft);
    17. tableFormat.setWidth(QTextLength(QTextLength::PercentageLength, 100));
    To copy to clipboard, switch view to plain text mode 

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

  10. #10
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Getting HTML table cell width

    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.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  11. #11
    Join Date
    Jan 2013
    Posts
    23
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Getting HTML table cell width

    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.
    Last edited by sipahi; 2nd February 2013 at 11:44.

  12. #12
    Join Date
    Jan 2009
    Location
    The Netherlands and Spain
    Posts
    150
    Thanks
    6
    Thanked 18 Times in 18 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Getting HTML table cell width

    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).

  13. #13
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Getting HTML table cell width

    Quote Originally Posted by sipahi View Post
    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).
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. .csv format cell width
    By bostero22 in forum Qt Programming
    Replies: 1
    Last Post: 4th September 2010, 00:06
  2. Replies: 1
    Last Post: 25th May 2010, 15:44
  3. QTreeView cell as table
    By ^NyAw^ in forum Qt Programming
    Replies: 2
    Last Post: 26th June 2009, 10:45
  4. Setting cell width in QHBoxLayout
    By EricF in forum Qt Programming
    Replies: 5
    Last Post: 13th December 2007, 16:42
  5. QTextImageFormat HTML not write <img width= height>
    By patrik08 in forum Qt Programming
    Replies: 9
    Last Post: 8th February 2007, 02:59

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.