Solved 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?
-
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? -
Do you mean: how to use the variable
time
as member variable fromMainWindow
? -
@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; }
-
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 worksBut when I click ok it just permanently hibernate
Mayb u need all code?
-
-
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))
-
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?
-
@tosik
Hi,
Yes, instead of getting it via the Change Signal, you can just take it directlyhttp://doc.qt.io/qt-5/qspinbox.html#value-prop
as in
int myval= ui->thespinbox->value(); -
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
-
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)
-
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 yourQListWidget
.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()); }
-
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.