0

Is it possible to get a list of values from each spinbox and put them into a list?

for (int i = 0; i < norm_size+1; i++){
    getSpinleftValue(i);
}

I use a for loop to set up all my connections

.
void GuiTest::getSpinleftValue(int index){
    QSpinBox* spinleft[norm_size+1] = {ui->norm_spinBox_9,
                                       ui->norm_spinBox_10,
                                       ui->norm_spinBox_11,
                                       ui->norm_spinBox_12,
                                       ui->norm_spinBox_13,
                                       ui->norm_spinBox_14,
                                       ui->norm_spinBox_15,
                                       ui->norm_spinBox_16};
    QObject::connect(spinleft[index], SIGNAL(valueChanged(int)), this, SLOT(spinboxWrite(int, index)));
}
.

then once the connections are made by the for loop, I wanted to write their outputs into a list to be used later.

.
void GuiTest::spinboxWrite(int arg1, int index){
    int norm_list[16];
    qDebug() << arg1;
    qDebug() << index;
}

in this case I'm using some debug functions so I can see if they're working. Right now it doesn't look like it's working, because I'm not writing the "connect" part right.

No such slot GuiTest::spinboxWrite(int, index) in

I know the other solution is to create a bunch of these

void GuiTest::on_norm_spinBox_9_valueChanged(int arg1)
{
    //code here
}

But I'd rather not pollute my entire file with 16 of these if I can help it!

tisaconundrum
  • 2,156
  • 2
  • 22
  • 37
  • You can use the same function for multiple spinboxes. That should get you down to 2 functions. – nwp Jul 05 '16 at 22:15
  • You didn't show how you connect the signals to the slots, but it should be something like `connect(spinbox1, valueChanged, guitest, on_norm_spinBox_valueChanged);`. – nwp Jul 05 '16 at 22:21
  • I use the gui designer that comes with QtCreator. It does that for me. – tisaconundrum Jul 05 '16 at 22:28
  • In the gui designer you should be able to open the .ui file, click on the "Edit Signals/Slots" and drag an arrow from a spinbox to the window, then select `valueChanged(int)` on the left and some function on the right. You can choose the same function on the right for multiple spinboxes. – nwp Jul 05 '16 at 22:34
  • I just tried what you suggested. But i'm not sure what _"to the window"_ is referring to. – tisaconundrum Jul 05 '16 at 22:44
  • To the window where the spinbox is located in. You will then need to add slots for that window. How did you get the spinboxes connected to your functions? – nwp Jul 05 '16 at 22:47
  • Click on **Edit Widgets** (or press F3) then right click on a **spin box**, then click on **go to slot** a window titled _"Go to Slot"_ will appear, I then clicked on **valueChanged(int)**. Then the gui created the function for me. – tisaconundrum Jul 05 '16 at 22:50
  • This will create a slot for you, which is good. You can then in the "Edit Signals/Slots" mode drag an arrow from another spinbox to the window behind it, which opens a "Configure Connection" window where on the right side click "Edit...", press the green + for Slots, replace `Slot()` with `on_spinBox_valueChanged(int)` and press Ok. You can now connect the `valueChanged(int)` signal on the left with the `on_spinBox_valueChanged(int)` slot on the right and do the same for any number of spinboxes. – nwp Jul 05 '16 at 22:57
  • more importantly will it create this output? **[0,0], [1,1], [2,2], [3,3], [4,4], [5,5], [6,6], [7,7]** – tisaconundrum Jul 05 '16 at 23:05
  • [link]http://i.imgur.com/8SrV8S6.png I think i did it right. Here's what I'm looking at – tisaconundrum Jul 05 '16 at 23:08
  • @nwp, i'm blocked from chat, thank you for the help. – tisaconundrum Jul 05 '16 at 23:40

1 Answers1

1

The signal valueChanged(int) and your slot spinboxWrite(int, index) (note that index is not even a type in your case!) do not have matching signatures, so connect won't work. From the docs:

the signature passed to the SIGNAL() macro must not have fewer arguments than the signature passed to the SLOT() macro.

I think that the simplest way to solve your problems would be connecting the valueChanged(int) signal from all the spin boxes to the same slot and use sender to get the spin box that was changed, Here is how you would do that, in the constructor:

GuiTest::GuiTest(QWidget* parent)/*do you initializations*/{

    //after setup ui, create your spin boxes. . .

    //get list of all children spin boxes
    //(you can replace that with a hard coded list if it is 
    //not applicable)
    QList<QSpinBox*> spinBoxes= findChildren<QSpinBox*>();
    //connect the signal from all spin boxes to the slot
    QSpinBox* spinBox;
    foreach(spinBox, spinBoxes)
        connect(spinBox, SIGNAL(valueChanged(int)), this, SLOT(SpinBoxChanged(int)));
}

and here is how your spinboxWrite slot would look like:

void GuiTest::SpinBoxChanged(int value){
    QSpinBox* sp= qobject_cast<QSpinBox*>(sender());

    //now sp is a pointer to the QSpinBox that emitted the valueChanged signal
    //and value is its value after the change
    //do whatever you want to do with them here. . .

}
Mike
  • 8,055
  • 1
  • 30
  • 44
  • you seem to have edited your question before I posted my answer, if this answer is not applicable to your question I will delete it – Mike Jul 05 '16 at 23:13
  • Thank you, Mike. Let me try it out. I read through your comments, it might work. – tisaconundrum Jul 05 '16 at 23:14
  • Thanks, Mike. I will mark you as the solution. This is really clever, and after running it in the debugger it seems to be what i think i need. – tisaconundrum Jul 05 '16 at 23:36
  • should I revert the changes on my question? So your answer is relevant? – tisaconundrum Jul 05 '16 at 23:41
  • @tisaconundrum , I think your question after editing is less accurate and understandable than before editing, so I think that you should revert it – Mike Jul 05 '16 at 23:48
  • Thanks again Mike, this helped a lot – tisaconundrum Jul 05 '16 at 23:55
  • 1
    @tisaconundrum , apologizes for editing the answer. There is no need to use `QSignalMapper` here, this is a simpler answer. I wasn't alert enough, sorry. – Mike Jul 09 '16 at 02:37