QSignalMapper problems with strings - c++

I have a question about QSignalMapper.
I have simple application, a calculator. And I have something like this, I click button and I want to display it. But I have problem, I don't know how to assign string to a button. It only want to work with integers. But i know it is possible to do it with strings. And I need to do it on strings, because then I want to convert it to double type. I have idea how do rest of things I want to do, but this QSignalMapper is killing me.
QSignalMapper *signalMapper = new QSignalMapper(this);
connect(ui->Button0, SIGNAL(clicked()),
signalMapper, SLOT(map()));
signalMapper->setMapping(ui->Button0, '0');
I tried to do something with QString but it did not help.
I will be grateful for any help.

I don't think you need signalMapper to accomplish what you are looking for. A standard connect() function call will likely work.
Try using Qt5 syntax for creating the connection
Change your connect function call to include units if you are using the old way.
Edited - Example syntax for connect
New way. Note that param datatypes are not included in the connect
call.
connect(
sender, &Sender::valueChanged,
receiver, &Receiver::updateValue
);
Old way
connect(
sender, SIGNAL( valueChanged( QString, QString ) ),
receiver, SLOT( updateValue( QString ) )
);
Edited - Method 1 --> Using two known C++ objs
Create a signal function emitting a QString
Create a slot function recieving a QString
Connect the two QStrings together
Handle as desired
Edited - Method 2 --> Using QML exclusively
Declare signal(string str) in QML
Use existing btn onClicked event and call declared signal
Handle to signal via slot and do as needed with text
Edited - Method 3 --> Use QSignalMapper if dealing with MVC and indexs
1. See Qt documentation
Use lambda method to connect button click and text
connect(button, &QPushButton::clicked, [this, text] { clicked(text);
});
Hopefully one of these methods provides some help to solving your problem!

Related

Connecting function to dynamically made QPushButton

Sorry, this question is probably duplicate but i have to ask since i can't make it work.
I am dynamically making QPushButtons with for loop like this.
for (int var = 0; var < size; ++var) {
QPushButton *copyr = new QPushButton("copy");
...
}
I am successfully adding those new widgets to layout but i cant bind an function on them.I am trying to connect that button on SLOT like as many sources on the internet had suggested:
connect (copyr , SIGNAL( clicked() ), this, SLOT( c2c(txt) ) );
but i can't make it work, and i am kp getting an error like :
No such SLOT ClassName::c2c(txt)
event it's normally defined in .h file and it exists in .cpp file also.
Any ides why is this happening and how too fix it?
First of all, use the new syntax for connections:
connect(copyr, &QPushButton::clicked, this, &ClassName::c2c);
However, you cannot connect a signal that does not provide a parameter to a slot that expects one. Here, you have multiple options:
Remove the parameter from the slot entirely and get the value from within the slot.
Provide a default parameter in the slot declaration so it can be called without arguments
Wrap the connection in a lambda if you want a parameter from the connecting code to arrive at the slot like this:
connect( copyr, &QPushButton::clicked, [this, txt](){ this->c2c(txt); } );

Qt: Connecting multiple input widgets with valueChanged(int) signals to single slot?

