0

I am quite new to C++ and Qt. I've gotten pretty far on my current project, but I've been putting off this one part. I have a pushbutton that opens a new dialog like this:

void MainWindow::on_fillAll_clicked()
{
    int yo;
    BlockSelect bSelect;
    bSelect.setModal(true);
    bSelect.exec();

    if( bSelect.exec() == QDialog::Accepted )
    {
        //Get stuff here?
        //I want to fill yo with the spinbox value
        yo = bSelect.stuff();
        return;
    }

    qDebug() << yo;
}

This works fine. In the dialog I have a spin box. I want to send that value inputted to the spin box to my main window when the user clicks OK.

I have been trying to get "int yo;" to have that value from the spinbox but everything I try just gets an error.

I added this to my BlockSelect public class:

int stuff();

And I made this function in my blockselect.cpp:

int BlockSelect::stuff()
{
    qDebug() << "The function was called";
    return ui->yolo->value();
}

But qDebug never shows anything???

So how can I fill yo from the main window with yolo from the dialog?

Sorry if I didn't explain this well :( I'm still learning. Thanks for your time :)

mrg95
  • 2,371
  • 11
  • 46
  • 89

1 Answers1

1

First of all, there is no need to call exec() twice, just use it once within the if statement.

To answer your question, you still have the bSelect dialog object (and I'm assuming BlockSelect is a class you define?), so make an accessor function inside it to retrieve the values you want.

if( bSelect.exec() == QDialog::Accepted )
{
    //Get stuff here?
    //I want to fill yo with the spinbox value
    yo = bSelect.stuff();
    return;
}

EDIT:

Your BlockSelect class needs to contain an accessor function, this means a function that returns a value.

int stuff() { return ui->yolo->value();}

What I'm doing here is retrieving the spinbox's value (assuming it is named 'yolo') and returning it as a result of calling the 'stuff' function.

Lochemage
  • 3,974
  • 11
  • 11
  • Oh! Thanks for telling me about the exec thing. :) I created this in my blockselect.cpp (the dialog cpp) void BlockSelect::stuff() { int yolo(5); } – mrg95 Jul 16 '13 at 23:44
  • I tried to put yo = bSelect.stuff(yolo) in my if statement but it didnt work – mrg95 Jul 16 '13 at 23:44
  • I added what I did to my BlockSelect class and the blockselect.cpp to my original post. I just don't understand what to put in my if statement to get the spinbox value. – mrg95 Jul 16 '13 at 23:56
  • according to your definition, bSelect.stuff() returns void and takes no parameters, so how is 'yo = bSelect.stuff(yolo);' supposed to work? – Lochemage Jul 16 '13 at 23:56
  • It's not calling the stuff function. I changed my op to show the changes I've made based off your answer – mrg95 Jul 17 '13 at 00:24
  • The spinbox is located in the dialog? Then you will need to set a property that holds the value, since the ui-object is not guaranteed to still hold the value. Connect a signal to a local slot to save your spinbox value. – Sebastian Lange Jul 17 '13 at 06:42
  • `int stuff() const` would be better – Frank Osterfeld Jul 17 '13 at 07:37