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]Making QHBoxLayout scrollable
Forum Updated to NodeBB v4.3 + New Features

[SOLVED]Making QHBoxLayout scrollable

Scheduled Pinned Locked Moved General and Desktop
8 Posts 3 Posters 4.5k 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.
  • M Offline
    M Offline
    mehdi.nine
    wrote on last edited by
    #1

    I need to add Textedit in run-time to a QHBoxLayout so I need add scroll to it. for this purpose I need to add QHBoxLayout to a parent (here:Qt: making QHBoxLayout scrollable) and add parent to Qscrollarea.

    This is my code:
    @
    QHBoxLayout *onelinewidget = new QHBoxLayout();
    QLabel l = new QLabel("slaam");
    QList<QObject
    > list;
    list.append(l);
    ui->widget->children().append(list);
    @

    But it has following error:
    @
    C:\Qt\Qt5.0.1\Tools\QtCreator\bin\untitled4\mainwindow.cpp:43: error: passing
    'const QObjectList {aka const QList<QObject*>}' as 'this' argument of 'void
    QList<T>::append(const QList<T>&) [with T = QObject*]' discards qualifiers
    [-fpermissive]
    @
    Any ideas?

    1 Reply Last reply
    0
    • M Offline
      M Offline
      Mario84
      wrote on last edited by
      #2

      I suppose ui->widget is your scrollarea, so you should do something like this:
      @
      ui->widget->setLayout(onelinewidget);
      foreach(QWidget *p, list) //use a list of QWidgets instead of QObjects
      {
      onelinewidget->addWidget(p);
      }
      @

      You cannot modify the list returned by QObject::children() because it is const

      1 Reply Last reply
      0
      • M Offline
        M Offline
        mehdi.nine
        wrote on last edited by
        #3

        Mr.Universe you are right. but i want add layout to the widget until i can add widget to the scroll area
        see this:

        @
        for (int lineNumber = 0; lineNumber < 2; ++lineNumber)
        {
        QWidget *oneLineWidget = new QWidget(this);
        QHBoxLayout *oneLineWidgetLayout = new QHBoxLayout();
        { //added these brackets just for the ease of reading.
        QLabel *labFirst = new QLabel(tr("first label"), oneLineWidget);
        QLabel *labSecond = new QLabel(tr("second label"), oneLineWidget);
        QPushButton *bFirst = new QPushButton(tr("first button"), oneLineWidget);
        QPushButton *bSecond = new QPushButton(tr("second button"), oneLineWidget);

                oneLineWidgetLayout->addWidget(labFirst);
                oneLineWidgetLayout->addWidget(labSecond);
                oneLineWidgetLayout->addWidget(bFirst);
                oneLineWidgetLayout->addWidget(bSecond);
        
        
        
        
            }
            oneLineWidget->setLayout(oneLineWidgetLayout);
        
            ui->verticalLayout->addWidget(oneLineWidget);
        }
        

        @

        when the lines increase i need to use scroll area@ but i can't add layout to the scroll area directly@ for this purpose i need add layout to a parent widget like Qwidget and add it to the scroll area.
        do you have idea for this ?

        1 Reply Last reply
        0
        • raven-worxR Offline
          raven-worxR Offline
          raven-worx
          Moderators
          wrote on last edited by
          #4

          You need another QWidget and set a QVBoxLayout to it and add there "oneLineWidget" instances one by one in your loop.
          Add the created widget then as content to your scrollarea.

          Hope i got you right..

          --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
          If you have a question please use the forum so others can benefit from the solution in the future

          1 Reply Last reply
          0
          • M Offline
            M Offline
            Mario84
            wrote on last edited by
            #5

            [quote author="mehdi.nine" date="1366274211"]but i can't add layout to the scroll area directly[/quote]

            That's correct: QScrollArea takes exactly ONE widget...
            So you have to create a container widget, give it a vertical layout and add your oneLineWidgets to this layout.
            (You could also omit oneLineWidget and add oneLineWidgetLayout directly to the vertical layout of the container widget using QBoxLayout::addLayout() )

            *Edit:
            Of course this is the same as what raven-worx said... took me too long to write so I didn't see it ;)

            1 Reply Last reply
            0
            • M Offline
              M Offline
              mehdi.nine
              wrote on last edited by
              #6

              thanks for your replies. but my problem is that i can't add any layout to Qwidget. Qwidget only have children.append() function and don't have addlayout. how i add my layouts to Qwidget until i can add Qwidget to scroll area?

              1 Reply Last reply
              0
              • raven-worxR Offline
                raven-worxR Offline
                raven-worx
                Moderators
                wrote on last edited by
                #7

                QWidget::setLayout()
                and
                QScrollArea::setWidget()

                --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
                If you have a question please use the forum so others can benefit from the solution in the future

                1 Reply Last reply
                0
                • M Offline
                  M Offline
                  mehdi.nine
                  wrote on last edited by
                  #8

                  thanks of all.my problem has solved.

                  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