Passing user Input to Slots
-
@JonB after the User clicks the Button a Dialog Window pops up where the User can insert an IP Adress for example.
void boxconec::Target_IP() { QString TargetIP = QInputDialog::getText(this, "Choose Target system", "Enter the IP Address of the Target System:"); ui->IPAdress->setText(TargetIP); }
So I want to pass this TargetIP Variable to a Function that connects to a QTcp Server listening under the given Address. How can I pass that information to my Client Function?
@JohnSRV
How does you client function look like ?
can you just add the new parameter and call itvoid boxconec::Target_IP() { QString TargetIP = QInputDialog::getText(this, "Choose Target system", "Enter the IP Address of the Target System:"); ui->IPAdress->setText(TargetIP); ClientFunction(TargetIP); }
-
@JonB after the User clicks the Button a Dialog Window pops up where the User can insert an IP Adress for example.
void boxconec::Target_IP() { QString TargetIP = QInputDialog::getText(this, "Choose Target system", "Enter the IP Address of the Target System:"); ui->IPAdress->setText(TargetIP); }
So I want to pass this TargetIP Variable to a Function that connects to a QTcp Server listening under the given Address. How can I pass that information to my Client Function?
-
@JohnSRV
How does you client function look like ?
can you just add the new parameter and call itvoid boxconec::Target_IP() { QString TargetIP = QInputDialog::getText(this, "Choose Target system", "Enter the IP Address of the Target System:"); ui->IPAdress->setText(TargetIP); ClientFunction(TargetIP); }
@mrjj Hey thanks for your reply and sorry for my late Reply. My Client Function is not complete so I tried to test this with a Simple QFile.
void BoxConnector::ProjectButtonPushed() { QString ProjectPath_UserInput = QInputDialog::getText(this, "Project", "Enter the Path to the Project"); ui->Project_Path->setText(ProjectPath_UserInput); QFile Project (Project_UserInput ); if (Project.exists()) { QMessageBox msgBox; msgBox.setText("Opening the Project.."); } else { QMessageBox ErrorBox; ErrorBox.setText("couldn't find the Project.."); } }
I can't seem to get a Message box neither for "Opening the Project.." nor for "couldn't find with the Project". Does the User Input get passed correctly to the QFile with this Code??
-
@mrjj Hey thanks for your reply and sorry for my late Reply. My Client Function is not complete so I tried to test this with a Simple QFile.
void BoxConnector::ProjectButtonPushed() { QString ProjectPath_UserInput = QInputDialog::getText(this, "Project", "Enter the Path to the Project"); ui->Project_Path->setText(ProjectPath_UserInput); QFile Project (Project_UserInput ); if (Project.exists()) { QMessageBox msgBox; msgBox.setText("Opening the Project.."); } else { QMessageBox ErrorBox; ErrorBox.setText("couldn't find the Project.."); } }
I can't seem to get a Message box neither for "Opening the Project.." nor for "couldn't find with the Project". Does the User Input get passed correctly to the QFile with this Code??
Hi
You need to call msgBox.exec(); to have it show.
Currently, you just set its text but dont tell it to show:=)
https://doc.qt.io/qt-5/qmessagebox.htmlAlso since you want the user to input the FULL path to a project what about using FileDialog instead so the user can browse to file and not have to type it all?
-
@mrjj Hey thanks for your reply and sorry for my late Reply. My Client Function is not complete so I tried to test this with a Simple QFile.
void BoxConnector::ProjectButtonPushed() { QString ProjectPath_UserInput = QInputDialog::getText(this, "Project", "Enter the Path to the Project"); ui->Project_Path->setText(ProjectPath_UserInput); QFile Project (Project_UserInput ); if (Project.exists()) { QMessageBox msgBox; msgBox.setText("Opening the Project.."); } else { QMessageBox ErrorBox; ErrorBox.setText("couldn't find the Project.."); } }
I can't seem to get a Message box neither for "Opening the Project.." nor for "couldn't find with the Project". Does the User Input get passed correctly to the QFile with this Code??
-
Thank you all for your help ! I solved my problem with the snippet from @mrjj
void boxconec::Target_IP() { QString TargetIP = QInputDialog::getText(this, "Choose Target system", "Enter the IP Address of the Target System:"); ui->IPAdress->setText(TargetIP); ClientFunction(TargetIP); }
The Application will get more complicated where the ClientFunction returns a Value that has to be passed to another slot but I'm not there yet. As for now I'm gonna mark this question as solved.
Thank you !
-
Thank you all for your help ! I solved my problem with the snippet from @mrjj
void boxconec::Target_IP() { QString TargetIP = QInputDialog::getText(this, "Choose Target system", "Enter the IP Address of the Target System:"); ui->IPAdress->setText(TargetIP); ClientFunction(TargetIP); }
The Application will get more complicated where the ClientFunction returns a Value that has to be passed to another slot but I'm not there yet. As for now I'm gonna mark this question as solved.
Thank you !
@JohnSRV
What you have ended up with is fine. But be aware you have not encountered any signal/slot stuff in it. In effect, the signal/slot stuff is being hidden from you insideQInputDialog::getText()
, and what you see at this level is a synchronous program. -
Hello Everyone,
I'm back on this topic now. I have a GUI where the User can choose which Project he wants to work with. When he pushes the Button "choose Project" a QFileDialog gets opened.
void BoxConnector::ProjectButtonPushed() { QString ProjectPath_UserInput = QFileDialog::getOpenFileName(this, tr("Open Project"), "C:/Users/Project");
Now I have many other buttons in my GUI where the user could manipulate the chosen project. How Can I pass the Path of the chosen Project to the other Slots?
I for example have a Button where the User could insert a Simulation Step Size
void BoxConnector::SimStepSizePushed() { QString SimSize_UserInput = QInputDialog::getText(this, "Simulation Step Size", "Insert Simulation Step Size"); }
How Can I pass the Path of the chosen Project in the first Slot to this Slot for example so I could manipulate the Simulation Step Size of the chosen Project?
-
Hello Everyone,
I'm back on this topic now. I have a GUI where the User can choose which Project he wants to work with. When he pushes the Button "choose Project" a QFileDialog gets opened.
void BoxConnector::ProjectButtonPushed() { QString ProjectPath_UserInput = QFileDialog::getOpenFileName(this, tr("Open Project"), "C:/Users/Project");
Now I have many other buttons in my GUI where the user could manipulate the chosen project. How Can I pass the Path of the chosen Project to the other Slots?
I for example have a Button where the User could insert a Simulation Step Size
void BoxConnector::SimStepSizePushed() { QString SimSize_UserInput = QInputDialog::getText(this, "Simulation Step Size", "Insert Simulation Step Size"); }
How Can I pass the Path of the chosen Project in the first Slot to this Slot for example so I could manipulate the Simulation Step Size of the chosen Project?
Hi
Just make
QString ProjectPath_UserInput;
a member of BoxConnector
so all slots and methods has access. -
Hi
Just make
QString ProjectPath_UserInput;
a member of BoxConnector
so all slots and methods has access. -
@mrjj I declared a QString in the BoxConnector.h.
Its Value gets set in the ProjectButtonPushed() Slot. When I call this String in another Slot it seems to be empty.
@JohnSRV
make sure you didnt do the classic error.
you line should be be
ProjectPath_UserInput = QFileDialog::getOpenFileName(this, tr("Open Project"), "C:/Users/Project");and NOT
QString ProjectPath_UserInput = QFileDialog::getOpenFileName(this, tr("Open Project"), "C:/Users/Project");
as then you still use local variable..