如何使小部件溢出以使滚动条出现在Qt中? [英] How can i make widgets overflow to make a scrollbar appear in Qt?

查看:155
本文介绍了如何使小部件溢出以使滚动条出现在Qt中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的小部件的结构是:

QWidget被定制为带有圆角边框的面板.

要在带有边框的边界内包含一个带有滚动条的区域,那么我将其放在其中:

To contain an area with scrollbar inside the borders with a margin, then I put this inside:

具有QVBoxLayout(垂直添加内容)的QScrollArea

然后我在其中添加一系列:

Then I add inside of it a series of :

QGroupBox,标题间距为0,并且带有QFormLayout

表单布局无法正常运行.里面的小部件是标签+旋转框.

The formlayout doesn´t work as I thought it would. The widgets inside are the labels + spinboxes, all of them.

这是一张图片:

首先.他们没有居中.我不知道为什么.

First. They are not centered. I don´t know why.

第二.正如我告诉他们的那样,它们都具有相同的固定大小,但是无论如何它们都是堆放的,而不是压扁的,所以它们彼此隐藏起来.为什么不保持该大小,而父QScrollArea在外部显示滚动条?那就是我想要的.

Second. They are all given the same fixed size, as I told them, but they are piled anyway, not squashed, so they hide each other. Why doesn´t it stay that size and the parent QScrollArea shows the scrollbar outside?. That´s what i want.

我不希望挤压或拉伸内容物.我希望他们能排在首位.如果屏幕很大,则面板会很长,但内容始终在顶部,且尺寸始终相同.

I don´t want the contents to be squashed or stretched. I want them to be on the top. If the screen is very big, the panel will be long, but the contents will be on the top, allways with the same size.

有人要求提供代码,所以我将其复制到此处,但是代码确实很大……我认为这更加令人困惑.但是,不好,删除没有意义的行.这是您在该框中看到的部分:

Somebody requested the code, so i copy it here, but the code it really big... I think it's more confusing. But well, ill remove the lines with no meaning. Here is the part you see in that box:

  // THE PANEL OUTSIDE (A QWIDGET) is mainParametersLayout_. This particular scroll bar inside
  // is cameraModeParametersPanel_

  cameraModeParametersPanel_ = new QScrollArea();
  cameraModeParametersPanel_->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
  cameraModeParametersPanel_->setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded);
  mainParametersLayout_->addWidget( cameraModeParametersPanel_ );

  // HERE THERE ARE TWO MORE QGROUP BOXES. THE ONE THAT DOESN´T STAY THE WAY I SHOWED IS THIS. 

  QVBoxLayout* mainCameraLayout = new QVBoxLayout(cameraModeParametersPanel_);
  mainCameraLayout->setSpacing(5);

  // GROUP BOX 
  QGroupBox* activeCameraParametersGroup = new QGroupBox();
  activeCameraParametersGroup->setObjectName( parametersContainerName );
  activeCameraParametersGroup->setTitle(strings->cameraModeCameraParamsTitle);
  mainCameraLayout->addWidget( activeCameraParametersGroup );

  // LAYOUT
  QFormLayout* paramLayout = new QFormLayout( activeCameraParametersGroup );
  paramLayout->setRowWrapPolicy(QFormLayout::DontWrapRows);
  paramLayout->setFormAlignment( Qt::AlignHCenter | Qt::AlignTop );
  paramLayout->setLabelAlignment(Qt::AlignRight);


  // Iso : Spin Integer
  isoSpin = new SmartIntSpinButtons( control->getMinISO(), control->getMaxISO() );
  isoSpin->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
  paramLayout->addRow(strings->cameraISOCapString, isoSpin);

  // FStop: Spin Double
  fstopSpin = new SmartDoubleSpinButtons( control->getMinFStop(), control->getMaxFStop(), 2);
  fstopSpin->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
  paramLayout->addRow(strings->cameraFStopString, fstopSpin);



  // some other spins here...



  // Camera position: 3 Spin Double (X,Y,Z)
  camPosSpinX = new SmartDoubleSpinButtons( control->getMinCamPos(), control->getMaxCamPos(), 1);
  camPosSpinY = new SmartDoubleSpinButtons( control->getMinCamPos(), control->getMaxCamPos(), 1);
  camPosSpinZ = new SmartDoubleSpinButtons( control->getMinCamPos(), control->getMaxCamPos(), 1);

  camPosSpinX->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
  camPosSpinY->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
  camPosSpinZ->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);

  paramLayout->addRow( new QLabel( strings->cameraPositionString ) );
  QHBoxLayout* positionLy = new QHBoxLayout();
  positionLy->addWidget( camPosSpinX );
  positionLy->addWidget( camPosSpinY );
  positionLy->addWidget( camPosSpinZ );
  paramLayout->addRow( positionLy );

  // Target Position: 3 Spin Double( X,Y,Z )
  camTargetPosSpinX = new SmartDoubleSpinButtons( control->getMinCamTarget(), control->getMaxCamTarget(), 1);
  camTargetPosSpinY = new SmartDoubleSpinButtons( control->getMinCamTarget(), control->getMaxCamTarget(), 1);
  camTargetPosSpinZ = new SmartDoubleSpinButtons( control->getMinCamTarget(), control->getMaxCamTarget(), 1);

  camTargetPosSpinX->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
  camTargetPosSpinY->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
  camTargetPosSpinZ->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);

  paramLayout->addRow( new QLabel( strings->cameraTargetPositionString ));
  QHBoxLayout* targetLy = new QHBoxLayout();
  targetLy->addWidget( camTargetPosSpinX );
  targetLy->addWidget( camTargetPosSpinY );
  targetLy->addWidget( camTargetPosSpinZ );
  paramLayout->addRow( targetLy );

  // and the resoultion spins, which are the same style like the last one (but only X and Y).

