【问题标题】:Make an scrollable area of widgets制作小部件的可滚动区域
【发布时间】:2017-10-14 08:53:56
【问题描述】:

对于一项任务,我需要使用 C++ 和 Qt 创建一个类似于 WordPress 的 CMS。在主页中,所有帖子都必须显示在带有滚动条的区域中。我尝试使用 QScrollArea 但问题似乎出在布局上。它缩小内部的对象以适应提供的高度。我试图通过将对象的大小策略设置为固定来解决此问题,但令人惊讶的是,这根本没有任何区别!

这是我正在尝试制作的窗口的代码:

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

类定义不包含任何具体内容,只是为了确保没有遗漏任何内容:

#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

【问题讨论】:

    标签: c++ qt scrollbar


    【解决方案1】:

    我找到了解决方案。显然我需要定义一个小部件,将滚动区域设置为该小部件,然后将包含元素的布局设置为小部件。

    【讨论】:

      【解决方案2】:

      我在您的代码中添加了一行(取消注释并重新排序后,请下次提供MCVE),即

      postsLayout->setSizeConstraint(QLayout::SetMinAndMaxSize);
      

      给你的构造函数。

      注意底部的切口。

      作为参考,我建议相当不错的documentation。有关QScrollArea 的更多主题(QScrollBars 到底在哪里?)我建议您通过this example 工作。


      另请注意,有人可能会争辩说您的方法不适合您的任务。

      【讨论】:

      • 非常感谢您提供的参考资料和链接。
      猜你喜欢
      • 1970-01-01
      • 2019-08-13
      • 2019-09-01
      • 2015-12-04
      • 1970-01-01
      • 1970-01-01
      • 2011-04-27
      • 1970-01-01
      • 2021-04-09
      相关资源
      最近更新 更多