0

Have plenty of .ui files with derived classes, but now I decided to derive QLabel in some places. These use the "buddy" feature.

Relevant part of the .ui file:

<widget class="ItemLabel" name="icon">
</widget>
<widget class="ItemLabel" name="item">
  <property name="buddy">
    <cstring>icon</cstring>
  </property>
</widget>

...and this definition at the bottom of the .ui file:

<customwidgets>
 <customwidget>
  <class>Itemlabel</class>
  <extends>QLabel</extends>
  <header>ui/widget/ItemLabel.h</header>
 </customwidget>
</customwidgets>

Generates this code:

icon = new ItemLabel(item_parent);
icon->setObjectName(QString::fromUtf8("icon"));

item = new ItemLabel(item_parent);
item->setObjectName(QString::fromUtf8("item"));
item->setBuddy("icon");

Where the last line is obviously incorrect as the argument should be a QWidget *. It should be (and also is, for non-derived QLabels) like this:

item->setBuddy(icon);

It seems that the "magic" understanding of what a buddy is, is lost when deriving. That is, that it should realize that the icon is to be treated as a variable name and not a string.

Is there a way to inform it about this magic again? (it does not matter if the buddy widget is derived or not)

Using, Qt 5.15.2. Haven't tried other versions, but I did find this Qt6 migration - UIC generated code fails to compile (connect slots) - updated unanswered question, which touches on this topic.

edit: Here's the complete ItemLabel.h:

#pragma once

#include <QLabel>

class QMouseEvent;

class ItemLabel : public QLabel
{
public:
    explicit ItemLabel(QWidget *parent=nullptr, Qt::WindowFlags f=Qt::WindowFlags());
    explicit ItemLabel(const QString &text, QWidget *parent=nullptr, Qt::WindowFlags f=Qt::WindowFlags());

private:
    void mouseMoveEvent(QMouseEvent *ev) override;
};
pythonator
  • 384
  • 2
  • 12

1 Answers1

0

I've done almost exactly this in Qt 5.15.3 on Linux, and it seems to work just fine.

More specifically, I have a class InfoText that derives from QLabel and a class InfoButton that derives from QPushButton. In my .ui file, I have:

        <widget class="InfoButton" name="infoButton_name"></widget>
...
        <widget class="InfoText" name="infoText_name">
         <property name="buddy">
          <cstring>infoButton_name</cstring>
         </property>
         <property name="text">
          <string>...</string>
         </property>
        </widget>
...
 <customwidgets>
  <customwidget>
   <class>InfoButton</class>
   <extends>QPushButton</extends>
   <header>widgets/InfoButton.h</header>
  </customwidget>
  <customwidget>
   <class>InfoText</class>
   <extends>QLabel</extends>
   <header>widgets/InfoText.h</header>
  </customwidget>
...
 </customwidgets>

In the generated ui_xxx.h file, I get:

...
#if QT_CONFIG(shortcut)
        infoText_name->setBuddy(infoButton_name);
...
#endif // QT_CONFIG(shortcut)
...

So it looks like what you're doing in the .ui file should work. Is there anything in the .h or the .cpp file for your ItemLabel class that might be confusing the Qt MOC?

Matt Young
  • 21
  • 3
  • Thanks for taking the time to look into this. I worked around the issue by resolving the buddy myself by setting a custom property. I've also since this was asked migrated to Qt6, but the issue is actually still there (if I go back to the standard buddy property). There's not much to be confused about that I can see... I've added the complete ItemLabel.h to the question. – pythonator Jun 23 '23 at 09:57