Set value from SpinBox to variable



  • Hello guys!

    I need to set the spinBox value to my variable, which is outside the function.

    So here i have variable time and wanna set it to spinBox value:

    void MainWindow::on_spinBox_valueChanged(int arg1)
    {
    
        arg1 = ui->spinBox->value();
    
    }
    

    I wanna store arg1 to time variable.

    Shall i define time as global variable?


  • Lifetime Qt Champion

    Hi and welcome to devnet,

    Do you mean a member variable ?

    By the way, the arg1 parameter already contains the latest value of the spin box.



  • @SGaist u r actually right.
    But I know only way with global. And its very bad way (u know)
    Can u give me an example, how to implement it with my situation?


  • Lifetime Qt Champion

    Do you mean: how to use the variable time as member variable from MainWindow ?



  • @SGaist Already done. ty

    Right?

    mainWindows.h
    
    public:
        //somecode here
        void setTimeout (const int _timeout) { timeout = _timeout; }
    
    mainWindow.cpp
    void MainWindow::on_spinBox_valueChanged(int arg1)
    {
        timeout = arg1;
    }
    

  • Lifetime Qt Champion

    Looks good

    Then please mark the thread as sovled using the "Topic Tools" button so that other forum users may know a solution has been found :)



  • @SGaist One more question, pls

    mycode:

    string command;
    string hiber = " /nobreak & shutdown /h";
    
    //code
    
    command = "timeout /t " + to_string(timeout) + hiber;
    
    if(ui->buttonBox->standardButton(button) == QDialogButtonBox::Ok) {
            system(command.c_str());
    }
    

    my command might be timeout /t 3600(my time) /nobreak & shutdown /h
    I test this string in cmd, it works

    But when I click ok it just permanently hibernate

    Mayb u need all code?

    Full code


  • Lifetime Qt Champion

    Did you check the value of timeout when you build the string ?

    By the way, Qt as QString that might help in building your string.

    It also has QProcess to call other applications or command line tools.



  • Ty for qstring and qproccess

    Think I found the problem. When I first choose time and after click on hibernate in combobox - works ok

    When I First click hibernate and then choose time - permanently hibernate

    Wtf))


  • Lifetime Qt Champion

    Because your timeout variable is likely not initialised with a sensible value.

    By the way, it looks like you don't even need that variable at all. You can query the value of your spin box when you build your command.



  • @SGaist U mean that I can directly transfer the value of spinbox to a variable when i build the command?


  • Qt Champions 2016

    @tosik
    Hi,
    Yes, instead of getting it via the Change Signal, you can just take it directly

    http://doc.qt.io/qt-5/qspinbox.html#value-prop

    as in
    int myval= ui->thespinbox->value();


  • Lifetime Qt Champion

    command = "timeout /t " + to_string(ui->spinBox->value()) + hiber;



  • @mrjj Nice) Thanx. But i steel need to fix the problem with initializing. Cause my command initialize when i click on Item in List. Like this

    else if(item->text()=="Hibernate"){
            command = "timeout /t " + to_string(timeout) + hiber;
    

    And If i click before i choose time from spinBox, and press ok button command will ignore spinbox value

    Any way to fix this?



  • @SGaist said in Set value from SpinBox to variable:

    ui->spinBox->value()

    Ty for helping. @mrjj already answer me))))

    P.S. Can u rep me, cause :

    As a new user, you can only post once every 300 second(s) until you have earned 1 reputation - please wait before posting again
    

  • Lifetime Qt Champion

    I saw that, but it looks like you are still using your member variable without setting it to a sensible value. However you really don't need that variable. Take again a look at my suggestion.



  • @SGaist Oh, sry mate. I paste old code. I already do that

    void MainWindow::on_buttonBox_clicked(QAbstractButton *button)
    {
        if(ui->buttonBox->standardButton(button) == QDialogButtonBox::Close) {
            close();
            }
        else if(ui->buttonBox->standardButton(button) == QDialogButtonBox::Ok) {
            system(command.c_str());
            }
        else {
            system("shutdown -a");
            }
    }
    
    void MainWindow::on_listWidget_itemClicked(QListWidgetItem *item)
    {
        if(item->text()=="Shutdown"){
                command = "shutdown /s /t " + to_string(ui->spinBox->value());
        }
        else if(item->text()=="Hibernate"){
            command = "timeout /t " + to_string(ui->spinBox->value()) + hiber;
        }
    }
    

    As u see my command build when i click something in list(as u say). So I think i need paste my command build into "Ok" button.

    In future i think i try to use Qproccess, cause now im so noob. And I dont think that i can do this)


  • Lifetime Qt Champion

    Your logic is a bit flawed.

    You should rather build the command when you are going to use it based on the state of your application. Currently command will be empty until you click on an item.

    You should rather build your command in on_buttonBox_clicked based on what you selected in your QListWidget.

    Don't wait to learn ;)



  • @SGaist Something like this?

     else if(ui->buttonBox->standardButton(button) == QDialogButtonBox::Ok) {
            if(on_listWidget_itemClicked(QListWidgetItem *item) == "Shutdown"){
                command = "shutdown /s /t " + to_string(ui->spinBox->value());
        }
            system(command.c_str());
            }
    

  • Lifetime Qt Champion

    No, not by calling the slot like that, by retrieving the currently selected item and if there's one then apply the logic you want to build the command.

    And you can also make command a locale variable.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.