Qt:小部件在scrollarea

时间:2015-09-08 13:54:36

标签: c++ qt layout widget scrollview

所以我有一个程序从文件读取参数并围绕这些参数构建UI。参数需要一些控件等,所以我为此制作了一个小部件: Widget

根据文件中的X量参数,将添加X次。如果参数量足够小,程序看起来很好: program normal

但是,如果参数太多,即使将它们放在滚动区中,它们也会重叠: program normal overlap

当我重新调整窗口大小时,情况变得更糟: program resized overlap

我使用以下代码创建布局:

void MainWindow::on_treeView_doubleClicked(const QModelIndex &index)
{
    //user clicked category in treeview.
    int returnedrow = index.row();
    int returnedcol = index.column();
    returnedcol = returnedcol;
    const QAbstractItemModel * model = index.model();
    QVariant vt = model->data(model->index(index.row(), returnedcol, index.parent()), Qt::DisplayRole);
    QString compare = vt.toString();
    size_t maxsizearr = returnedrow;
    QWidget *box = new QWidget();
    QGridLayout *grid = new QGridLayout(box);
    int row = 0,column =0;

    for(size_t k=0;k<ipdb.size();++k) //Find out which category was clicked and then make GUI
    {
        if(maxsizearr<ipdb[k].subip.size())
        {
            if(  ipdb[k].subip[returnedrow].compare(compare.toStdString()) == 0)//category found, build GUI
            {
                //ui->mainlabel->setText("found");
                int size = vprParam_GetSize(database[k].instance[returnedrow]); //Number of parameters in category
                for(int i=0; i<size;++i)
                {
                    QString name = vprParam_GetNameById(database[k].instance[returnedrow],i);
                    int min = vprParam_GetRangeMinByName(database[k].instance[returnedrow],name.toStdString().c_str());
                    int max = vprParam_GetRangeMaxByName(database[k].instance[returnedrow],name.toStdString().c_str());
                    int value = vprParam_GetByName(database[k].instance[returnedrow],name.toStdString().c_str());
                    ParamBlock *widget = new ParamBlock();
                    widget->changeTitle(name);
                    widget->changeMin(min);
                    widget->changeMax(max);
                    widget->setValue(value);
                    grid->addWidget(widget,row,column);
                    ++row;
                    if(row>14)
                    {
                        row=0;
                        ++column;
                    }


                }
            }
        }
    }
    if (row<14&&row>0) //add empty widgets to make the spacing equal in all catogeries
    {
        for(int b = row; b<=14;++b)
        {
            QWidget *filler = new QWidget();
            grid->addWidget(filler,b,column);
        }
    }
    ui->scrollArea->setWidget(box);
}

ParamBlock.cpp代码:

#include "paramblock.h"
#include "ui_paramblock.h"

ParamBlock::ParamBlock(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::ParamBlock)
{
    ui->setupUi(this);
}

ParamBlock::~ParamBlock()
{
    delete ui;
}

void ParamBlock::changeTitle(QString X)
{
    ui->groupBox->setTitle(X);
}

void ParamBlock::changeMin(int X)
{
    ui->label->setText(QString::number(X));
    ui->horizontalSlider->setMinimum(X);
}

void ParamBlock::changeMax(int X)
{
    ui->label_2->setText(QString::number(X));
    ui->horizontalSlider->setMaximum(X);
}

void ParamBlock::setValue(int X)
{
    ui->horizontalSlider->setValue(X);
    ui->lineEdit->setText(QString::number(X));
}


void ParamBlock::on_lineEdit_textEdited(const QString &arg1)
{

    if((arg1.toInt())>(ui->horizontalSlider->maximum()))
    {
        ui->lineEdit->setText(QString::number(ui->horizontalSlider->maximum()));
        ui->horizontalSlider->setValue(ui->horizontalSlider->maximum());
    }
    else if((arg1.toInt())<(ui->horizontalSlider->minimum()))
    {
        ui->lineEdit->setText(QString::number(ui->horizontalSlider->minimum()));
        ui->horizontalSlider->setValue(ui->horizontalSlider->minimum());
    }
    else
        ui->horizontalSlider->setValue(arg1.toInt());

}

