5

I am creating a window dimanica to the downloads list. But the scrollbar does not work and the "widgets children" are "cut".

Where can I be wrong? Thanks.

Source:

    QWidget *central = new QWidget;
    QScrollArea *scroll = new QScrollArea;
    QVBoxLayout *layout = new QVBoxLayout(scroll);
    scroll->setWidget(central);
    scroll->setWidgetResizable(true);

    int i=0;
    while(i<10){
        QWidget *p1 = new QWidget;
        QHBoxLayout *hl = new QHBoxLayout(p1);
        QLabel *label1 = new QLabel("test");
        QLabel *label2 = new QLabel("0%");
        hl->addWidget(label1);
        hl->addWidget(label2);
        layout->addWidget(p1);
        i++;
    }

    QMainWindow *w = new QMainWindow;
    w->setGeometry(50,50,480,320);
    w->setCentralWidget(scroll);
    w->show();
Protomen
  • 9,471
  • 9
  • 57
  • 124
  • It looks like you're trying to make a list. Maybe have a look at `QListWidget` or the `QListView` and `QAbstractListModel` duo? – PrisonMonkeys May 28 '13 at 15:14

1 Answers1

9

Found your mistake, you should set layout to widget central not to scroll:

QWidget *central = new QWidget;
QScrollArea *scroll = new QScrollArea;
QVBoxLayout *layout = new QVBoxLayout(central);
scroll->setWidget(central);
scroll->setWidgetResizable(true);

EDITED:

Your labels already take all available space, if you noticed, label1 begins at left border ends in the middle, where label2 starts and ends at the right border. If I understood you correctly, you want label1 to take all the space available, while label2 with percents to take only what space is needed, no more?

Read about QSizePolicy class and use setSizePolicy() on your labels. Try to insert this line right after label2 declaration:

QLabel *label2 = new QLabel("0%");
label2->setSizePolicy(QSizePolicy::QSizePolicy::Maximum,QSizePolicy::Maximum);

And add line layout->addStretch(); right before QMainWindow *w = new QMainWindow;

Mikhail
  • 7,749
  • 11
  • 62
  • 136
Shf
  • 3,463
  • 2
  • 26
  • 42
  • Nice, But has the only stretch to Horizontal? – Protomen May 28 '13 at 20:35
  • you want to keep geometry (50,50,480,320) and stretch labels take all horizontal space? – Shf May 29 '13 at 10:38
  • Yes, I want each item (Widget child) occupy all space horizontally (horizontal only). Could you help me? Thank you for your attention. No need to be fixed height or width. The window can be resized. – Protomen May 29 '13 at 13:40
  • i don't understant, what you want to achive, in my test app, all works perfectly. Why don't you post screenshot with your current situation, and how you want to change it – Shf May 29 '13 at 18:04
  • Maybe if you replace `while(i<10)` by `while(i<3)` you will see the problem. Screenshot: http://i.stack.imgur.com/CRVhq.png – Protomen May 29 '13 at 18:16
  • add line `layout->addStretch();` before `QMainWindow *w = new QMainWindow;` – Shf May 29 '13 at 18:45