Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Connect signals and slots of a class from another class



  • Hello,
    I am getting a compiler centered around this line:

    connect(applyBtn, &QPushButton::clicked, propPgs->widget(i), &ParamPropPg::pushChanges);
    

    /no matching function for call to ‘ParamTab::connect(QPushButton*&, void (QAbstractButton::)(bool), QWidget, void (ParamPropPg::)(bool))’
    253 | connect(applyBtn, &QPushButton::clicked, propPgs->widget(i), &ParamPropPg::pushChanges);
    | ^
    conversion from ‘QWidget
    ’ to ‘const Object*’ {aka ‘const ParamPropPg*’} [-fpermissive]
    253 | connect(applyBtn, &QPushButton::clicked, propPgs->widget(i), &ParamPropPg::pushChanges);
    | ~~~~~~~~~~~~~^
    | |
    | QWidget*

    I tried various things and this line reduced it to one error:

    connect(applyBtn, &QPushButton::clicked, propPgs->widget(i), static_cast<void (ParamPropPg::*) (qreal)>(&ParamPropPg::pushChanges));
    

    invalid static_cast from type ‘void (ParamPropPg::)()’ to type ‘void (ParamPropPg::)(qreal)’ {aka ‘void (ParamPropPg::)(double)’}
    252 | connect(applyBtn, &QPushButton::clicked, propPgs->widget(i), static_cast<void (ParamPropPg::
    ) (qreal)>(&ParamPropPg::pushChanges));
    | ^

    All classes involved have Q_OBJECT macro in the header and are QWidgets or derived from some type of QWidget.

    I am creating the ui items and adding them to the ParamPropPg class from a different class, which is working. I am trying to connect the button that was created to the public slot of the ParamPropPg class. The ParamPropPg class is included (and is even in the same header file as the class calling the connect).

    Basically, I am trying to connect a class' signals and slots from another class. Is this possible or do I need I need to create some function in the ParamPropPg class that is called when I want to connect one of it's own objects/children signals to its own slot?



  • @thewiggin
    Hi,
    Is applyBtn a pointer to QPushButton?
    Does propPgs->widget(i) return a pointer to ParamPropPg?



  • @CP71 Yes to both, sorry that’s not clear. I will try to untangle my code to get provide a minimum reproducible example



  • @thewiggin
    does propPgs->widget(i) return a const pointer?
    Is yes, try to remove const


  • Lifetime Qt Champion

    Hi
    it kinda seems that you try to hook the clicked() signal up to a slot that expects a qreal ?

    • Basically, I am trying to connect a class' signals and slots from another class. Is this possible
      Yes its kinda the very foundation on why signal and slots are cool. :)


  • @mrjj

    Man, just deleted my text as I've seen your comment :)
    I need too much time to finish my replies :)

    @thewiggin If you slot really needs a qreal, try to connect using lambda connections.



  • Here's the example with the relative information included. the EnglishPage only has one layout and if the connect line is commented out the code compiles and does add the button to each page in the stack.

    gui.h.......

    #include <QDialog>
     
    QT_BEGIN_NAMESPACE
     
    QT_END_NAMESPACE
     
    class gui : public QDialog
    {
        Q_OBJECT
     
    public:
        gui(QWidget *parent = nullptr);
        ~gui();
     
    };
    

    gui.c........

    #include "gui.h"
    #include <QtWidgets>
    #include "mylistwidget.h"
     
    gui::gui(QWidget *parent)
        : QDialog(parent)
    {
        setFixedSize(800,600);
        setWindowTitle(tr("Gui Tests"));
     
        QVBoxLayout *mainLayout = new QVBoxLayout;
        QTabWidget *tabWidget = new QTabWidget;
        MyListWidget *myListWidget = new MyListWidget;
        QScrollArea *myListWidgetScrollArea = new QScrollArea;
        myListWidgetScrollArea->setWidget(myListWidget);
        tabWidget->addTab(myListWidgetScrollArea, "List Widget");
        mainLayout->addWidget(tabWidget);
     
        for(int i = 0; i < myListWidget->pageStack->count(); ++i)
        {
            QVBoxLayout *pageLay = myListWidget->pageStack->widget(i)->findChild<QVBoxLayout*>();
            if(pageLay)
            {
                QPushButton *applyBtn = new QPushButton("push");
                pageLay->addWidget(applyBtn, 0, Qt::AlignCenter);
                connect(applyBtn, &QPushButton::clicked, myListWidget->pageStack->widget(i), &EnglishPage::btnClicked);
            }
        }
    

    Mylistwidget.h…..

    #include <QListWidget>
    #include <QStackedWidget>

    class MyListWidget : public QWidget
    {
    Q_OBJECT

    public:
    MyListWidget();
    QStackedWidget *pageStack;
    QListWidget *listWidget;
    }

    setLayout(mainLayout);
    setWindowTitle(QGuiApplication::applicationDisplayName());
    }

    class EnglishPage : public QWidget
    {
    Q_OBJECT
    public:
    EnglishPage(QWidget *parent = 0);

    public slots:
    void btnClicked(void);
    };

    @CP71 I widget does return const. I'm not too familiar with C++. The hover bubble over the function reads QWidget *QStackedWidget::(int index) const. I guess const on the end makes the return const? How do I "remove" the const?



  • @thewiggin said in Connect signals and slots of a class from another class:

    void btnClicked(void);

    What happens if you remove the void parameter, since clicked signal passes a boolean by default to the slot?
    (Just leave it empty)



  • @thewiggin
    const at the end: "QWidget *QStackedWidget::(int index) const" means function body of class can not change its member varables ( variables are defined in .h )



  • @thewiggin
    I don't see anything strange! or it seems so.
    I know, what I say now could be strange, but try to remove the build folder via file explorer.
    I don't know why, sometimes when I see something that is strange and it seems inexplicable this operation removes the issue.
    I don't say this is the case, but you don't have anything to lose ;)



  • @Pl45m4
    I have tried to do this.
    I have made connect with and without void but I haven't seen the issue



  • @CP71 no luck when removing build folder and retrying

    @Pl45m4 changing the slot from void to nothing did not resolve this



  • Here's my workaround, it seems needlessly complex and has me begging the question of if its possible to connect to the slot of another class outside that class. All the declarations I can find use "this" as the third augment.

    gui.c I added this at the bottom of the for loop, right after adding the button the the layout.

    EnglishPage *pg = dynamic_cast<EnglishPage*>(myListWidget->pageStack->widget(i));
                if(pg)
                {
                     pg->setBtn(applyBtn);
                }
                else
                {
                    qDebug() << "failed to connect apply btn for english page";
               }
    

    I changed the public slot in EnglishPage to a private slot and added a public function called setBtn.

    void EnglishPage::setBtn(QPushButton *btn)
    {
        connect(btn, &QPushButton::clicked, this, &EnglishPage::btnClicked);
    }
    


  • Well shockingly it was needlessly complex.

    The below line works in context with the other code posted. I will aggregate it together in this post eventually for a clean solution unless someone provides a better one. I had to change the slot back to public from private.

    connect(applyBtn, &QPushButton::clicked, dynamic_cast<EnglishPage*>(myListWidget->pageStack->widget(i)), &EnglishPage::btnclicked);
    


  • @thewiggin
    Sorry, my mistake :(
    There is something strange!
    myListWidget-> pageStack-> widget (i) returns QWidget * and not EnglishPage *, for this it didn't found btnClicked.
    Sometimes the solution is under our eyes but it hides under them:;



  • @thewiggin said in Connect signals and slots of a class from another class:

    Here's my workaround, it seems needlessly complex and has me begging the question of if its possible to connect to the slot of another class outside that class. All the declarations I can find use "this" as the third augment.

    Yes of course!
    you see "this" because normally the sender or/and the receiver is the class itself, but you can connect one or two classes that arent the classe itself!
    But this you already see in your last post!


Log in to reply