[Qt][C++] setMinimumWidth和setMaximumWidth不在标签上起作用

huangapple go评论121阅读模式
英文:

[Qt][C++] setMinimumWidth and setMaximumWidth not working on label

问题

I have label created and want tooltip over it, I want to set tooltip's maximum and minimum width but somehow its not working.
我已经创建了标签并想在其上添加工具提示,我想设置工具提示的最大和最小宽度,但不知何故它不起作用。
I am not expert to QT, not able to figure out the reason.
我不是QT的专家,无法找出原因。

Code:

  1. #include "widget.h"
  2. #include <QApplication>
  3. #include <QListWidget>
  4. #include <QListWidgetItem>
  5. #include <QLabel>
  6. #include <QHBoxLayout>
  7. int main(int argc, char *argv[])
  8. {
  9. QApplication a(argc, argv);
  10. QListWidget listWidget;
  11. listWidget.setContentsMargins(QMargins(0, 0, 0, 0));
  12. for (int i = 0; i < 5; ++i)
  13. {
  14. QListWidgetItem* item = new QListWidgetItem();
  15. auto *itemWidget = new QWidget();
  16. auto *textLabel = new QLabel(QString("Item %1").arg(i + 1), itemWidget);
  17. textLabel->setMinimumWidth(100); //Not working whatever value I set
  18. textLabel->setMaximumWidth(400); //Not working whatever value I set
  19. textLabel->setToolTip("<p>This is the looooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooonggggggggggggggg name </p>");
  20. listWidget.addItem(item);
  21. listWidget.setItemWidget(item, itemWidget);
  22. }
  23. listWidget.show();
  24. return a.exec();
  25. }

Tooltip:
工具提示:

英文:

I have label created and want tooltip over it, I want to set tooltip's maximum and minimum width but somehow its not working.
I am not expert to QT, not able to figure out the reason.

Code:

  1. #include &quot;widget.h&quot;
  2. #include &lt;QApplication&gt;
  3. #include &lt;QListWidget&gt;
  4. #include &lt;QListWidgetItem&gt;
  5. #include &lt;QLabel&gt;
  6. #include &lt;QHBoxLayout&gt;
  7. int main(int argc, char *argv[])
  8. {
  9. QApplication a(argc, argv);
  10. QListWidget listWidget;
  11. listWidget.setContentsMargins(QMargins(0, 0, 0, 0));
  12. for (int i = 0; i &lt; 5; ++i)
  13. {
  14. QListWidgetItem* item = new QListWidgetItem();
  15. auto *itemWidget = new QWidget();
  16. auto *textLabel = new QLabel(QString(&quot;Item %1&quot;).arg(i + 1), itemWidget);
  17. textLabel-&gt;setMinimumWidth(100); //Not working whatever value I set
  18. textLabel-&gt;setMaximumWidth(400); //Not working whatever value I set
  19. textLabel-&gt;setToolTip(&quot;&lt;p&gt;This is the looooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooonggggggggggggggg name &lt;/p&gt;&quot;);
  20. listWidget.addItem(item);
  21. listWidget.setItemWidget(item, itemWidget);
  22. }
  23. listWidget.show();
  24. return a.exec();
  25. }

Tooltip:

[Qt][C++] setMinimumWidth和setMaximumWidth不在标签上起作用

can someone please help.

答案1

得分: 1

  1. 不能直接设置工具提示的最大和最小值,因此您应该间接地完成这个任务:
  2. ```cpp
  3. static const QString FORMATTER = "&lt;p&gt;%1&lt;/p&gt;";
  4. QString tooltip =
  5. "This is the "
  6. "looooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo"
  7. "oooooooooooooooooonggggggggggggggg name.";
  8. static constexpr auto maximum = 10;
  9. textLabel->setToolTip(FORMATTER.arg(tooltip.mid(0, maximum)));

更新:

如果您确切地希望拥有任何工具提示的属性,您可以重写事件并显示重新实现的 ToolTipWidget。
例如:https://stackoverflow.com/questions/31328957/qt-widget-inside-tooltip

  1. <details>
  2. <summary>英文:</summary>
  3. You cant directly set max and min on tooltip , hence you should indrectly do that(for this usecase):
  4. ```cpp
  5. static const QString FORMATTER = &quot;&lt;p&gt;%1&lt;/p&gt;&quot;;
  6. QString tooltip =
  7. &quot;This is the &quot;
  8. &quot;looooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo&quot;
  9. &quot;oooooooooooooooooonggggggggggggggg name.&quot;;
  10. static constexpr auto maximum = 10;
  11. textLabel-&gt;setToolTip(FORMATTER.arg(tooltip.mid(0, maximum)));

Update:

if you want exactly have your widget with any properties for tooltip , you can override your events and show the ToolTipWidget that reimplemented.
i.e: https://stackoverflow.com/questions/31328957/qt-widget-inside-tooltip

答案2

得分: 0

  1. textLabel-&gt;setMinimumWidth(100);
  2. textLabel-&gt;setMaximumWidth(400);

这将设置标签自身的最小和最大宽度,而不是工具提示。

如果您想自定义工具提示行为,您需要重写event()函数来捕获QEvent::ToolTip(可能还有QEvent::ToolTipChange),然后使用QToolTip::showText()来自行绘制工具提示。

  1. <details>
  2. <summary>英文:</summary>
  3. textLabel-&gt;setMinimumWidth(100);
  4. textLabel-&gt;setMaximumWidth(400);
  5. This will set the min/max width of the label itself not the tooltip.
  6. If you want to customize tooltip behavior, you&#39;ll have to override the [event()](https://doc.qt.io/qt-6/qwidget.html#event) function to catch `QEvent::ToolTip` (and probably `QEvent::ToolTipChange`) and draw it yourself using [QToolTip::showText()](https://doc.qt.io/qt-6/qtooltip.html#showText)
  7. </details>
  8. # 答案3
  9. **得分**: -1
  10. 请使用:textLabel-&gt;setToolTip
  11. <details>
  12. <summary>英文:</summary>
  13. please use :textLabel-&gt;setToolTip
  14. </details>

huangapple
  • 本文由 发表于 2023年2月8日 16:44:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/75383203.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定