Answer the question
In order to leave comments, you need to log in
How to pass parameter to parent class function in Qt C++?
Hello. There was a problem when passing a parameter to a parent class function using slot signals.
Used lambda function. Everything inside the class is great.
parent.h
class Rashod : public QWidget
{
Q_OBJECT
public:
explicit Rashod(QWidget *parent = nullptr);
private:
QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
QVBoxLayout *vblay = new QVBoxLayout();
QPushButton *add_btn = new QPushButton("Добавить");
public slots:
void show_add_btn(QString s);
};
void Rashod::show_add_btn(QString s)
{
add_btn->show();
QLabel *knt = new QLabel(s);
vblay->addWidget(knt, 0);
child *chuld = new chuld(this);
}
class add_widget : public QWidget
{
Q_OBJECT
public:
explicit add_widget(QWidget *parent);
private:
QPushButton *add = new QPushButton("Добавить");
QString FIO;
};
add_widget::add_widget(QWidget *parent) : QWidget(parent)
{
connect(this->add, &QPushButton::clicked, [=](){parent->show_add_btn(FIO);});
}
Answer the question
In order to leave comments, you need to log in
You have a few options:
1) Use the old join syntax and make show_add_btn a slot
2) Use qobject_cast in the lambda to cast the object to the right type
3) Use static_cast to cast in the lambda if you're sure it's the right object
4) Redesign the architecture so that castes are not needed at all. For example, so that the slot is connected to the signal by some factory or its analogue. I vote for this option.
QObject::connect(this->add, &QPushButton::clicked, parent, &Rashod::show_add_btn);
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question