如何使小部件溢出以使Qt中出现滚动条?

时间:2013-01-10 09:19:12

标签: qt user-interface scrollbar qscrollarea

我的小部件的结构是:

自定义QWidget为具有圆形边框的面板。

要在边框内部包含带有滚动条的区域,请将其放入内部:

QScrollArea带有QVBoxLayout(垂直添加内容)

然后我在里面加了一系列:

标题间距为0的QGroupBox和QFormLayout

formlayout不能像我想象的那样工作。里面的小部件是标签+旋转盒,所有这些都是。

这是一张图片:

Image

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

二。正如我告诉他们的那样,它们都被赋予相同的固定尺寸,但无论如何它们都被堆积,而不是被压扁,所以它们互相隐藏。为什么它不会保持这个大小,父QScrollArea显示外面的滚动条?这就是我想要的。

我不希望内容被压扁或拉伸。我希望他们能够登顶。如果屏幕非常大,面板会很长,但内容将位于顶部,总是大小相同。


有人要求代码,所以我把它复制到这里,但代码真的很大......我觉得它更令人困惑。但好吧,生病了删除没有任何意义的行。以下是您在该框中看到的部分:

  // 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).

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

/* 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);
}

1 个答案:

答案 0 :(得分:17)

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