实现效果:(确认按钮固定,上方内容右侧有滚动条可上下滑动)
第一次做滚动区,想通过QVboxLayout在上面做一个固定QWidget,限制其高度,让他达到滚动的目的,做成了下面这个模样
2、通过网上查找方法知道了滚动区QScrollArea类可以实现区域内容滚动效果
*QScrollArea布局是出现滚动条的原因,主要是因为其中包含了一个子类scrllAreaWidgetContents,是小widget套一个大widget实现内部装不下而出现滚动效果。
步骤如下:
1)new QScrollArea;
2)new QFrame;
3)new QLabel,QPushButton等内部布局控件;
4)将内部控件装入内部布局QFrame;
5)将QFrame内部布局装入QScrollArea
Public Functions
QScrollArea(QWidget *parent = nullptr)
virtual
~QScrollArea()
Qt::Alignment
alignment() const
void
ensureVisible(int x, int y, int xmargin = 50, int ymargin = 50)
void
ensureWidgetVisible(QWidget *childWidget, int xmargin = 50, int ymargin = 50)
void
setAlignment(Qt::Alignment)
void
setWidget(QWidget *widget)
void
setWidgetResizable(bool resizable)
QWidget *
takeWidget()
QWidget *
widget() const
bool
widgetResizable() const
/*添加窗体内部滚动布局*/
void addTrunk()
{
scrollArea = new QScrollArea(this);
scrollArea->setGeometry(0, 50, 360, 450);/*左上长宽*/
QFrame * widget = new QFrame(this);
widget->setContentsMargins(0, 0, 0, 0);
widget->setFixedSize(350, 590);
m_pTrunk = new QVBoxLayout(widget);
m_pTrunk->setContentsMargins(15, 0, 20, 10);
m_pTrunk->setSpacing(10);
QLabel *roomName = new QLabel;
roomName->setText(u8"房间名:");
roomName->setObjectName("editroom_topic");
QTextEdit *roomNameEdit = new QTextEdit;
roomNameEdit->setFixedWidth(331);
roomNameEdit->setFixedHeight(54);
roomNameEdit->setPlaceholderText(QStringLiteral("请输入房间名"));
roomNameEdit->setObjectName("grey_4r_textedit");
QLabel *roomNoticeTitle = new QLabel;
roomNoticeTitle->setText(u8"公告标题(限10个字):");
roomNoticeTitle->setObjectName("editroom_topic");
m_pNoticeTitleRegister = new QLabel(); //计数器
m_pNoticeTitleRegister->setGeometry(100, 250, 58, 18);
m_pNoticeTitleRegister->setStyleSheet("QLabel{color:#95949D;font:14px;}");
roomNoticeTitleEdit = new QLineEdit;
roomNoticeTitleEdit->setFixedWidth(250);
roomNoticeTitleEdit->setFixedHeight(30);
roomNoticeTitleEdit->setPlaceholderText(QStringLiteral("请输入公告标题"));
roomNoticeTitleEdit->setObjectName("grey_4r_lineedit_register");
roomNoticeTitleEdit->setMaxLength(10);
connect(roomNoticeTitleEdit, SIGNAL(textEdited()), this, SLOT(slot_scalerNoticeTitleText()));
QLabel *roomNotice = new QLabel;
roomNotice->setText(u8"房间公告(限300个字):");
roomNotice->setObjectName("editroom_topic");
m_pNoticeRegister = new QLabel(); //计数器
m_pNoticeRegister->setGeometry(100, 450, 58, 18);
m_pNoticeRegister->setStyleSheet("QLabel{color:#95949D;font:14px;}");
roomNoticeEdit = new QTextEdit;
roomNoticeEdit->setFixedWidth(314);
roomNoticeEdit->setFixedHeight(110);
roomNoticeEdit->setPlaceholderText(QStringLiteral("请输入房间公告..."));
roomNoticeEdit->setObjectName("grey_4r_textedit_Register");
roomNoticeEdit->setMaximumWidth(10);
connect(roomNoticeEdit, SIGNAL(textChanged()), this, SLOT(slot_scalerNoticeText()));
QLabel *roomWelcome = new QLabel;
roomWelcome->setText(u8"欢迎语(限300个字):");
roomWelcome->setObjectName("editroom_topic");
m_pWelcomeRegister = new QLabel(); //计数器
m_pWelcomeRegister->setGeometry(100, 650, 58, 18);
m_pWelcomeRegister->setStyleSheet("QLabel{color:#95949D;font:14px;}");
roomWelcomeEdit = new QTextEdit;
roomWelcomeEdit->setFixedWidth(314);
roomWelcomeEdit->setFixedHeight(110);
roomWelcomeEdit->setPlaceholderText(QStringLiteral("请输入欢迎语..."));
//roomWelcomeEdit->setHtml(QString("%1").arg(QStringLiteral(" 请输入欢迎语...")));
roomWelcomeEdit->setObjectName("grey_4r_textedit_Register");
connect(roomWelcomeEdit, SIGNAL(textChanged()), this, SLOT(slot_scalerWelcomeText()));
QLabel *roomKey = new QLabel;
roomKey->setText(u8"房间密码:");
roomKey->setObjectName("editroom_topic");
QLineEdit *roomKeyEdit = new QLineEdit;
roomKeyEdit->setFixedWidth(331);
roomKeyEdit->setFixedHeight(30);
roomKeyEdit->setPlaceholderText(QStringLiteral("请输入4位房间密码..."));
roomKeyEdit->setObjectName("grey_4r_lineedit");
QRegExp regx("[0-9]+$");
QValidator *validator = new QRegExpValidator(regx, roomKeyEdit);
roomKeyEdit->setValidator(validator);
roomKeyEdit->setMaxLength(4);
/*结合计数器和edit*/
QWidget *m_pNoticeTitleBar = new QWidget;
m_pNoticeTitleBar->setContentsMargins(0, 0, 0, 0);
m_pNoticeTitleBar->setFixedWidth(330);
m_pNoticeTitleBar->setFixedHeight(30);
m_pNoticeTitleBar->setObjectName("edit_Register_bar");
QVBoxLayout *m_pNoticeTitleBox = new QVBoxLayout(m_pNoticeTitleBar);
m_pNoticeTitleBox->addWidget(roomNoticeTitleEdit, 0, Qt::AlignLeft);
QWidget *m_pNoticeBar = new QWidget;
m_pNoticeBar->setFixedHeight(120);
m_pNoticeBar->setFixedWidth(330);
m_pNoticeBar->setObjectName("edit_Register_bar");
QVBoxLayout *m_pNoticeBox = new QVBoxLayout(m_pNoticeBar);
m_pNoticeBox->addWidget(roomNoticeEdit, 0, Qt::AlignTop | Qt::AlignLeft);
m_pNoticeBox->addWidget(m_pNoticeRegister, 0, Qt::AlignTrailing | Qt::AlignRight);
m_pNoticeBox->addSpacing(2);
QWidget *m_pWelcomeBar = new QWidget;
m_pWelcomeBar->setFixedHeight(120);
m_pWelcomeBar->setFixedWidth(330);
m_pWelcomeBar->setObjectName("edit_Register_bar");
QVBoxLayout *m_pNoticeWelcomeBox = new QVBoxLayout(m_pWelcomeBar);
m_pNoticeWelcomeBox->addSpacing(-6);
m_pNoticeWelcomeBox->addWidget(roomWelcomeEdit, 0, Qt::AlignTop | Qt::AlignLeft);
m_pNoticeWelcomeBox->addWidget(m_pWelcomeRegister, 0, Qt::AlignTrailing | Qt::AlignRight);
m_pNoticeWelcomeBox->addSpacing(2);
m_pTrunk->addWidget(roomName, 0, Qt::AlignLeft);
m_pTrunk->addWidget(roomNameEdit, 0, Qt::AlignLeft);
m_pTrunk->addWidget(roomNoticeTitle, 0, Qt::AlignLeft);
m_pTrunk->addWidget(m_pNoticeTitleBar, 0, Qt::AlignLeft);
m_pTrunk->addWidget(roomNotice, 0, Qt::AlignLeft);
m_pTrunk->addWidget(m_pNoticeBar, 0, Qt::AlignLeft);
m_pTrunk->addWidget(roomWelcome, 0, Qt::AlignLeft);
m_pTrunk->addWidget(m_pWelcomeBar, 0, Qt::AlignLeft);
m_pTrunk->addWidget(roomKey, 0, Qt::AlignLeft);
m_pTrunk->addWidget(roomKeyEdit, 0, Qt::AlignLeft);
widget->setLayout(m_pTrunk);
scrollArea->setWidget(widget);
widget->setObjectName("we");
widget->setStyleSheet("QFrame#we{background:white;}");
}
*QFrame与QWidget的区别:
QFrame是基本控件的基类,QWidget是QFrame基类,关系如下:
QPushButton,QLabel… -> QFrame ->QWidget
3.无法改变滚动区背景颜色
QScrollArea 是无法通过样式表来改变背景色的,只能设置加入到QScrollArea里面的QWidget的样式,这样才能改变背景色。
当然也可以通过函数来设置QScollArea的背景色。
scrollArea->setBackgroundRole(QPalette::Dark); // 背景色
4.setVerticalScrollMode(QAbstractItemView::ScrollMode::ScrollPerPixel);
enum QAbstractItemView::ScrollMode
Describes how the scrollbar should behave. When setting the scroll mode to ScrollPerPixel the single step size will adjust automatically unless it was set explicitly using setSingleStep(). The automatic adjustment can be restored by setting the single step size to -1.
描述滚动条的行为方式。将滚动模式设置为ScrollPerPixel时,除非使用setsingleStep()显式设置,否则单步大小将自动调整。通过将单步大小设置为-1,可以恢复自动调整。
Constant
Value
Description
QAbstractItemView::ScrollPerItem
0
The view will scroll the contents one item at a time.
QAbstractItemView::ScrollPerPixel
1
The view will scroll the contents one pixel at a time.
This enum was introduced or modified in Qt 4.2.