1

I have a QDialogButtonBox:

QDialogButtonBox buttonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel, Qt::Horizontal, &dialog);

And when user presses the Ok button I want to call a slot, which has 2 QString arguments. I was trying to use signals, but I cannot make it work.

I have tried to do something like this:

 connect(buttonBox, &QDialogButtonBox::accepted, this, 
        App::replace);

App::replace is the slot I want to call, but I do not know how to pass the arguments.

How can I achieve that?

scopchanov
  • 7,966
  • 10
  • 40
  • 68
msk
  • 141
  • 1
  • 3
  • 11

2 Answers2

1

You can create a slot and in that slot call your requested function like this.

...
connect(buttonBox, SIGNAL(accepted()), this, SLOT(accept()));
connect(buttonBox, SIGNAL(rejected()), this, SLOT(reject()));
...

//slot implementation
void CLASS::accept()
{
    foo(QString1, QString2);
}

Something similar to this. In your particular case it will be like following

...
connect(buttonBox, SIGNAL(accepted()), this, SLOT(accept()));
connect(buttonBox, SIGNAL(rejected()), this, SLOT(reject()));
...

//slot implementation
void App::accept()
{
    replace(QString1, QString2);
}

The reason is that slot can accept less or equal arguments that signal provides. In this case signal accepted doesn't provide any arguments so you can't get any arguments. For that you have to manually collect and pass them in your slot.

arsdever
  • 1,111
  • 1
  • 11
  • 32
1

As an alternative to @Arsen's answer, you can connect a lambda that provides the requisite arguments

connect(buttonBox, &QDialogButtonBox::accepted, this, [this](){ replace(value1, value2); });
Caleth
  • 52,200
  • 2
  • 44
  • 75