Nicholas Corin
Nicholas Corin

Reputation: 2404

Can't get QSignalMapper to work

I'm making an application using the C++ Qt Framework. The problem I have at the moment is similar to submitting a form. I need to add a Client to the system. So when you click the "Add Client" button, it needs to submit the text from a QLineEdit and a QDate from a QDateEdit to a function.

The more I have researched, the more it seems I have to use a QSingalMapper, but I cannot seem to get it to work at all.

Here's a snippet of the code I tried to use first. I have a Client data structure with the Name and Joining Date that needs to be submitted. I can, however, also create the Client object and pass that through as a parameter insead if it's a better idea.

 QObject::connect(addClientBtn, SIGNAL(clicked()), this, SLOT(addClient(clientName->text(), joiningDate->date())));

When I tried to use QSignalMapper, it kept telling me that I can't send a Client object because its not of type QString, Widget*, etc.

Is there maybe an easier way to do this that I've overlooked? Any help would be greatly appreciated.

Upvotes: 0

Views: 173

Answers (1)

Bowdzone
Bowdzone

Reputation: 3854

You do not need a QSignalMapper if I understand you correctly but its difficult to tell as you hardly posted any code. Expecially it is difficult because we have no idea what this is. But assuming it is a QDialog or QMainWindow, you have to do something along the following:

in the class definition .h

...
protected slots:
    void add_client();
...

in the class implementation .cpp

mydialogormainwindow::mydialogormainwindow(){

    QObject::connect(addClientBtn, SIGNAL(clicked()), this, SLOT(addClient()));
}

void mydialogormainwindow::add_client(){

    QString name = clientName->text();
    QDate date = joiningDate->date();
    ....
}

This is due to the signal-slot connection. The Signal sends out a signal including parameters which are sent to the slot. The clicked signal has no parameters so it can't send anything along to the slot. But with the slot being defined in the same class, you can access the data there directly.

Note: This works only if your class is a derived QObject (which is the case for QDialog and QMainWindow) and has the Q_OBJECT macro in its class definition.

Upvotes: 2

Related Questions