2015-09-08 78 views
0

所以我有一個程序從一個文件讀取參數並圍繞這些參數構建一個UI。參數需要一些控件等,所以我做了一個小部件: WidgetQt:小部件在scrollarea中重疊

它將被添加X數量的時間取決於文件中的X數量參數。如果參數的數量是足夠小程序看起來很好,像這樣: program normal

但是,如果有太多的參數,他們將重疊,即使它們被放置在一個scrollarea: 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 

我想明白的是,無論是scrollarea做什麼它應該這樣做(如果它不適合而不是重疊所有內容,請讓我滾動它)或正確調整窗口小部件的大小。

我試了很多SizePolicies的東西,但沒有解決。我也嘗試了一個沒有成功的QGridLayout(相同的結果)。

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

幫助,將不勝感激:)

+0

首先,你也可以使用,而不是在hboxlayouts vboxlayouts一個QGridLayout。 :-) – Gombat

+0

我開始使用QGridLayout。這給了我現在的結果,但我會再次嘗試。 –

+0

這只是一個建議。我不認爲這會改變其中物品大小的行爲。這只是更容易編寫和閱讀。 – Gombat

回答

1

你的問題是ParamBlock缺乏頂層設計。組合框中有一個佈局,但底層控件本身沒有佈局。一旦你設置好了,它就會工作。

下面是ParamBlock最小的獨立實現的樣子。

請注意使用QLabel::setNum,qBound,按值保存成員(不是指針),缺少硬編碼大小,利用相互更新,錯誤檢查和使用現代語法。

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); 
    } 
}; 
+0

我知道它必須是我錯過的小事。謝謝! –

+1

@BenNon有時,這就是你使用'.ui'文件獲取簡單小部件的原因。我已經包含了一個示例,說明如何以最少的代碼實現該小部件。 –

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(); 
} 
+0

您不需要將'mainWindow'作爲參數傳遞給按鈕。事實上,你根本不需要一個主窗口。僅僅因爲Qt Creator模板有它,並不意味着你需要它。這很愚蠢。實際上,在90%的情況下,每個人都使用「QMainWindow」。 –

+0

我不認爲你解決了任何問題,因爲你不使用OP的'ParamBlock'小部件......這就是問題所在。 –

+0

確實,不起作用。查看我更新的代碼。 –