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. [SOLVED] QCombobox size should fit in the largest string

[SOLVED] QCombobox size should fit in the largest string

Scheduled Pinned Locked Moved General and Desktop
4 Posts 3 Posters 1.8k Views 1 Watching
  • 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
    smita30apr
    wrote on last edited by
    #1

    Hi,

    I have a QCombobox which contains strings of different length. And I want the length of the combobox should be resized with the largest string. I do it like this:

    @Action_combo_box::Action_combo_box(QStringList char_list, const Scene *scene, QWidget *parent)
    : QComboBox(parent)
    , m_scene(scene)
    {
    qDebug()<<"Original width"<<width();
    set_width(char_list);
    addItems(char_list);
    }

    void Action_combo_box::set_width(const QStringList &char_list){
    //find the lengthiest string
    QString longest;
    for (int i = 0; i < char_list.length(); i++) {
    QString str = char_list.value(i);
    if (str.length() > longest.length())
    longest = str;
    }

    QFontMetrics font_metrics(fontMetrics());
    int width = font_metrics.width(longest);
    qDebug()<<"Longest str width"<<width;
    setMinimumWidth(width);
    

    }@

    The above code does not work and my largest string is always clipped.

    My output is:
    Original width 100
    Longest str width 49

    which looks strange to me.
    Any help??

    1 Reply Last reply
    0
    • J Offline
      J Offline
      JulienMaille
      wrote on last edited by
      #2

      Did you had a look at:
      "setSizeAdjustPolicy":http://qt-project.org/doc/qt-5/qcombobox.html#sizeAdjustPolicy-prop

      with
      "QComboBox::AdjustToMinimumContentsLength":http://qt-project.org/doc/qt-5/qcombobox.html#SizeAdjustPolicy-enum

      1 Reply Last reply
      0
      • S Offline
        S Offline
        smita30apr
        wrote on last edited by
        #3

        Yes I did. That did not help.

        I solved the problem with

        setMinimumWidth(minimumSizeHint().width());

        1 Reply Last reply
        0
        • A Offline
          A Offline
          arsinte_andrei
          wrote on last edited by
          #4

          can you mark this as [SOLVED] in the first post title that anyone will know that it has been fixed.

          1 Reply Last reply
          0

          • Login

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