0

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:

enter image description here

can someone please help.

3 Answers3

1

You cant directly set max and min on tooltip , hence you should indrectly do that(for this usecase):

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: Qt WIdget inside ToolTip

H.M
  • 425
  • 2
  • 16
0
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() function to catch QEvent::ToolTip (and probably QEvent::ToolTipChange) and draw it yourself using QToolTip::showText()

perivesta
  • 3,417
  • 1
  • 10
  • 25
-1

please use :textLabel->setToolTip