Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DockAreaWidget -> Invalid index when make use of dynamic tabs #670

Open
IMAN4K opened this issue Nov 3, 2024 · 0 comments
Open

DockAreaWidget -> Invalid index when make use of dynamic tabs #670

IMAN4K opened this issue Nov 3, 2024 · 0 comments
Assignees

Comments

@IMAN4K
Copy link

IMAN4K commented Nov 3, 2024

Description

Whenever we use the CDockManager::addDockWidgetTabToArea() API dynamically after invoking CDockManager::restoreState() we will face a warning related to indices:

void ads::CDockAreaWidget::setCurrentIndex(int) Invalid index

Expected behavior

We should be able to repeatedly Add/Remove dock widgets dynamically after invoking CDockManager::restoreState() on startup or QMainWindow::showEvent()

ADS version

v4.3.1-2-g952131a

Operating System

Debian GNU/Linux 12 (bookworm)

Reproducible Example

mainwindow.h:

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QtCore>
#include <QtWidgets>

#include <DockAreaWidget.h>
#include <DockManager.h>
#include <DockWidget.h>

class CMainWindow : public QMainWindow {
  Q_OBJECT

public:
  CMainWindow(QWidget *parent = nullptr);
  ~CMainWindow();

protected:
  void closeEvent(QCloseEvent *event) override;
  void showEvent(QShowEvent *event) override;

private:
  ads::CDockManager *DockManager;
  ads::CDockAreaWidget *CentralDockArea;
  ads::CDockWidget *DynamicTab;
  QSettings settings;
};

#endif // MAINWINDOW_H

mainwindow.cpp:

#include "mainwindow.h"

using namespace ads;

static constexpr auto kDockState = "dock-state";

CMainWindow::CMainWindow(QWidget *parent)
    : QMainWindow(parent),
      settings(QCoreApplication::applicationDirPath() + "/settings.ini",
               QSettings::IniFormat) {
  resize(720, 480);
  CDockManager::setConfigFlag(CDockManager::OpaqueSplitterResize, true);
  CDockManager::setConfigFlag(CDockManager::XmlCompressionEnabled, false);
  CDockManager::setConfigFlag(CDockManager::XmlAutoFormattingEnabled, true);
  CDockManager::setConfigFlag(CDockManager::FocusHighlighting, true);

  DockManager = new CDockManager(this);
  QLabel *label = new QLabel();
  label->setText("Central Dock Area");
  label->setAlignment(Qt::AlignCenter);
  CDockWidget *CentralDockWidget = new CDockWidget("CentralWidget");
  CentralDockWidget->setWidget(label);
  CentralDockWidget->setFeature(ads::CDockWidget::NoTab, true);
  CentralDockArea = DockManager->setCentralWidget(CentralDockWidget);

  CDockWidget *Tab1DockWidget = new CDockWidget("Tab 1");
  Tab1DockWidget->setWidget(new QLabel("Tab #01"));
  DockManager->addDockWidgetTabToArea(Tab1DockWidget, CentralDockArea);
}

CMainWindow::~CMainWindow() = default;

void CMainWindow::closeEvent(QCloseEvent *event) {
  if (DynamicTab) {
    DockManager->removeDockWidget(DynamicTab);
  }
  settings.setValue(kDockState, DockManager->saveState());
  QMainWindow::closeEvent(event);
}

void CMainWindow::showEvent(QShowEvent *event) {
  const auto xml = settings.value(kDockState).toByteArray();
  if (!xml.isEmpty()) {
    DockManager->restoreState(xml);
  }
  QTimer::singleShot(1000, this, [&]() {
    DynamicTab = new CDockWidget("Dynamic Tab");
    DynamicTab->setWidget(new QLabel("Dynamic Tab Widget"));
    // when xml isn't empty we'll see a warning:
    // void ads::CDockAreaWidget::setCurrentIndex(int) Invalid index 2
    DockManager->addDockWidgetTabToArea(DynamicTab, CentralDockArea);
  });
  QMainWindow::showEvent(event);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants