Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Auto line-break with QLabel within QTableWidget
Forum Update on Monday, May 27th 2025

Auto line-break with QLabel within QTableWidget

Scheduled Pinned Locked Moved Solved General and Desktop
qt 5qtablewidgetqlabelwordwrap
3 Posts 2 Posters 2.4k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    silverfox
    wrote on 4 Jun 2021, 23:11 last edited by silverfox 6 Apr 2021, 23:12
    #1

    I'm working on a QTableWidget with some QLabel inside it, but I need the QLabel to line-break automatically. I've tried setWordWrap from this question:

    QVBoxLayout *newL = new QVBoxLayout;
    QTableWidget * tab = new QTableWidget; tab->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
    newL->addWidget(tab);
    
    tab->setRowCount(1);
    tab->setColumnCount(1);
    
    QLabel *lb = new QLabel; lb->setText("testing");
    lb->setWordWrap(true);
    tab->setCellWidget(0,0,lb);
    
    QWidget *window = new QWidget;
    window->setLayout(newL);
    window->show();
    

    Problems

    1. If one word is really lengthy, the wordWrap doesn't move to a new line. Instead, it just expand into outer region:
    lb->setText("testtesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttestawd");
    

    alt text
    2. If there're multiple lines (more than 2), the QTableWidget doesn't create more lines, so the lower lines are not shown:

    lb->setText("test\ntest\ntest\ntest\ntest\ntest");
    

    alt text
    3. Even if there're only spaces between words, the QTableWidget still doesn't expand more than 2 lines:

    lb->setText("test              test        test       test           test        test       test");
    

    alt text
    Questions

    1. For the first problem, I'd like the text to switch to new line exactly at the point that the text go to outer region, even if there're no \n character (same way with MS Word).
    2. For the second and third problem, I'd like the QWidgetTable to provide enough space to show all the text.

    Please help. Thanks in advance.

    1 Reply Last reply
    0
    • C Offline
      C Offline
      ChrisW67
      wrote on 5 Jun 2021, 05:19 last edited by
      #2

      @silverfox said in Auto line-break with QLabel within QTableWidget:

      For the first problem, I'd like the text to switch to new line exactly at the point that the text go to outer region, even if there're no \n character (same way with MS Word).

      QLabel wraps at word boundaries, e.g. the transition from alphanumerics to whitespace, to fit the width available. If you want to change that behaviour then you need to subclass QLabel and implement your own QWidget::paintEvent(), sizeHint(), and possibly other things.

      QTextEdit may be an alternate choice.

      For the second and third problem, I'd like the QWidgetTable to provide enough space to show all the text.

      The default behaviour is that the table widget, through its QHeaderViews, dictates the size of the cell and therefore the space available for the widget you put in it to use. This ignores any suggested size that the widget might provide. You can change that behaviour:

      // All rows in table
      tab->verticalHeader()->setSectionResizeMode(QHeaderView::ResizeToContents);
      // one specific row
      tab->setSectionResizeMode(1, QHeaderView::ResizeToContents);
      

      That said, I have no idea why you are putting a label widget into a table cell just to display text. The QTableWidget is quite capable of displaying text from its model on its own. In that case you can use a QItemDelegate to thoroughly customise the appearance of text coming from the model.

      S 1 Reply Last reply 5 Jun 2021, 06:19
      1
      • C ChrisW67
        5 Jun 2021, 05:19

        @silverfox said in Auto line-break with QLabel within QTableWidget:

        For the first problem, I'd like the text to switch to new line exactly at the point that the text go to outer region, even if there're no \n character (same way with MS Word).

        QLabel wraps at word boundaries, e.g. the transition from alphanumerics to whitespace, to fit the width available. If you want to change that behaviour then you need to subclass QLabel and implement your own QWidget::paintEvent(), sizeHint(), and possibly other things.

        QTextEdit may be an alternate choice.

        For the second and third problem, I'd like the QWidgetTable to provide enough space to show all the text.

        The default behaviour is that the table widget, through its QHeaderViews, dictates the size of the cell and therefore the space available for the widget you put in it to use. This ignores any suggested size that the widget might provide. You can change that behaviour:

        // All rows in table
        tab->verticalHeader()->setSectionResizeMode(QHeaderView::ResizeToContents);
        // one specific row
        tab->setSectionResizeMode(1, QHeaderView::ResizeToContents);
        

        That said, I have no idea why you are putting a label widget into a table cell just to display text. The QTableWidget is quite capable of displaying text from its model on its own. In that case you can use a QItemDelegate to thoroughly customise the appearance of text coming from the model.

        S Offline
        S Offline
        silverfox
        wrote on 5 Jun 2021, 06:19 last edited by
        #3

        @ChrisW67
        Thanks! It work like a charm.

        For the reason to why I was using QLabel, well I'm mostly testing for stuffs. I'll switch it up later.

        1 Reply Last reply
        0

        2/3

        5 Jun 2021, 05:19

        • Login

        • Login or register to search.
        2 out of 3
        • First post
          2/3
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved