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. QComboBox long item text breaks layout
QtWS25 Last Chance

QComboBox long item text breaks layout

Scheduled Pinned Locked Moved Unsolved General and Desktop
qscrollareaqcomboboxqformlayout
4 Posts 3 Posters 4.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.
  • T Offline
    T Offline
    the_
    wrote on 1 Jun 2016, 14:23 last edited by
    #1

    Hi folks,

    I am a little bit stuck with this. I have a input form that contains a lot of input fields (QTextEdit, QLinEdit, QComboBox) each with a description label in a QFormLayout. The layout is in a QScrollArea.
    As long as the text of the items in any QComboBox is not too long, all looks fine, the QScrollArea grows vertically as it should be.
    But if the text is long (lets say, very long), it grows horizontally too.

    Is there a way to

    • have linebreaks inside a QComboBox?
    • avoid that the QScrollArea grows in horizontal direction?
    • or limit the width of the QComboBox itself so it displays elided text, but the popup should display the full text?

    Thanks for your inputs :)

    -- No support in PM --

    1 Reply Last reply
    0
    • R Offline
      R Offline
      Rondog
      wrote on 1 Jun 2016, 20:33 last edited by Rondog 6 Jan 2016, 20:37
      #2

      One idea that should work (assuming it is acceptable to you) is to have the text in the combo box shorted to some maximum length but keep the complete text stored as item data.

      For example,

      QComboBox *cb = new QComboBox(this);
      
      QString text("Some really really long text that I want to display in the combo box");
      QString display_text = Text_Shorten_Function(text); // "Some really ... combo box"
      
      cb->addItem(display_text,text); // second parameter is for 'user data'
      
      // get displayed text
      QString displayed_text = cb->currentText();
      
      // get actual text and not what is displayed
      QString real_text = cb->itemData(cb->currentIndex(),Qt::UserRole);
      

      Another option would be to set a maximum width of the QComboBox. This is sure to work but I find when you do stuff like this you end up with other problems. Preferably this is left as the last option when you exhaust all others.

      Note: You can set the tool tip text to show the full string if you think there might be duplicates or problems identifying the contents. If you need to use options like 'findText()' use 'findData()' instead.

      T 1 Reply Last reply 2 Jun 2016, 12:16
      0
      • S Offline
        S Offline
        SGaist
        Lifetime Qt Champion
        wrote on 1 Jun 2016, 20:36 last edited by
        #3

        Hi,

        You could use a custom QStyledItemDelegate that does the eliding.

        Hope it helps

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply
        0
        • R Rondog
          1 Jun 2016, 20:33

          One idea that should work (assuming it is acceptable to you) is to have the text in the combo box shorted to some maximum length but keep the complete text stored as item data.

          For example,

          QComboBox *cb = new QComboBox(this);
          
          QString text("Some really really long text that I want to display in the combo box");
          QString display_text = Text_Shorten_Function(text); // "Some really ... combo box"
          
          cb->addItem(display_text,text); // second parameter is for 'user data'
          
          // get displayed text
          QString displayed_text = cb->currentText();
          
          // get actual text and not what is displayed
          QString real_text = cb->itemData(cb->currentIndex(),Qt::UserRole);
          

          Another option would be to set a maximum width of the QComboBox. This is sure to work but I find when you do stuff like this you end up with other problems. Preferably this is left as the last option when you exhaust all others.

          Note: You can set the tool tip text to show the full string if you think there might be duplicates or problems identifying the contents. If you need to use options like 'findText()' use 'findData()' instead.

          T Offline
          T Offline
          the_
          wrote on 2 Jun 2016, 12:16 last edited by
          #4

          @Rondog

          I was already thinking of setting the maximum width for each combobox but when creating the comboboxes I dont know the width of the scrollareas viewport.

          @SGaist
          Will try that, seems to be the more nice solution :)

          Thanks

          -- No support in PM --

          1 Reply Last reply
          0

          1/4

          1 Jun 2016, 14:23

          • Login

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