2

我有一个StackedWidget继承自QStackedWidget. 的标准行为QStackedWidget是它采用其最大元素的大小。我想要做的是强制它调整到它的当前元素。我已经尝试了一些在这里找到的解决方案,但它们都不起作用,包括设置大小策略或调用hide()所有看不见的小部件。

我认为问题可能存在于两种可能性: 1. 出现严重问题StackedWidget。2. 调整大小的问题不直接与StackedWidget某些布局或StackedWidget嵌套在其中的另一个小部件有关。

  1. 关于第一种可能性,我在这里提供StackedWidget课程。

请看一下我StackedWidget的声明是什么样的:

  class StackedWidget : public QStackedWidget
  {
    Q_OBJECT
    private:
      QComboBox* widgetChooser = nullptr;
    public:
      StackedWidget(QWidget* parent) : QStackedWidget(parent) {}
      void setWidgetChooser(QComboBox* widgetChooser);
      void addWidget(QWidget* widget);
    private:
      void makeConnection();
    signals:
    public slots:
      void setCurrentIndex(const int& index);
  };

定义如下:

    void StackedWidget::setWidgetChooser(QComboBox* widgetChooser)
    {
      if (widgetChooser == nullptr) throw RuntimeError("widgetChooser is nullptr");
      this->widgetChooser = widgetChooser;
      makeConnection();
    }

    void StackedWidget::addWidget(QWidget* widget)
    {
      widget->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
      QStackedWidget::addWidget(widget);
    }

    void StackedWidget::makeConnection()
    {
      connect(widgetChooser, SIGNAL(currentIndexChanged(int)), this, SLOT(setCurrentIndex(int)));
    }

    void StackedWidget::setCurrentIndex(const int& index)
    {
      qDebug() << "Index changed to " << index;
      QWidget* pWidget = widget(index);
      Q_ASSERT(pWidget);
      QStackedWidget::setCurrentWidget(pWidget);
      pWidget->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
      pWidget->adjustSize();
      adjustSize();
    }
  1. 关于第二种可能性,这里是我如何使用StackedWidget.
void SchemaWidget::TempBuildAlt(const SchemaElement* son, QVBoxLayout* parentLayout)
  {
    if (auto schemaAlternative = dynamic_cast<const SchemaAlternatives*>(son))
    {
      QComboBox* widgetBox = BuildWidgetChooserBox(schemaAlternative);
      StackedWidget* stackedWidget = BuildStackedWidget(schemaAlternative);
      stackedWidget->setWidgetChooser(widgetBox);

      QHBoxLayout* boxLayout = new QHBoxLayout;
      boxLayout->addWidget(new QLabel(schemaAlternative->Name, this));
      boxLayout->addWidget(widgetBox);

      QVBoxLayout* layout = new QVBoxLayout;
      layout->addLayout(boxLayout);
      layout->addWidget(stackedWidget);

      parentLayout->addLayout(layout);
    } else throw RuntimeError("Cannot cast son to SchemaAlternatives.");
  }

  QComboBox* SchemaWidget::BuildWidgetChooserBox(const SchemaAlternatives* schema)
  {
    QComboBox* alternativesBox = new QComboBox(this);
    QStringListModel* listModel = new QStringListModel(this);
    QStringList list;
    for (int i = 1; i <= schema->Sons.High(); ++i)
    {
      if (const SchemaTree* son = dynamic_cast<const SchemaTree*>(schema->Sons(i).Get()))
      {
        list << son->Name;
      }
      else throw RuntimeError("Son cannot be cast to SchemaTree.");
    }
    listModel->setStringList(list);
    alternativesBox->setModel(listModel);
    return alternativesBox;
  }

  StackedWidget* SchemaWidget::BuildStackedWidget(const SchemaAlternatives* schema)
  {
    StackedWidget* stackedWidget = new StackedWidget(this);
    for (int i = 1; i <= schema->Sons.High(); ++i)
    {
      if (const SchemaTree* alternativeSon = dynamic_cast<const SchemaTree*>(schema->Sons(i).Get()))
      {
        QWidget* widget = new QWidget(this);
        QVBoxLayout* widgetLayout = new QVBoxLayout;
        BuildWidget(alternativeSon, widgetLayout);
        widget->setLayout(widgetLayout);
        stackedWidget->addWidget(widget);
      }
      else throw RuntimeError("Son could not be cast to SchemaTree.");
    }
    return stackedWidget;
  }

我想提前对愿意花一些时间解决我的问题的人表示感谢。我很感激。谢谢。你很棒。

4

0 回答 0