2012-06-05 66 views
3

我的目的是創建一個帶有QVBoxLayout的可滾動控件,它內部有各種控件(比如按鈕)。該控件放置在* .ui表單中。在構造該控件我寫了下面的代碼:Qt:「擴展」不適用於佈局

MyScrollArea::MyScrollArea(QWidget *parent) : 
     QScrollArea(parent) 
    { 
     // create the scrollable container 

     this->container = new QWidget(); // container widget member 
     this->container->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding); 
     this->container->setContentsMargins(QMargins(0,0,0,0)); 

     this->content = new QVBoxLayout(); // layout member 
     this->content->setMargin(0); 
     this->content->setSpacing(0); 

     for (int i=0; i<100; i++) 
     { 
      QPushButton * widget = new QPushButton(); 
      widget->setText("button"); 
      widget->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed); 
      this->content->addWidget(widget); 
     } 

     this->container->setLayout(this->content); 
     this->content->layout(); 

     this->setWidget(this->container); 
    } 

問題:按鈕具有固定的大小和不水平擴大。他們有一個固定的大小。我希望他們水平擴展以填充他們所在的行。我如何讓他們水平擴展父容器?

回答

4

嘗試撥打this->setWidgetResizable(true);

+2

對於佈局中的每個小部件都帶有「setMinimumHeight」技巧。謝謝。 – JasonGenX