Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Set value from SpinBox to variable
Forum Updated to NodeBB v4.3 + New Features

Set value from SpinBox to variable

Scheduled Pinned Locked Moved Solved General and Desktop
20 Posts 3 Posters 7.5k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    tosik
    wrote on last edited by
    #1

    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?

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      T 1 Reply Last reply
      0
      • SGaistS SGaist

        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.

        T Offline
        T Offline
        tosik
        wrote on last edited by tosik
        #3

        @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?

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

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

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          T 1 Reply Last reply
          0
          • SGaistS SGaist

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

            T Offline
            T Offline
            tosik
            wrote on last edited by
            #5

            @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;
            }
            
            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              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 :)

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              T 1 Reply Last reply
              0
              • SGaistS SGaist

                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 :)

                T Offline
                T Offline
                tosik
                wrote on last edited by tosik
                #7

                @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

                1 Reply Last reply
                0
                • SGaistS Offline
                  SGaistS Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  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.

                  Interested in AI ? www.idiap.ch
                  Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                  1 Reply Last reply
                  0
                  • T Offline
                    T Offline
                    tosik
                    wrote on last edited by tosik
                    #9

                    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))

                    1 Reply Last reply
                    0
                    • SGaistS Offline
                      SGaistS Offline
                      SGaist
                      Lifetime Qt Champion
                      wrote on last edited by
                      #10

                      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.

                      Interested in AI ? www.idiap.ch
                      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                      T 1 Reply Last reply
                      0
                      • SGaistS SGaist

                        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.

                        T Offline
                        T Offline
                        tosik
                        wrote on last edited by
                        #11

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

                        mrjjM 1 Reply Last reply
                        0
                        • T tosik

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

                          mrjjM Offline
                          mrjjM Offline
                          mrjj
                          Lifetime Qt Champion
                          wrote on last edited by mrjj
                          #12

                          @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();

                          T 1 Reply Last reply
                          2
                          • SGaistS Offline
                            SGaistS Offline
                            SGaist
                            Lifetime Qt Champion
                            wrote on last edited by
                            #13

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

                            Interested in AI ? www.idiap.ch
                            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                            T 1 Reply Last reply
                            2
                            • mrjjM mrjj

                              @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();

                              T Offline
                              T Offline
                              tosik
                              wrote on last edited by
                              #14

                              @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?

                              1 Reply Last reply
                              0
                              • SGaistS SGaist

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

                                T Offline
                                T Offline
                                tosik
                                wrote on last edited by
                                #15

                                @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
                                
                                1 Reply Last reply
                                0
                                • SGaistS Offline
                                  SGaistS Offline
                                  SGaist
                                  Lifetime Qt Champion
                                  wrote on last edited by
                                  #16

                                  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.

                                  Interested in AI ? www.idiap.ch
                                  Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                                  T 1 Reply Last reply
                                  0
                                  • SGaistS SGaist

                                    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.

                                    T Offline
                                    T Offline
                                    tosik
                                    wrote on last edited by
                                    #17

                                    @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)

                                    1 Reply Last reply
                                    0
                                    • SGaistS Offline
                                      SGaistS Offline
                                      SGaist
                                      Lifetime Qt Champion
                                      wrote on last edited by
                                      #18

                                      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 ;)

                                      Interested in AI ? www.idiap.ch
                                      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                                      T 1 Reply Last reply
                                      0
                                      • SGaistS SGaist

                                        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 ;)

                                        T Offline
                                        T Offline
                                        tosik
                                        wrote on last edited by
                                        #19

                                        @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());
                                                }
                                        
                                        1 Reply Last reply
                                        0
                                        • SGaistS Offline
                                          SGaistS Offline
                                          SGaist
                                          Lifetime Qt Champion
                                          wrote on last edited by
                                          #20

                                          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.

                                          Interested in AI ? www.idiap.ch
                                          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                                          1 Reply Last reply
                                          0

                                          • Login

                                          • Login or register to search.
                                          • First post
                                            Last post
                                          0
                                          • Categories
                                          • Recent
                                          • Tags
                                          • Popular
                                          • Users
                                          • Groups
                                          • Search
                                          • Get Qt Extensions
                                          • Unsolved