Mark
Mark

Reputation: 5132

Use setWindowModified() on QTabWidget

Because QTabWidget inherits QWidget we have setWindowModified() available. But it seems it doesn't work for the tab title:

ui->tab1->setWindowTitle(QString("%1[*]").arg(tr("Tab title")));
ui->tab1->setWindowModified(true);

but it doesn't show the '*' nor changes the tab text. Is there a way to handle this automatically instead of manually use setTabText()?

Upvotes: 0

Views: 383

Answers (1)

G.M.
G.M.

Reputation: 12898

I don't think there's any way to get the tab text to follow the widget title by default. Having said that, it should be very easy to fix up by overriding QTabWidget::tabInserted.

class tab_widget: public QTabWidget {
  using super      = QTabWidget;
  using this_class = tab_widget;
public:
  using super::super;
protected:
  virtual void tabInserted (int index) override
    {
      super::tabInserted(index);
      if (auto *w = widget(index)) {
        connect(w, &QWidget::windowTitleChanged, this, &this_class::handle_window_title_change);
      }
    }
  virtual void tabRemoved (int index) override
    {
      super::tabRemoved(index);
      if (auto *w = widget(index)) {
        disconnect(w, &QWidget::windowTitleChanged, this, &this_class::handle_window_title_change);
      }
    }
private:
  void handle_window_title_change (const QString &title)
    {
      if (auto *w = qobject_cast<QWidget *>(sender())) {
        setTabText(indexOf(w), title);
      }
    }
};

Using the above class rather than QTabWidget should result in the tab text mirroring the title of the widget associated with that tab.

Upvotes: 2

Related Questions