首页 文章

QScrollArea中没有任何内容

提问于
浏览
0

我有一个很好的小部件,基本上看起来像一个带有一堆QSliders的对话框 . 滑块的数量取决于调用对话框(不是实际的QDialog;只是一个QWidget)时的情况 .

由于不同数量的滑块导致盒子在不同时间的大小不同,我现在想通过将滑块限制在QScrollArea来清理一些东西 . 如果我理解正确的话,这样的滚动区域会显示许多滑块适合其高度,如果有更多,可以向下滚动以查看其余部分 .

无论如何,我尝试了这样一个(有点复杂)的程序:

在自定义QWidget类的构造函数中(m_variableName =成员变量):

CustomScrollBox::CustomScrollBox(QWidget* _parent){

  setWindowTitle(...);
  ...

  m_scrollArea = new QScrollArea(this);
  m_scrollAreaBox = new QGroupBox(m_scrollArea);
  m_layout = new QGridLayout();
  m_scrollAreaBox->setLayout(m_layout);
  m_scrollArea->setWidget(m_scrollAreaBox);
  m_scrollArea->setFixedHeight(250);

  m_bottomButton = new QPushButton(this); //probably irrelevant
  ...
  [connect calls, etc.]
}

在构造函数之后,会发生滑块的真实,依赖于情境的设置:

void
CustomScrollBox::SetUpWidgets(){

  for([however many sliders the situation calls for]){
    CustomSlider* s = new CustomSlider(this, label);   //just a QWidget consisting of a 
                                                       //QSlider and a QLabel to 
                                                       //the left of it
    ..
    m_layout->addWidget(s, [grid dimensions as needed]);  
  }

  ...
  [set text on bottom button, etc., and add it as well]
}

除了左侧的固定滚动条之外,此过程不会在整个对话框中显示任何内容 . 如果可能的话,初始化步骤的正确顺序是什么?我的猜测是,我可能在错误的时间给出了错误的父母或设置了布局,但到目前为止我尝试过的后方没有奏效......

1 回答

  • 1

    首先,您不需要为子窗口小部件和布局创建显式成员到CustomScrollBox,除非您以后需要访问它们(即使这样您也可以通过它们与CustomScrollBox的子关系来跟踪它们) . 特别是,设置了窗口小部件的布局后,您可以使用QWidget :: layout获取QLayout *并将其向下转换为QGridLayout *或QVBoxLayout * . 其次,你正在为大多数儿童小部件提供父母 . 通常你不应该这样做,例如添加小部件的布局将获得所有权,即布局将成为添加的小部件的父级 . 以下原则上我会做什么 . 它至少会指向一个更好的方向 .

    CustomScrollBox::CustomScrollBox(QWidget* parent)
    : QWidget(parent)
    {
    
      setWindowTitle(...);
      ...
      QVBoxLayout* vBoxLayout(new QVBoxLayout);
      QScrollArea* scrollArea(new QScrollArea);      
      vBoxLayout->addWidget(scrollArea);
      QGroupBox* groupBox(new QGroupBox);
      QGridLayout* gridLayout(new QGridLayout);
      gridLayout->addWidget(.../*whatever buttons etc*/)
      groupBox->setLayout(gridLayout);
      scrollArea->setWidget(groupBox);
      setLayout(vBoxLayout);    
      ...
      [connect calls, etc.]
    }
    

相关问题