2

I have a class StackedWidget which inherits from QStackedWidget. The standard behaviour of QStackedWidget is that it adopts the size of its biggest element. What I want to do is force it to resize to its current element. I have already tried a couple of solutions found here and none of them work including e.g. setting size policies or calling hide() on all the widgets that go out of sight.

I think problem may reside in two possibilities: 1. Something is terribly wrong with StackedWidget. 2. The problem of resizing does not pertain directly to StackedWidget but to some layout or another widget that StackedWidget is nested in.

  1. Concerning the first possibility, here I provide the StackedWidget class.

Please take a look at what my StackedWidget's declaration looks like:

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

Here goes the definition:

    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. Concering the second possibility, here goes how I use 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;
  }

I want to say thanks in advance to anyone who will be willing to spend some time on my problem. I do appreciate it. Thanks. You're great.

slavic-d
  • 33
  • 3
  • Have you tried just overriding [`QWidget::sizeHint`](https://doc.qt.io/qt-5/qwidget.html#sizeHint-prop) in your `StackedWidget` class? Have it return the size hint associated with the current widget. – G.M. Mar 24 '20 at 13:26
  • I have overriden it and it did nothing. Is it just a matter of overriding or do I have to call it somewhere? I presume it is done automatically somewhere without programmer's influence but I just want to have it clear. – slavic-d Mar 24 '20 at 13:32
  • Yes, it would be called automatically by Qt's layout management code. – G.M. Mar 24 '20 at 14:08

0 Answers0