Unsolved Connect signals and slots of a class from another class
-
@thewiggin
does propPgs->widget(i) return a const pointer?
Is yes, try to remove const -
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. :)
- Basically, I am trying to connect a class' signals and slots from another class. Is this possible
-
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_OBJECTpublic:
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 -
-
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!