With Qt 4.8 I create a number of input widgets (QSpinBox, QSlider) programmatically.
In the end, I would like to have a single method to handle changes of any of these input widgets, ideally by index.
However, these widgets only have a Signal with parameter, e.g. valueChanged(int).
This is not compatible with QSignalMapper()'s Slot map().
As it was pointed out in the comments, the connection does work!
connect( spinbox, SIGNAL( valueChanged(int) ),
signalMapper, SLOT( map() )
);
Now I just need to get the value, but this cannot be done via the sender() method anymore, because this now is the SignalMapper.
Original question:
Is there another way besides (re)implementing QSignalMapper with additional parameters or a parameter-less valueChanged() for the widget or using objectName and QObject::sender() in order for the Slot to see which element changed (and get the new value)?
You can use QAbstractSpinBox::editingFinished() and QAbstractSlider::sliderReleased() as your signals, they are parameterless.
Unfortunately there is no parameterless version of QAbstractSlider::valueChanged() so if you want a signal emitted continuously as the slider moves, you may need to subclass QSlider and create it. E.g.
class MySlider : public QSlider
{
...
private slots:
void HandleValueChanged(int) { emit valueChanged(); }
signals:
void valueChanged();
};
MySlider::MySlider(...)
{
connect(this, SIGNAL(valueChanged(int)), this, SLOT(HandleValueChanged(int)));
}
Though I admit, this may not be the most elegant solution.
If I understand your question and what you have until now correctly, you are missing two parts.
mapper->setMapping(<spinbox>, <id or name or pointer to the spinbox);
connect(mapper, SIGNAL(mapped(<datatype you used in setMapping()>),
this, SLOT(HandleValueChanged(<datatype you used in setMapping()>)));
So the HandleValueChanged() slot will receive an identifier of your sender, then you can directly access the value of the sender with the appropriate getter.
The method setMapping() takes either and integer, QString or a pointer to the widget itself as second argument. This is then forwarded via the mapped() signal of the mapper, so you can later identify which widget emitted the signal.

How do I get a signal to call a function with certain arguments?

I want to get a signal to call a function with certain arguments, like the example below.
QPushButton *yes = new QPushButton("Yes");
yes->connect(yes, SIGNAL(clicked()), NULL, SLOT(printf("You pressed a button")));
How do I accomplish this?
An often overlooked way to reverse signal/slot relationships is QObject::sender. You can call it in the receiving slot to get a handle on the QPushButton (using qobject_cast) and get the text from there. Alternatively you can use QSignalMapper to augment signals.
It seems very inefficient but you could create a new signal with a QString argument, which you connect to your pushbutton. The text contained will be defined on your emit call.
eg.
connect(yes, SIGNAL(clicked()), this, SLOT(emitHelloWorldText());
connect(this, SIGNAL(emitText(QString)), receiver, SLOT(dostuffWithText(QString)));
then your emitHelloWorldText method can be something like
void emitHelloWorldText() {
emit emitText("Hello world");
}
Then this can be picked up by your receiver class
void doStuffWithText(const QString &text) {
Unfortunately, the slot and the signal must have matching arguments. If you really need to stick with this interface, you could create an intermediary slot to propagate the received signal, but there’s no real way around.

Qt: connecting signal to slot having more arguments

I want to connect a signal clicked() from the button to a slot of different object.
Currently I connect signal to helper method and call desired slot from there:
connect(button, SIGNAL(clicked()), this, SLOT(buttonClicked()));
void buttonClicked() { // Helper method. I'd like to avoid it.
someObject.desiredSlot(localFunc1(), localFunc2());
}
But maybe there is a more simple and obvious way to do this?
is this what you want to do:
the signal clicked should be connected to the "desiredSlot" which takes two arguments that are returned by localFunc1 & 2 ??
this is not possible, as you can read in the QT docs. A slot can take less arguments than provided by the signal - but not the opposite way! (The documentation says "This connection will report a runtime error")
This ought to work with the new signal/slot mechanism in qt 5:
connect( button, &QPushButton::clicked, [&](){ someObject.desiredSlot( localFunc1(), localFunc2() ); } );
You will need to adjust the lambda capture to your needs.
In some cases, default arguments may help, e.g. declare desiredSlot as:
desiredSlot(int a=0, int b=0)
You cannot access members in default argument though.
That is not the way to connect signals and slots in QT. You should use:
connect(button, SIGNAL(clicked()), receiver, SLOT(slotToBeCalled());
Have a look at the QT documentation.

why append Slot doesn't work?

I have got a problem when I try to make following simple connections
QSpinBox *spinBox = new QSpinBox;
QSlider *slider = new QSlider(Qt::Horizontal);
QTextEdit *text = new QTextEdit("Hello QT!");
QObject::connect(spinBox, SIGNAL(valueChanged(int)),slider, SLOT(setValue(int)));
QObject::connect(slider, SIGNAL(valueChanged(int)),spinBox, SLOT(setValue(int)));
QObject::connect(slider,SIGNAL(valueChanged(int)),text, SLOT(append("slider changed!")));
QObject::connect(spinBox,SIGNAL(valueChanged(int)),text, SLOT(append("spinbox changed!")));
QObject::connect(text,SIGNAL(textChanged()),spinBox,SLOT(clear()));
It can be successfully compiled and excuted.But the two append slots seem not work.I've checked the help manual about QTextEdit and there's a public slot append there.Have I missed something?Help would be appreciated!
Unfortunately, you cannot pass custom values to your slots via QObject::connect (only type information for the arguments is allowed/interpreted correctly). Instead, create your own slot, something like
void MyWidget::mySliderChangedSlot(int newValue)
{
text->append("slider changed!");
}
and use
QObject::connect(slider, SIGNAL(valueChanged(int)), pMyWidget, SLOT(mySliderChangedSlot(int)));
to achieve your desired behaviour.
I hope that helps.
What exactly are you trying to do? That has now way of working because you connect a signal which has an int param to a slot with a string parameter for one, the other thing is that the signal slots where not meant for this kind of usage you just say wich function are conected and they pass parameters betwen them you dont pass the values yourself, you are not using them correctly read the documentation at http://doc.trolltech.com/4.6/signalsandslots.html for correct usage examples.