QGridLayout and QScrollArea



  • Hi, i am new to QT and i'm trying to have a gridlayout within a scrollarea. But somehow i can't get it to work the way i intent it to.

    I intend to create my own widget, which has two push buttons, and a certain minimum size (i'm not quite sure what size it should have in the end. something around 512x256 i guess).

    One button opens a filedialog and the user can choose a directory, and all images in that directory are shown in a gridlayout which is within a scroll area.

    I subclassed the QWidget and my code looks like this:
    @ ...
    {
    /// create an initial main layout
    main_layout = new QGridLayout();
    setLayout(main_layout);

    /// create a scrollarea for the top widget
    top_scroll = new QScrollArea;
    
    /// create a top and bottom widget
    /// bottom -> Holds all the controls
    bottom_widget = new QWidget();
    top_widget = new QWidget(top_scroll);
    
    /// create new layout for the bottom widgets
    bottom_layout = new QHBoxLayout(bottom_widget);
    top_layout = new QGridLayout(top_widget);
    
    /// associate the scroll area with the top-widget
    top_scroll->setWidget(top_widget);
    
    open = new QPushButton(QIcon(), tr("Open"));
    clear = new QPushButton(QIcon(), tr("Clear"));
    
    /// connect the buttons to their respective symbols
    connect(open, SIGNAL(clicked()), this, SLOT(openFileDialog()));
    
    bottom_layout->addWidget(open);
    bottom_layout->addWidget(clear);
    top_widget->setLayout(top_layout);
    bottom_widget->setLayout(bottom_layout);
    main_layout->addWidget(top_scroll, 0, 0);
    main_layout->addWidget(bottom_widget,1,0);
    

    @
    later i add widgets to like this:

    @ QLabel *lab = new QLabel();
    lab->setPixmpa(pix);
    lab->setFixedSize(128,128);
    top_layout->addWidget(lab)
    @

    But nothing is shown in the scrollarea.

    I tried adding the scrollarea and the layout after i inserted the labels:

    @
    QLabel *lab = new QLabel();
    lab->setPixmpa(pix);
    lab->setFixedSize(128,128);
    top_layout->addWidget(lab);
    top_widget->setLayout(top_layout);
    top_scroll->setWidget(top_widget);
    @

    but that did not do the trick.
    Can you point me in the right direction?

    EDIT:
    I use this widget in the following way: (maybe I got it wrong here?)

    @MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
    {
    ui->setupUi(this);
    ui->gridLayout->addWidget(new imagePreviewer(this));
    }@


Log in to reply
 

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