1
votes

I have a Custom Container Widget (we’ll call it WidgetA) which consists of some number of widgets, as well as a container that is exposed to Qt Designer using a QDesignerContainerExtension. I then have another widget (WidgetB) which extends WidgetA, adding a number of widgets to the container in WidgetA. Both widgets have a .ui form file associated with them.

This configuration works well for my purposes with one exception. Qt’s connectSlotsByName function, which runs within the setupUI function of both classes, is causing the widgets of Widget A to be connected to their slots twice. My code looks something like this:

// WidgetA.h
namespace Ui {
    class WidgetA;
}

class WidgetA : public QWidget
{
    Q_OBJECT

public:
    explicit WidgetA( QWidget* parent = 0 ) :
        QWidget( parent ),
        ui( new Ui::WidgetA )
    {
        ui->setupUi( this ); // <-- Connects widgets in WidgetA.
    }

    // ...

private:
    Ui::WidgetA* ui;
}

// ...
// WidgetB.h
namespace Ui {
    class WidgetB;
}

class WidgetB : public WidgetA
{
    Q_OBJECT

public:
    explicit WidgetB( QWidget* parent = 0 ) :
        WidgetA( parent ), // <-- WidgetA’s constructor, calls setupUi resulting in WidgetA’s widgets being connected.
        ui( new Ui::WidgetB )
    {
        ui->setupUi( this ); // <-- Connects widgets in WidgetA and WidgetB, resulting in duplicate connection of WidgetA’s widgets.
    }

    // ...

private:
    ui::WidgetB* ui;
}

I’m aware of the Qt:: UniqueConnection flag, but because the ui’s header class is generated automatically I have no control over the nature of the connections. How can avoid this double connection?

1

1 Answers

0
votes

The answer is simple:

Do not use the connectSignalsByName feature, do not create methods like on_object_signal(), this is just asking for trouble in any software.

You can use Qt Designer drag'n drop signal connectors if the widget in question should be connected via it's UI, but in my personal experience dealing with huge codebases written in Qt is that you should not use that and instead use the connect() function manually as it's much less errorprone.