2013-01-10 16 views
6

我的小部件的結構是:如何讓小部件溢出以使滾動條出現在Qt中?

QWidget自定義爲具有圓形邊框的面板。

爲了遏制與保證金的邊框內的滾動條的面積,然後我把這個裏面:

QScrollArea與QVBoxLayout(垂直添加內容)

然後,添加它的內部系列:

QGroupBox與標題與間隔0和QFormLayout

formlayout沒有按照我的想法工作。裏面的小部件是標籤+ spinboxes,都是。

這裏有一個形象:

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

您有完整的代碼。希望它很有用! – darkgaze

+0

現在一切都很清楚。您不應該在滾動區域設置佈局。而是使用'QScrollArea :: setWidget(QWidget *)'方法。 –

回答

16

QScrollArea不是容器。 QScrollArea是另一個小部件的「滾動視圖」。您不應該在QScrollArea上設置佈局。您應該創建小工具,用適當的佈局填充它,然後使用QScrollArea::setWidget(QWidget *)使其可滾動。

+0

哦。我應該在裏面放一個qwidget,然後爲它設置一個佈局並插入QGroupBoxes? – darkgaze

+0

是的!高超。有趣的是我閱讀了文檔上的所有內容,但他們沒有提到這一點。 setLayout可用,它不應該被使用。這很有意思。非常感謝。對不起,我沒有足夠的聲望:-( – darkgaze

+0

setLayout可用於每個小部件,因爲我記得(即使在QPushButton上),但在QScrollArea的「詳細描述」中有清楚的寫法,你必須使用'setWidget' –