c++ - Qt QHBoxLayout 问题?

标签 c++ qt layout qt4

我在我的 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) - 这是它的样子 -

http://oi56.tinypic.com/5mzf5y.jpg

这就是我想要的-

http://oi55.tinypic.com/23wv3tf.jpg

最佳答案

通过快速阅读,我会尝试这样做:

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/

相关文章:

c++ - 为什么删除 End-Iterator 不会导致 Segmentation Fault with shared_ptr?

c++ - QDrag 在拖动时被破坏

c++ - 如何在 Qt 5.0.2 中添加/使用 <QtMath>

c++ - 如何让我的 Qt 程序持续向我的 Arduino 发送字符串?

Android覆盖布局在所有接收触摸的窗口之上

c++ - 输出所需的列数

c++ - 如何使用 Qt 打印 unicode 字符?

html - 在 html 中编码表格形式

c++ - 设置小部件初始大小

c++ - "default definition would be ill-formed"是什么意思?