Solved Set value from SpinBox to variable
-
@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.