2014-02-14 132 views
1

我在QWidget内有QGridLayout。我一直在向此添加子女QGridLayouts。我希望QWidget根据子布局所需的大小调整大小,但用户无法调整大小。Qt QWidget禁用重新调整大于所需的最小大小

MainWindow.cpp(MainWindowQWidget继承)

MainWindow::MainWindow(QWidget *parent) : 
    QWidget(parent) 
{ 
    QGridLayout *mainLayout = new QGridLayout(); 
    { 
     AAController *ac1 = new AAController("Ins1"); 
     AAController *ac2 = new AAController("Ins2"); 
     AAController *ac3 = new AAController("Ins3"); 

     mainLayout->addLayout(ac1, 0, 0); 
     mainLayout->addLayout(ac2, 0, 1); 
     mainLayout->addLayout(ac3, 1, 1); 
    } 
    setLayout(mainLayout); 
} 

AAController.cpp(AAControllerQGridLayout继承)

AAController::AAController(const QString& instrument) 
    :_instrument(instrument) 
{ 
    _lblInstrument = new QLabel(_instrument); 
    _lblMismatchThreshold = new QLabel("Mismatch threshold : "); 
    _lblQuoteVolume = new QLabel("Quote volume : "); 
    _btnUpdateAlgo = new QPushButton("Update Algo"); 
    _spnMismatchThreshold = new QSpinBox(); 
    _spnQuoteVolume = new QSpinBox(); 

    this->addWidget(_lblInstrument, 0, 1, 1, 2, Qt::AlignCenter); 
    this->addWidget(_lblMismatchThreshold, 1, 0, 1, 2); 
    this->addWidget(_lblQuoteVolume, 2, 0, 1, 2); 
    this->addWidget(_btnUpdateAlgo, 3, 2, 1, 2); 
    this->addWidget(_spnMismatchThreshold, 1, 3); 
    this->addWidget(_spnQuoteVolume, 2, 3); 

    setSizeConstraint(SetFixedSize); 
} 

我得到的是这样的:

但此刻我可以改变它的大小,如:

enter image description here

我想禁用此类调整大小。我该怎么做呢?

+0

你的主窗口正好被设置为固定大小。添加子项时,将其设置为最小值,然后将其设置回固定大小。 –

+0

可能不会重复,因为它没有StatusBar也没有QDialog。 –

+0

@SebastianLange:您的方法会产生一个固定大小的窗口。但大于上面第一张图片中的最小值。 – nakiya

回答

2

在主窗口试试这个:

this->layout()->setSizeConstraint(QLayout::SetFixedSize); 

这是结果我得到:

enter image description here

+0

请看看上面的评论SebastianLange – nakiya

+0

@nakiya看我的编辑。这是你要找的结果吗? – thuga

+0

是的,我在找什么。谢谢。 – nakiya