默认情况下,我认为没有任何方法可以让标签文本跟随小部件标题。说了这句话,应该很容易通过压倒
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);
}
}
};
使用上面的类而不是
QTabWidget
将导致选项卡文本镜像与该选项卡关联的小部件的标题。