英文:
[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 "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:
can someone please help.
答案1
得分: 1
不能直接设置工具提示的最大和最小值,因此您应该间接地完成这个任务:
```cpp
static const QString FORMATTER = "<p>%1</p>";
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 = "<p>%1</p>";
QString tooltip =
"This is the "
"looooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo"
"oooooooooooooooooonggggggggggggggg name.";
static constexpr auto maximum = 10;
textLabel->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->setMinimumWidth(100);
textLabel->setMaximumWidth(400);
这将设置标签自身的最小和最大宽度,而不是工具提示。
如果您想自定义工具提示行为,您需要重写event()函数来捕获QEvent::ToolTip
(可能还有QEvent::ToolTipChange
),然后使用QToolTip::showText()来自行绘制工具提示。
<details>
<summary>英文:</summary>
textLabel->setMinimumWidth(100);
textLabel->setMaximumWidth(400);
This will set the min/max width of the label itself not the tooltip.
If you want to customize tooltip behavior, you'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->setToolTip
<details>
<summary>英文:</summary>
please use :textLabel->setToolTip
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论