@@ -37,11 +37,7 @@ void TipsWidget::setText(const QString &text)
3737 m_text = "བོད་སྐད་ཡིག་གཟུགས་ཚད་ལེན་ཚོད་ལྟའི་སྐོར་གྱི་རྗོད་ཚིག";
3838#endif
3939
40- setFixedSize (fontMetrics ().horizontalAdvance (m_text), fontMetrics ().boundingRect (m_text).height ());
41- if (parentWidget ()) {
42- parentWidget ()->adjustSize ();
43- }
44-
40+ updateGeometry ();
4541 update ();
4642
4743#ifndef QT_NO_ACCESSIBILITY
@@ -60,18 +56,7 @@ void TipsWidget::setTextList(const QStringList &textList)
6056 }
6157 m_textList = textList;
6258
63- int width = 0 ;
64- int height = 0 ;
65- for (const QString& text : m_textList) {
66- width = qMax (width, fontMetrics ().horizontalAdvance (text));
67- height += fontMetrics ().boundingRect (text).height ();
68- }
69-
70- setFixedSize (width, height);
71- if (parentWidget ()) {
72- parentWidget ()->adjustSize ();
73- }
74-
59+ updateGeometry ();
7560 update ();
7661}
7762
@@ -110,25 +95,45 @@ void TipsWidget::paintEvent(QPaintEvent *event)
11095 }
11196}
11297
98+ QSize TipsWidget::sizeHint () const
99+ {
100+ const QFontMetrics fm = fontMetrics ();
101+
102+ switch (m_type) {
103+ case SingleLine: {
104+ return QSize (fm.horizontalAdvance (m_text),
105+ fm.boundingRect (m_text).height ());
106+ }
107+ case MultiLine: {
108+ int maxWidth = 0 ;
109+ int totalHeight = 0 ;
110+ for (const QString& text : m_textList) {
111+ maxWidth = qMax (maxWidth, fm.horizontalAdvance (text));
112+ totalHeight += fm.boundingRect (text).height ();
113+ }
114+
115+ return QSize (maxWidth, totalHeight);
116+ }
117+ }
118+
119+ Q_UNREACHABLE_RETURN (QSize (0 , 0 ));
120+ }
121+
113122bool TipsWidget::event (QEvent *event)
114123{
115124 if (event->type () == QEvent::FontChange) {
116- switch (m_type) {
117- case SingleLine:
118- {
119- setText (m_text);
120- break ;
121- }
122- case MultiLine:
123- {
124- setTextList (m_textList);
125- break ;
126- }
127- }
125+ updateGeometry ();
126+ update ();
128127 } else if (event->type () == QEvent::MouseButtonRelease
129128 && static_cast <QMouseEvent *>(event)->button () == Qt::RightButton) {
130129 return true ;
131130 }
132131 return QFrame::event (event);
133132}
133+
134+ QSize TipsWidget::minimumSizeHint () const
135+ {
136+ return sizeHint ();
137+ }
138+
134139}
0 commit comments