现在所有样式表都出现了:

Now comes the stylesheet of all:

/* THE PANEL THAT CONTAINS THE QSCROLLBAR OUTSIDE */

SmartPanel
{
    background-image: url(:/resources/images/containers/panel_bg.png);
    background-repeat: repeat-y;
    background-position: left top;
    background-color: white;
    border: 1px solid #aaa;
    border-radius: 10;
    min-width: 20px;
    padding: 5px;

}


QScrollArea#parametersPanelScrollArea
{
    background: transparent;
    border: none;
}


/* the qgroupbox */  


QGroupBox#parametersContainer
{
  background-color: white;
  padding-top: 25px;
  border-style: solid;
  border-width: 1px;
  border-color: #aaa;
  border-radius: 10px;
}

QGroupBox#parametersContainer::title  {
    subcontrol-origin: margin;
    subcontrol-position: top center;
    border: 1px solid #aaa;
    margin-top: -5px;
    padding: 8px 5px 5px 5px;
    font-size: 18px;
    border-radius: 5px;
}    


/* ------------------ SPINBOX WIDGET ------------------------------------------*/

QWidget#intSpin, QWidget#doubleSpin
{
    min-height: 20px;
    border: 1px solid #ccc;
    padding: 0px;
    border-top-left-radius: 4px;
    border-bottom-left-radius: 4px;
    background: qlineargradient(x1:0, y1:0, x2:0, y2:1, stop:0 #ddd, stop:1 #fff);
}

QPushButton#upSpinBtn,
QPushButton#downSpinBtn
{
    border-radius: 0px;
    /*background: qlineargradient(x1:0, y1:0, x2:0, y2:1, stop:0 #ddd, stop:1 #fff);*/
    background-repeat: no-repeat;
    background-position: center;
    border: none;
}

QPushButton#upSpinBtn
{   
    background-image: url(:/resources/images/buttons/up_sm_arrow.png);
}

QPushButton#downSpinBtn
{
    background-image: url(:/resources/images/buttons/down_sm_arrow.png);
}

推荐答案

QScrollArea不是容器. QScrollArea是另一个小部件的滚动视图".您不应该在QScrollArea上设置布局.您应该创建窗口小部件,以适当的布局填充它,然后使用 QScrollArea :: setWidget(QWidget *)使其可滚动.

QScrollArea is not a container. QScrollArea is a "scrolling view" for another widget. You shouldn't set up layout on QScrollArea. You should create widget, fill it with proper layout and then use QScrollArea::setWidget(QWidget *) to make it scrollable.

这篇关于如何使小部件溢出以使滚动条出现在Qt中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