void ParamBlock::on_horizontalSlider_valueChanged(int value)
{
    ui->lineEdit->setText(QString::number(value));
}

ParamBlock.ui:

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>ParamBlock</class>
 <widget class="QWidget" name="ParamBlock">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>326</width>
    <height>53</height>
   </rect>
  </property>
  <property name="sizePolicy">
   <sizepolicy hsizetype="Preferred" vsizetype="Preferred">
    <horstretch>0</horstretch>
    <verstretch>0</verstretch>
   </sizepolicy>
  </property>
  <property name="windowTitle">
   <string>Form</string>
  </property>
  <property name="styleSheet">
   <string notr="true">QToolTip
{
     border: 1px solid black;
     background-color: #ffa02f;
     padding: 1px;
     border-radius: 3px;
     opacity: 100;
}

QWidget
{
    color: #b1b1b1;
    background-color: #323232;
}

QWidget:item:hover
{
    background-color: QLinearGradient( x1: 0, y1: 0, x2: 0, y2: 1, stop: 0 #ffa02f, stop: 1#d7801a);
    color: #000000;
}

QWidget:item:selected
{
    background-color: QLinearGradient( x1: 0, y1: 0, x2: 0, y2: 1, stop: 0 #ffa02f, stop: 1 #d7801a);
}

QMenuBar::item
{
    background: transparent;
}

QMenuBar::item:selected
{
    background: transparent;
    border: 1px solid #ffaa00;
}

QMenuBar::item:pressed
{
    background: #444;
    border: 1px solid #000;
    background-color: QLinearGradient(
        x1:0, y1:0,
        x2:0, y2:1,
        stop:1 #212121,
        stop:0.4 #343434/*,
        stop:0.2 #343434,
        stop:0.1 #ffaa00*/
    );
    margin-bottom:-1px;
    padding-bottom:1px;
}

QMenu
{
    border: 1px solid #000;
}

QMenu::item
{
    padding: 2px 20px 2px 20px;
}

QMenu::item:selected
{
    color: #000000;
}

QWidget:disabled
{
    color: #404040;
    background-color: #323232;
}

QAbstractItemView
{
    background-color: QLinearGradient( x1: 0, y1: 0, x2: 0, y2: 1, stop: 0 #4d4d4d, stop: 0.1 #646464, stop: 1 #5d5d5d);
}

QWidget:focus
{
    /*border: 2px solid QLinearGradient( x1: 0, y1: 0, x2: 0, y2: 1, stop: 0 #ffa02f, stop: 1 #d7801a);*/
}

QLineEdit
{
    background-color: QLinearGradient( x1: 0, y1: 0, x2: 0, y2: 1, stop: 0 #4d4d4d, stop: 0 #646464, stop: 1 #5d5d5d);
    padding: 1px;
    border-style: solid;
    border: 1px solid #1e1e1e;
    border-radius: 5;
}

QPushButton
{
    color: #b1b1b1;
    background-color: QLinearGradient( x1: 0, y1: 0, x2: 0, y2: 1, stop: 0 #565656, stop: 0.1 #525252, stop: 0.5 #4e4e4e, stop: 0.9 #4a4a4a, stop: 1 #464646);
    border-width: 1px;
    border-color: #1e1e1e;
    border-style: solid;
    border-radius: 6;
    padding: 3px;
    font-size: 12px;
    padding-left: 5px;
    padding-right: 5px;
}

QPushButton:pressed
{
    background-color: QLinearGradient( x1: 0, y1: 0, x2: 0, y2: 1, stop: 0 #2d2d2d, stop: 0.1 #2b2b2b, stop: 0.5 #292929, stop: 0.9 #282828, stop: 1 #252525);
}

QComboBox
{
    selection-background-color: #ffaa00;
    background-color: QLinearGradient( x1: 0, y1: 0, x2: 0, y2: 1, stop: 0 #565656, stop: 0.1 #525252, stop: 0.5 #4e4e4e, stop: 0.9 #4a4a4a, stop: 1 #464646);
    border-style: solid;
    border: 1px solid #1e1e1e;
    border-radius: 5;
}

QComboBox:hover,QPushButton:hover
{
    border: 2px solid QLinearGradient( x1: 0, y1: 0, x2: 0, y2: 1, stop: 0 #ffa02f, stop: 1 #d7801a);
}


QComboBox:on
{
    padding-top: 3px;
    padding-left: 4px;
    background-color: QLinearGradient( x1: 0, y1: 0, x2: 0, y2: 1, stop: 0 #2d2d2d, stop: 0.1 #2b2b2b, stop: 0.5 #292929, stop: 0.9 #282828, stop: 1 #252525);
    selection-background-color: #ffaa00;
}

QComboBox QAbstractItemView
{
    border: 2px solid darkgray;
    selection-background-color: QLinearGradient( x1: 0, y1: 0, x2: 0, y2: 1, stop: 0 #ffa02f, stop: 1 #d7801a);
}

QComboBox::drop-down
{
     subcontrol-origin: padding;
     subcontrol-position: top right;
     width: 15px;

     border-left-width: 0px;
     border-left-color: darkgray;
     border-left-style: solid; /* just a single line */
     border-top-right-radius: 3px; /* same radius as the QComboBox */
     border-bottom-right-radius: 3px;
 }

QComboBox::down-arrow
{
    image: url(:/down_arrow.png);
}


QGroupBox:focus
{
border: 2px solid QLinearGradient( x1: 0, y1: 0, x2: 0, y2: 1, stop: 0 #ffa02f, stop: 1 #d7801a);
}

QTextEdit:focus
{
    border: 2px solid QLinearGradient( x1: 0, y1: 0, x2: 0, y2: 1, stop: 0 #ffa02f, stop: 1 #d7801a);
}

QScrollBar:horizontal {
     border: 1px solid #222222;
     background: QLinearGradient( x1: 0, y1: 0, x2: 0, y2: 1, stop: 0.0 #121212, stop: 0.2 #282828, stop: 1 #484848);
     height: 7px;
     margin: 0px 16px 0 16px;
}

QScrollBar::handle:horizontal
{
      background: QLinearGradient( x1: 0, y1: 0, x2: 1, y2: 0, stop: 0 #ffa02f, stop: 0.5 #d7801a, stop: 1 #ffa02f);
      min-height: 20px;
      border-radius: 2px;
}

QScrollBar::add-line:horizontal {
      border: 1px solid #1b1b19;
      border-radius: 2px;
      background: QLinearGradient( x1: 0, y1: 0, x2: 1, y2: 0, stop: 0 #ffa02f, stop: 1 #d7801a);
      width: 14px;
      subcontrol-position: right;
      subcontrol-origin: margin;
}

QScrollBar::sub-line:horizontal {
      border: 1px solid #1b1b19;
      border-radius: 2px;
      background: QLinearGradient( x1: 0, y1: 0, x2: 1, y2: 0, stop: 0 #ffa02f, stop: 1 #d7801a);
      width: 14px;
     subcontrol-position: left;
     subcontrol-origin: margin;
}

QScrollBar::right-arrow:horizontal, QScrollBar::left-arrow:horizontal
{
      border: 1px solid black;
      width: 1px;
      height: 1px;
      background: white;
}

QScrollBar::add-page:horizontal, QScrollBar::sub-page:horizontal
{
      background: none;
}

QScrollBar:vertical
{
      background: QLinearGradient( x1: 0, y1: 0, x2: 1, y2: 0, stop: 0.0 #121212, stop: 0.2 #282828, stop: 1 #484848);
      width: 7px;
      margin: 16px 0 16px 0;
      border: 1px solid #222222;
}

QScrollBar::handle:vertical
{
      background: QLinearGradient( x1: 0, y1: 0, x2: 0, y2: 1, stop: 0 #ffa02f, stop: 0.5 #d7801a, stop: 1 #ffa02f);
      min-height: 20px;
      border-radius: 2px;
}

QScrollBar::add-line:vertical
{
      border: 1px solid #1b1b19;
      border-radius: 2px;
      background: QLinearGradient( x1: 0, y1: 0, x2: 0, y2: 1, stop: 0 #ffa02f, stop: 1 #d7801a);
      height: 14px;
      subcontrol-position: bottom;
      subcontrol-origin: margin;
}

QScrollBar::sub-line:vertical
{
      border: 1px solid #1b1b19;
      border-radius: 2px;
      background: QLinearGradient( x1: 0, y1: 0, x2: 0, y2: 1, stop: 0 #d7801a, stop: 1 #ffa02f);
      height: 14px;
      subcontrol-position: top;
      subcontrol-origin: margin;
}

QScrollBar::up-arrow:vertical, QScrollBar::down-arrow:vertical
{
      border: 1px solid black;
      width: 1px;
      height: 1px;
      background: white;
}


QScrollBar::add-page:vertical, QScrollBar::sub-page:vertical
{
      background: none;
}

QTextEdit
{
    background-color: #242424;
}

QPlainTextEdit
{
    background-color: #242424;
}

QHeaderView::section
{
    background-color: QLinearGradient(x1:0, y1:0, x2:0, y2:1, stop:0 #616161, stop: 0.5 #505050, stop: 0.6 #434343, stop:1 #656565);
    color: white;
    padding-left: 4px;
    border: 1px solid #6c6c6c;
}

QCheckBox:disabled
{
color: #414141;
}

QDockWidget::title
{
    text-align: center;
    spacing: 3px; /* spacing between items in the tool bar */
    background-color: QLinearGradient(x1:0, y1:0, x2:0, y2:1, stop:0 #323232, stop: 0.5 #242424, stop:1 #323232);
}

QDockWidget::close-button, QDockWidget::float-button
{
    text-align: center;
    spacing: 1px; /* spacing between items in the tool bar */
    background-color: QLinearGradient(x1:0, y1:0, x2:0, y2:1, stop:0 #323232, stop: 0.5 #242424, stop:1 #323232);
}

QDockWidget::close-button:hover, QDockWidget::float-button:hover
{
    background: #242424;
}

QDockWidget::close-button:pressed, QDockWidget::float-button:pressed
{
    padding: 1px -1px -1px 1px;
}

QMainWindow::separator
{
    background-color: QLinearGradient(x1:0, y1:0, x2:0, y2:1, stop:0 #161616, stop: 0.5 #151515, stop: 0.6 #212121, stop:1 #343434);
    color: white;
    padding-left: 4px;
    border: 1px solid #4c4c4c;
    spacing: 3px; /* spacing between items in the tool bar */
}

QMainWindow::separator:hover
{

    background-color: QLinearGradient(x1:0, y1:0, x2:0, y2:1, stop:0 #d7801a, stop:0.5 #b56c17 stop:1 #ffa02f);
    color: white;
    padding-left: 4px;
    border: 1px solid #6c6c6c;
    spacing: 3px; /* spacing between items in the tool bar */
}

QToolBar::handle
{
     spacing: 3px; /* spacing between items in the tool bar */
    background: url(:/images/handle.png);
}

QMenu::separator
{
    height: 2px;
    background-color: QLinearGradient(x1:0, y1:0, x2:0, y2:1, stop:0 #161616, stop: 0.5 #151515, stop: 0.6 #212121, stop:1 #343434);
    color: white;
    padding-left: 4px;
    margin-left: 10px;
    margin-right: 5px;
}

QProgressBar
{
    border: 2px solid grey;
    border-radius: 5px;
    text-align: center;
}

QProgressBar::chunk
{
    background-color: #d7801a;
    width: 2.15px;
    margin: 0.5px;
}

QTabBar::tab {
    color: #b1b1b1;
    border: 1px solid #444;
    border-bottom-style: none;
    background-color: #323232;
    padding-left: 10px;
    padding-right: 10px;
    padding-top: 3px;
    padding-bottom: 2px;
    margin-right: -1px;
}

QTabWidget::pane {
    border: 1px solid #444;
    top: 1px;
}

QTabBar::tab:last
{
    margin-right: 0; /* the last selected tab has nothing to overlap with on the right */
    border-top-right-radius: 3px;
}

QTabBar::tab:first:!selected
{
 margin-left: 0px; /* the last selected tab has nothing to overlap with on the right */


    border-top-left-radius: 3px;
}

QTabBar::tab:!selected
{
    color: #b1b1b1;
    border-bottom-style: solid;
    margin-top: 3px;
    background-color: QLinearGradient(x1:0, y1:0, x2:0, y2:1, stop:1 #212121, stop:.4 #343434);
}

QTabBar::tab:selected
{
    border-top-left-radius: 3px;
    border-top-right-radius: 3px;
    margin-bottom: 0px;
}

QTabBar::tab:!selected:hover
{
    /*border-top: 2px solid #ffaa00;
    padding-bottom: 3px;*/
    border-top-left-radius: 3px;
    border-top-right-radius: 3px;
    background-color: QLinearGradient(x1:0, y1:0, x2:0, y2:1, stop:1 #212121, stop:0.4 #343434, stop:0.2 #343434, stop:0.1 #ffaa00);
}

QRadioButton::indicator:checked, QRadioButton::indicator:unchecked{
    color: #b1b1b1;
    background-color: #323232;
    border: 1px solid #b1b1b1;
    border-radius: 6px;
}

QRadioButton::indicator:checked
{
    background-color: qradialgradient(
        cx: 0.5, cy: 0.5,
        fx: 0.5, fy: 0.5,
        radius: 1.0,
        stop: 0.25 #ffaa00,
        stop: 0.3 #323232
    );
}

QCheckBox::indicator{
    color: #b1b1b1;
    background-color: #323232;
    border: 1px solid #b1b1b1;
    width: 9px;
    height: 9px;
}

QRadioButton::indicator
{
    border-radius: 6px;
}

QRadioButton::indicator:hover, QCheckBox::indicator:hover
{
    border: 1px solid #ffaa00;
}

QCheckBox::indicator:checked
{
   image:url(:/images/checkbox.png);
}


QCheckBox::indicator:disabled, QRadioButton::indicator:disabled
{
    border: 1px solid #444;
}

QSlider::groove:horizontal {
border: 1px solid #bbb;
background: white;
height: 10px;
border-radius: 4px;
}

QSlider::sub-page:horizontal {
background: qlineargradient(x1: 0, y1: 0,    x2: 0, y2: 1,
    stop: 0 #66e, stop: 1 #bbf);
background: qlineargradient(x1: 0, y1: 0.2, x2: 1, y2: 1,
    stop: 0 #ffbc6d, stop: 1 #d7801a);
border: 1px solid #777;
height: 10px;
border-radius: 4px;
}

QSlider::add-page:horizontal {
background: #fff;
border: 1px solid #777;
height: 10px;
border-radius: 4px;
}

QSlider::handle:horizontal {
background: qlineargradient(x1:0, y1:0, x2:1, y2:1,
    stop:0 #eee, stop:1 #ccc);
border: 1px solid #777;
width: 13px;
margin-top: -2px;
margin-bottom: -2px;
border-radius: 4px;
}

QSlider::handle:horizontal:hover {
background: qlineargradient(x1:0, y1:0, x2:1, y2:1,
    stop:0 #fff, stop:1 #ddd);
border: 1px solid #444;
border-radius: 4px;
}

QSlider::sub-page:horizontal:disabled {
background: #bbb;
border-color: #999;
}

QSlider::add-page:horizontal:disabled {
background: #eee;
border-color: #999;
}

QSlider::handle:horizontal:disabled {
background: #eee;
border: 1px solid #aaa;
border-radius: 4px;
}</string>
  </property>
  <widget class="QGroupBox" name="groupBox">
   <property name="geometry">
    <rect>
     <x>0</x>
     <y>0</y>
     <width>326</width>
     <height>52</height>
    </rect>
   </property>
   <property name="sizePolicy">
    <sizepolicy hsizetype="Preferred" vsizetype="Preferred">
     <horstretch>0</horstretch>
     <verstretch>0</verstretch>
    </sizepolicy>
   </property>
   <property name="title">
    <string>Parameter</string>
   </property>
   <layout class="QHBoxLayout" name="horizontalLayout">
    <property name="sizeConstraint">
     <enum>QLayout::SetDefaultConstraint</enum>
    </property>
    <item>
     <widget class="QLabel" name="label">
      <property name="sizePolicy">
       <sizepolicy hsizetype="Preferred" vsizetype="Expanding">
        <horstretch>0</horstretch>
        <verstretch>0</verstretch>
       </sizepolicy>
      </property>
      <property name="text">
       <string>TextLabel</string>
      </property>
      <property name="alignment">
       <set>Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter</set>
      </property>
      <property name="wordWrap">
       <bool>true</bool>
      </property>
     </widget>
    </item>
    <item>
     <widget class="QSlider" name="horizontalSlider">
      <property name="sizePolicy">
       <sizepolicy hsizetype="Expanding" vsizetype="Expanding">
        <horstretch>0</horstretch>
        <verstretch>0</verstretch>
       </sizepolicy>
      </property>
      <property name="minimumSize">
       <size>
        <width>0</width>
        <height>0</height>
       </size>
      </property>
      <property name="orientation">
       <enum>Qt::Horizontal</enum>
      </property>
      <property name="invertedAppearance">
       <bool>false</bool>
      </property>
     </widget>
    </item>
    <item>
     <widget class="QLabel" name="label_2">
      <property name="sizePolicy">
       <sizepolicy hsizetype="Preferred" vsizetype="Expanding">
        <horstretch>0</horstretch>
        <verstretch>0</verstretch>
       </sizepolicy>
      </property>
      <property name="text">
       <string>TextLabel</string>
      </property>
      <property name="wordWrap">
       <bool>true</bool>
      </property>
     </widget>
    </item>
    <item>
     <widget class="QLineEdit" name="lineEdit">
      <property name="sizePolicy">
       <sizepolicy hsizetype="Expanding" vsizetype="Expanding">
        <horstretch>0</horstretch>
        <verstretch>0</verstretch>
       </sizepolicy>
      </property>
      <property name="minimumSize">
       <size>
        <width>0</width>
        <height>0</height>
       </size>
      </property>
      <property name="maximumSize">
       <size>
        <width>50</width>
        <height>16777215</height>
       </size>
      </property>
     </widget>
    </item>
   </layout>
  </widget>
 </widget>
 <resources/>
 <connections/>
</ui>

ParamBlock.h:

#ifndef PARAMBLOCK_H
#define PARAMBLOCK_H

#include <QWidget>

namespace Ui {
class ParamBlock;
}

class ParamBlock : public QWidget
{
    Q_OBJECT

public:
    explicit ParamBlock(QWidget *parent = 0);
    void changeTitle(QString X);
    void changeMin(int X);
    void changeMax(int X);
    void setValue(int X);
    ~ParamBlock();

private slots:

    void on_lineEdit_textEdited(const QString &arg1);

    void on_horizontalSlider_valueChanged(int value);

private:
    Ui::ParamBlock *ui;
};

#endif // PARAMBLOCK_H

我想得到的是,滚动区域可以执行它应该执行的操作(如果它不适合而不是重叠所有内容,请让我滚动它)或者正确调整窗口小部件的大小。

我尝试了大量使用SizePolicies的东西但是没有成功。我也尝试了一个没有成功的QGridLayout(结果相同)。

在Windows 7 32位上使用Qt Creator 3.5.0和Qt 5.5.0。

非常感谢帮助:)

2 个答案:

答案 0 :(得分:1)

您的问题是ParamBlock中缺少顶级布局。组框中有一个布局,但底层小部件本身没有布局。一旦你设置它,它都会工作。

以下是ParamBlock的最低独立实施方式。

注意使用QLabel::setNumqBound,按值(不是通过指针)保存成员,缺少硬编码大小,利用相互更新,错误检查和使用现代语法。

class ParamBlock : public QWidget {
   Q_OBJECT
   Q_PROPERTY(int min READ min WRITE setMin)
   Q_PROPERTY(int max READ max WRITE setMax)
   Q_PROPERTY(int value READ value WRITE setValue USER true)
   QHBoxLayout top { this };
   QGroupBox box;
   QHBoxLayout layout { &box };
   QSlider slider;
   QLabel minLabel, maxLabel;
   QLineEdit edit;
   void on_edit_textEdited(const QString & text) {
      bool ok;
      auto value = text.toInt(&ok);
      if (!ok) return;
      auto newValue = qBound(slider.minimum(), value, slider.maximum());
      slider.setValue(newValue);
   }
   void on_slider_valueChanged(int value) {
      edit.setText(QString::number(value));
   }
   bool event(QEvent * ev) Q_DECL_OVERRIDE {
      if (ev->type() == QEvent::FontChange) fontChangeEvent();
      return QWidget::event(ev);
   }
   void fontChangeEvent() {
      QFontMetrics fm { font() };
      edit.setFixedWidth(edit.minimumSizeHint().width() +
                         fm.width(QString(edit.maxLength()-1, QLatin1Char('0'))));
   }
public:
   ParamBlock(QWidget * parent = 0) : QWidget(parent) {
      top.addWidget(&box);
      top.setMargin(0);
      layout.addWidget(&minLabel);
      layout.addWidget(&slider);
      layout.addWidget(&maxLabel);
      layout.addWidget(&edit);
      slider.setOrientation(Qt::Horizontal);
      slider.setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::Fixed);
      edit.setMaxLength(3);
      edit.setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Preferred);
      connect(&edit, &QLineEdit::textEdited, this, &ParamBlock::on_edit_textEdited);
      connect(&slider, &QSlider::valueChanged, this, &ParamBlock::on_slider_valueChanged);
      fontChangeEvent();
      setMin(min());
      setMax(max());
   }
   int min() const { return slider.minimum(); }
   int max() const { return slider.maximum(); }
   int value() const { return slider.value(); }
   void setMin(int min) {
      minLabel.setNum(min);
      slider.setMinimum(min);
   }
   void setMax(int max) {
      maxLabel.setNum(max);
      slider.setMaximum(max);
   }
   void setValue(int val) {
      slider.setValue(val);
   }
   void setTitle(const QString & title) {
      box.setTitle(title);
   }
};

答案 1 :(得分:0)

以下是解决问题的最小示例:

#include <QApplication>
#include <QMainWindow>
#include <QScrollArea>
#include <QGridLayout>
#include <QPushButton>

int main(int argc, char *argv[])
{
  QApplication a(argc, argv);

  QMainWindow* mainWindow = new QMainWindow(0);
  QScrollArea* scrollArea = new QScrollArea(mainWindow);
  mainWindow->setCentralWidget( scrollArea );
  QWidget* widget = new QWidget( mainWindow );
  QGridLayout *gridLayout = new QGridLayout( widget );
  for ( int i = 0; i < 3; i++ )
  {
    for ( int j = 0; j < 3; j++ )
    {
      QPushButton* button = new QPushButton( 
        QString("Row %1 Col %2").arg(i).arg(j), mainWindow );
      gridLayout->addWidget( button, i, j );
    }
  }
  scrollArea->setWidget( widget );
  mainWindow->show();

  return a.exec();
}