Vertical scroll area containing QLabel:QLineEdit pairs

  • Hello again everyone,

    I have an arbitrary number of text items which i want to present on screen so the user can edit them. I want to create a vertically scrolling area in my dialog box, which will contain a series of pairs, each pair consisting of a QLabel and a QLineEdit., which widget classes should i use for this?

    Thanks again as always!

  • Solved it =) anyway, keeping this as a reference that could help anyone else trying to do the same or similar. The basic idea is that you're creating a vertical layout, inside which you add a series of horizontal layouts, each containing a label and a line edit. The vertical layout is a direct child of the 'scrollAreaWidgetContents' widget that Qt Designer creates when you add a QScrollArea to your dialog.

    The code goes something like this:

      /* assuming you have a QLineEdit * pointer array named 'line_entries' of size 'num_entries' */
      int i, label_width;
      char buf [64];
      QLabel *items_label;
      QVBoxLayout *items_layout = new QVBoxLayout (scrollAreaWidgetContents); /* this name is decided by uic when it compiles your .ui XML file */
      items_layout->setSpacing (3); /* adjust to taste */
      QHBoxLayout *hbox_layout;
      items_label = new QLabel ("Rank 0000: "); /* longest possible text for left-hand label */
      items_label->updateGeometry ();
      label_width = items_label->sizeHint ().width ();
      delete items_label;
      for (i = 0; i < num_entries; i++) {
        /* label */
        sprintf (buf, "Rank %d: ", i + 1); /* yeah good old standard C... so bite me :D */
        hbox_layout = new QHBoxLayout ();
        items_label = new QLabel (buf);
        items_label->setMinimumWidth (label_width);
        hbox_layout->addWidget (items_label);
        /* line entry */
        line_entries [i] = new QLineEdit ();
        hbox_layout->addWidget (line_entries [i]);
        items_layout->addLayout (hbox_layout);

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.