8

Create a widget scrollable area

 2 years ago
source link: https://www.codesd.com/item/create-a-widget-scrollable-area.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Create a widget scrollable area

advertisements

For an assignment, I need create a CMS similar to WordPress using C++ and Qt. In the main page, all of the posts must be shown in an area with a scrollbar. I tried using QScrollArea but the problem seems to be the layout. It shrinks the objects inside to fit into the provided height. I tried to fix this by setting the size policy of the objects to fixed but surprisingly that didn't make any difference at all!

Here's the code to the window I'm trying to make:

#include "cms.h"
#include "user.h"
#include "post.h"
#include "uipost.h"
#include <QStyle>
#include <QDesktopWidget>
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QScrollArea>
#include <QPushButton>
#include <QMenuBar>
#include <QMenu>
#include <QAction>
#include <QLabel>
#include <QSizePolicy>

CMS::CMS(User & SignedInUser, QWidget *parent) : QWidget(parent), signedInUser(SignedInUser) {
    User admin;
    Post temp(admin);

    QHBoxLayout *mainLayout = new QHBoxLayout();

    QScrollArea *posts = new QScrollArea();
    posts->setWidgetResizable(true);
    posts->setFrameShape(QFrame::NoFrame);
    QVBoxLayout *postsLayout = new QVBoxLayout(posts);
    for (int i = 0; i < 50; i++) {
    QLabel *label = new QLabel(tr("some sample label"));
    postsLayout->addWidget(label);
    /* Here the posts will be read from file and shown. Since the class for posts isn't still ready, I'm just trying to try it with label and later on use that class.
     * That class inheritances QFrame.
     */
    }

    QVBoxLayout *buttonsLayout = new QVBoxLayout();
    buttonsLayout->setAlignment(Qt::AlignTop);
    QPushButton *manUsers = new QPushButton(tr("Manage Users"));
    buttonsLayout->addWidget(manUsers);
    QPushButton *addUser = new QPushButton(tr("Add a User"));
    buttonsLayout->addWidget(addUser);
    QPushButton *logout = new QPushButton(tr("Log Out"));
    buttonsLayout->addWidget(logout);
    QPushButton *exit = new QPushButton(tr("Exit"));
    buttonsLayout->addWidget(exit);

    mainLayout->addWidget(posts);
    mainLayout->addLayout(buttonsLayout);
    setLayout(mainLayout);

    setGeometry(QStyle::alignedRect(Qt::LeftToRight, Qt::AlignCenter, size(), QDesktopWidget().availableGeometry())); // Centerizing the window
    setFixedSize(size()); // Making the window unresizable

    connect(exit, &QPushButton::clicked, this, &QWidget::close);
}

The class definition doesn't contain anything specific but just to make sure nothing's missed:

#ifndef CMS_H
#define CMS_H

#include <QMainWindow>
#include "user.h"

namespace Ui {
    class CMS;
}

class CMS : public QWidget {
    Q_OBJECT
private:
    User & signedInUser;
public:
    CMS(User & SignedInUser, QWidget *parent = nullptr);
};

#endif // CMS_H


I added a single line to your code (after uncommenting and reordering some line, please provide a MCVE next time), which is

postsLayout->setSizeConstraint(QLayout::SetMinAndMaxSize);

to your constructor.

Notice the cut at the bottom.

For reference I suggest the pretty good documentation. For further topics with QScrollArea (Where the hell are the QScrollBars?) I suggest you to work through this example.


Also note that one might argue that your approach is not that suited for your task.

Tags scrollbar

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK