我在我的 QMainWindow 中使用扩展的 QGroupBox 作为 QDockWidget 的小部件。 这是代码片段:
RzPlaneViewerControlPanelWidget::RzPlaneViewerControlPanelWidget(QWidget *parent) : QGroupBox(parent) {
// TODO Auto-generated constructor stub
init();
}
void RzPlaneViewerControlPanelWidget::init()
{
QHBoxLayout *hbox=new QHBoxLayout;
hbox->setSizeConstraint(hbox->SetMinimumSize);
hbox->setSpacing(0);
hbox->setStretch(1,0);
setMaximumHeight(50);
QScrollBar *scrollbar=new QScrollBar;
scrollbar->setOrientation(Qt::Horizontal);
scrollbar->setMouseTracking(true);
scrollbar->setFocusPolicy(Qt::StrongFocus);
scrollbar->setMinimum(0);
scrollbar->setSingleStep(1);
QLineEdit *qlineedit = new QLineEdit;
qlineedit->setMaximumWidth(60);
qlineedit->setReadOnly(true);
hbox->addWidget(scrollbar);
hbox->addWidget(qlineedit);
//hbox->addWidget(new )
setLayout(hbox);
以下是我如何将此小部件添加到 QDockWidget 中:
RzPlaneViewerControlPanelWidget *controlPanel=new RzPlaneViewerControlPanelWidget ;
controlPanel->init();
QDockWidget controlPanelDockWidet=new QDockWidget;
controlPanelDockWidet->setAllowedAreas(Qt::BottomDockWidgetArea);
controlPanelDockWidet->setFeatures(QDockWidget::DockWidgetVerticalTitleBar);
controlPanelDockWidet->setWidget(controlPanel);
但是 ScrollBar 并没有像我预期的那样被拉伸(stretch) - 这是它的样子 -
这就是我想要的-
最佳答案
通过快速阅读,我会尝试这样做:
hbox->addWidget(scrollbar, 1);
addWidget 的第二个(可选)参数是拉伸(stretch)因子。来自Qt docs :
If the stretch factor is 0 and nothing else in the QBoxLayout has a stretch factor greater than zero, the space is distributed according to the QWidget:sizePolicy() of each widget that's involved.
另外,请注意您的:
hbox->setStretch(1,0);
调用没有任何效果,因为它将 qlineedit
的拉伸(stretch)因子设置为 0,但这已经是默认值。
关于c++ - Qt QHBoxLayout 问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6449002/