Important: Please read the Qt Code of Conduct -

[solved] QWidget * parameter for any widget

  • I have a class made like this:
    @class test : public QWidget{
    test(QWidget * parent=nullptr, Qt::WindowFlags flags=0);
    void addWid(QWidget *);

    QHBoxLayout * viewLayout;
    QWidget * viewportWidget;
    QScrollArea * viewportScroll;
    void resizeEvent(QResizeEvent * );

    The addWid() method is used to add more widgets to the viewLayout; it's just an external access method for viewLayout->addWidget() because viewLayout is private. Unfortunately it's not working as expected. When I call the method with a QWidget pointer it works just fine, but if I call it with a QPushButton * or any other widget pointer, the compiler complains that it cannot find a proper method to call for a QPushButton * parameter (or, whichever is the object type).

    In the QBoxLayout class documentation I see the addWidget() method is defined with a QWidget * parameter, just like I did:
    @void QBoxLayout::addWidget(QWidget * widget, int stretch = 0, Qt::Alignment alignment = 0)@

    So what am I doing wrong ?

  • In this code, nothing is wrong. The problem may be in the calling code, if that code does not have access to the knowledge that a QPushButton is a QWidget.

  • I figured it out. As you suspected Andre, the calling was wrong. I was using a function call as parameter, it should have returned a QWidget * but instead it returned a QWidget **, so it totally mismatched the addWid() method.

Log in to reply