@@ -127,8 +127,17 @@ void Tabbar::addTabWithIndex(int index, const QString &filePath, const QString &
127127 // 除去空白符 梁卫东 2020-08-26 14:49:15 ;适配助记符
128128 QString trimmedName = replaceMnemonic (tabName.simplified ());
129129 qDebug () << " trimmedName:" << trimmedName;
130+
131+ // Batch all layout-triggering operations to avoid jitter:
132+ // 1. Disable visual updates
133+ // 2. Insert tab (triggers QTabBar layout with old width → may overflow)
134+ // 3. Force DTabBar layout to resize inner QTabBar to correct width
135+ // 4. Force QTabBar to recalculate tab positions with correct width
136+ // 5. Re-enable visual updates → single paint with final state
137+ setUpdatesEnabled (false );
130138 DTabBar::insertTab (index, trimmedName);
131139 DTabBar::setCurrentIndex (index);
140+
132141 qDebug () << " filePath.contains(Utils::localDataPath())" ;
133142 if (filePath.contains (Utils::localDataPath ())) {
134143 if (Utils::isBackupFile (filePath) && !tipPath.isNull () && tipPath.length () > 0 ) {
@@ -159,6 +168,16 @@ void Tabbar::addTabWithIndex(int index, const QString &filePath, const QString &
159168 qDebug () << " path:" << path;
160169 setTabToolTip (index, path);
161170 }
171+
172+ // Force DTabBar's layout to resize inner QTabBar to its new sizeHint
173+ layout ()->activate ();
174+ // Force QTabBar internal layout recalculation with correct width
175+ setIconSize (iconSize ());
176+ // activate() may trigger scroll button show/hide cascade which
177+ // invalidates layout again; run a second time to settle completely
178+ layout ()->activate ();
179+
180+ setUpdatesEnabled (true );
162181}
163182
164183void Tabbar::resizeEvent (QResizeEvent *event)
@@ -826,23 +845,20 @@ QSize Tabbar::tabSizeHint(int index) const
826845 qDebug () << " Enter tabSizeHint, index:" << index;
827846 if (index >= 0 ) {
828847 int total = this ->width ();
829- qDebug () << " total:" << total;
830- // 计算每个tab平均宽度 返回 100到160
831- int aveargeWidth = 160 ;
832- aveargeWidth = total / DTabBar::count ();
833- qDebug () << " aveargeWidth:" << aveargeWidth;
834- if (aveargeWidth >= 160 ) {
835- qDebug () << " aveargeWidth >= 160" ;
836- aveargeWidth = 160 ;
837- } else if (aveargeWidth <= 110 ) {
838- qDebug () << " aveargeWidth <= 110" ;
839- aveargeWidth = 110 ;
848+ // 计算每个tab宽度:标签数未溢出时使用固定最大宽度,溢出时等分
849+ const int maxTabWidth = 160 ;
850+ const int minTabWidth = 110 ;
851+ int tabCount = DTabBar::count ();
852+ int tabWidth = maxTabWidth;
853+
854+ if (tabCount * maxTabWidth > total) {
855+ tabWidth = total / tabCount;
856+ tabWidth = qBound (minTabWidth, tabWidth, maxTabWidth);
840857 }
841- qDebug () << " aveargeWidth:" << aveargeWidth;
842858#ifdef DTKWIDGET_CLASS_DSizeMode
843- return QSize (aveargeWidth , DGuiApplicationHelper::isCompactMode () ? s_TabbarHeightCompact : s_TabbarHeight);
859+ return QSize (tabWidth , DGuiApplicationHelper::isCompactMode () ? s_TabbarHeightCompact : s_TabbarHeight);
844860#else
845- return QSize (aveargeWidth , 40 );
861+ return QSize (tabWidth , 40 );
846862#endif
847863 }
848864 qDebug () << " Exit tabSizeHint" ;
0 commit comments