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;
};
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
InfoTextthat derives fromQLabeland a classInfoButtonthat derives fromQPushButton. In my .ui file, I have:In the generated ui_xxx.h file, I get:
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
ItemLabelclass that might be confusing the Qt MOC?