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

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

[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:

#include "widget.h"
#include <QApplication>
#include <QListWidget>
#include <QListWidgetItem>
#include <QLabel>
#include <QHBoxLayout>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    QListWidget listWidget;
    listWidget.setContentsMargins(QMargins(0, 0, 0, 0));

    for (int i = 0; i < 5; ++i)
    {
        QListWidgetItem* item = new QListWidgetItem();
        auto *itemWidget = new QWidget();
        auto *textLabel = new QLabel(QString("Item %1").arg(i + 1), itemWidget);
        textLabel->setMinimumWidth(100); //Not working whatever value I set
        textLabel->setMaximumWidth(400); //Not working whatever value I set
        textLabel->setToolTip("<p>This is the looooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooonggggggggggggggg name </p>");

        listWidget.addItem(item);
        listWidget.setItemWidget(item, itemWidget);
    }

    listWidget.show();

    return a.exec();
}

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:

#include &quot;widget.h&quot;
#include &lt;QApplication&gt;
#include &lt;QListWidget&gt;
#include &lt;QListWidgetItem&gt;
#include &lt;QLabel&gt;
#include &lt;QHBoxLayout&gt;

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    QListWidget listWidget;
    listWidget.setContentsMargins(QMargins(0, 0, 0, 0));


    for (int i = 0; i &lt; 5; ++i)
    {
        QListWidgetItem* item = new QListWidgetItem();
        auto *itemWidget = new QWidget();
        auto *textLabel = new QLabel(QString(&quot;Item %1&quot;).arg(i + 1), itemWidget);
        textLabel-&gt;setMinimumWidth(100); //Not working whatever value I set
        textLabel-&gt;setMaximumWidth(400); //Not working whatever value I set
        textLabel-&gt;setToolTip(&quot;&lt;p&gt;This is the looooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooonggggggggggggggg name &lt;/p&gt;&quot;);

        listWidget.addItem(item);
        listWidget.setItemWidget(item, itemWidget);
    }

    listWidget.show();

    return a.exec();
}

Tooltip:

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

can someone please help.

答案1

得分: 1

不能直接设置工具提示的最大和最小值,因此您应该间接地完成这个任务:

```cpp
static const QString FORMATTER = "&lt;p&gt;%1&lt;/p&gt;";
QString tooltip =
    "This is the "
    "looooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo"
    "oooooooooooooooooonggggggggggggggg name.";
static constexpr auto maximum = 10;
textLabel->setToolTip(FORMATTER.arg(tooltip.mid(0, maximum)));

更新:

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


<details>
<summary>英文:</summary>

You cant directly set max and min on tooltip , hence you should indrectly do that(for this usecase):
 
```cpp
static const QString FORMATTER = &quot;&lt;p&gt;%1&lt;/p&gt;&quot;;
QString tooltip =
    &quot;This is the &quot;
    &quot;looooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo&quot;
    &quot;oooooooooooooooooonggggggggggggggg name.&quot;;
static constexpr auto maximum = 10;
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

textLabel-&gt;setMinimumWidth(100);
textLabel-&gt;setMaximumWidth(400);

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

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


<details>
<summary>英文:</summary>

    textLabel-&gt;setMinimumWidth(100);
    textLabel-&gt;setMaximumWidth(400);

This will set the min/max width of the label itself not the tooltip.

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)

</details>



# 答案3
**得分**: -1

请使用:textLabel-&gt;setToolTip

<details>
<summary>英文:</summary>

please use :textLabel-&gt;setToolTip

</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:

确定