P
P
Pavel Matveev2019-11-24 19:44:52
Qt
Pavel Matveev, 2019-11-24 19:44:52

How to pass parameters to parent object slot?

I reviewed many different examples, but did not understand how to pass parameters to the slot of the parent object.
The class of the object passing the parameters:

class MakeTable : public QDialog
{
public:
    MakeTable(QWidget* parent = nullptr);
private:
    QGridLayout* lay;

    qint16 width;
    qint16 height;

    QLabel* w;
    QLabel* h;

    QSpinBox* setWidth;
    QSpinBox* setHeight;

    QPushButton* ok;
    QPushButton* cancel;
public slots:
    void reject();
};

The constructor of this class:
MakeTable::MakeTable(QWidget* parent)
    : QDialog(parent, Qt::WindowTitleHint | Qt::WindowSystemMenuHint)
{
    w = new QLabel("Введите количество столбцов таблицы");
    h = new QLabel("Введите количество строк таблицы");

    setWidth = new QSpinBox;
    setHeight = new QSpinBox;

    setWidth->setMaximumWidth(50);
    setWidth->setMaximum(1024);
    setWidth->setMinimum(1);
    setWidth->setValue(32);

    setHeight->setMaximumWidth(50);
    setHeight->setMaximum(1024);
    setWidth->setMinimum(1);
    setHeight->setValue(32);

    w->setBuddy(setWidth);
    h->setBuddy(setHeight);

    ok = new QPushButton("ОК");
    cancel = new QPushButton("Отмена");

    width = qint16(setWidth->value());
    height = qint16(setHeight->value());

    connect(ok, SIGNAL(clicked()), parent, SLOT(makeTable(width, height))); //Здесь непонятный момент.
    connect(cancel, SIGNAL(clicked()), this, SLOT(reject()));

    lay = new QGridLayout;
    lay->addWidget(w,0,0);
    lay->addWidget(h,1,0);
    lay->addWidget(setWidth,0,1);
    lay->addWidget(setHeight,1,1);
    lay->addWidget(ok, 2,0);
    lay->addWidget(cancel, 2,1);
    setLayout(lay);
    this->setFixedSize(320,100);
    this->show();
    this->exec();
}

Parent widget slot receiving parameters:
void Widget::makeTable(qint16 width, qint16 height);

The parent widget is parent in the MakeTable constructor.
Parameters are values ​​from QSpinBox'ov MakeTable.
When executing the program, the following error is thrown to QtCreator's "Application output" console:
QObject::connect: No such slot Widget::makeTable(width, height)
QObject::connect: (receiver name: 'Widget')

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander Ananiev, 2019-11-24
@SaNNy32

In the connect function, the slot must contain a function description with the type of parameters, for example,
True, this will not solve the problem with passing parameters to the slot, because the signal does not contain parameters. To do this, use local class member variables.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question