0

The idea was to connect QWidget with a variable so that when text changes on a widget it will be also changed in a variable.

And do this with just one line like this

WidgetMapper::connect(ui->lineEdit, SIGNAL(textChanged(QString)), someClass.var);

which would connect for example QLineEdit with a variable.

1) This would display var in a lineEdit

2) when lineEdit fires an textChanged(QString) signal - WidgetMapper would convert this QString to correct mapped type with stringstream and write it to var.

But i dont really know templates that well, and dont know if it is possible at all. I dont think it is possible to use one WidgetMapper for every type, so i also tried creating separate instances for each type (WidgetMapper<int> mapper;) which would still be betten then writing setters and onTextChangedSlots for each QLiteEdit but i could not figure out how to make it work as well (converter part still could not figure out the correct type).

WidgetMapper is using QSignalMapper to map signal to QWidget, and it worked fine, the part i have troubles with - is converting QString to template variable.

So is it possible? And if yes how could i do this? Or maybe there already a solution for this problem? (Somehow use QDataWidgetMapper with a class that contains variables maybe?)

Stals
  • 1,543
  • 4
  • 27
  • 52
  • Why don't you use `QLineEdit::value()` and convert the returned value when you need it ? – Dimitry Ernot Nov 28 '13 at 09:00
  • @RomhaKorev because there are a lot of them and changes should change values in variables right after it was edited. So i would like avoiding writing onTextChanged slot for every QLineEdit to convert its text() to variable – Stals Nov 28 '13 at 09:08

1 Answers1

2

First of, connecting the variable would do nothing else than calling some function if it were possigle.

Second try using QSignalMapper, this way you could use a single slot for all widgets, given you keep their pointers in an array with the index being the signal(int) emitted by the SignalMapper. This way your slot can just use MyWidgetArray[i]->text().

Sebastian Lange
  • 3,879
  • 1
  • 19
  • 38
  • but how do i then remember which Widget is for which variable? – Stals Nov 28 '13 at 10:00
  • take a look at QSignalMapper. You can emit int-signals for different widgets. Just create two arrays having the widgets and variables, then just use: ``MyVarArray[i] = MyWidgetArray.at[i]->text();`` where i is just the int emitted by QSignalMapper (this is my preferred way of managing lots of wigets of the same type in one slot) – Sebastian Lange Nov 28 '13 at 10:03