[SOLVED]Making QHBoxLayout scrollable



  • 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?



  • 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



  • 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 ?


  • Moderators

    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..



  • [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 ;)



  • 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?


  • Moderators

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



  • thanks of all.my problem has solved.


Log in to reply
 

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