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. QGridLayout and QScrollArea
Qt 6.11 is out! See what's new in the release blog

QGridLayout and QScrollArea

Scheduled Pinned Locked Moved General and Desktop
1 Posts 1 Posters 1.2k 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.
  • F Offline
    F Offline
    fotte
    wrote on last edited by
    #1

    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));
    }@

    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