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. Passing user Input to Slots
Forum Updated to NodeBB v4.3 + New Features

Passing user Input to Slots

Scheduled Pinned Locked Moved Solved General and Desktop
17 Posts 3 Posters 1.9k Views 1 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.
  • J Offline
    J Offline
    JohnSRV
    wrote on 13 Jul 2020, 15:22 last edited by
    #1

    Hey Everyone,

    I'm new to Qt and I find the Signal/Slot Concept in this Framework really helpful. I'm doing some GUI Programming and I'm trying to get some user Input and then do some actions with it. So I have some Qpush Buttons in my GUI when the User clicks one of them he is asked to insert a number or a String.

    How can I pass the Input to a Slot that works with it ?

    J 1 Reply Last reply 13 Jul 2020, 15:25
    0
    • J JohnSRV
      13 Jul 2020, 15:22

      Hey Everyone,

      I'm new to Qt and I find the Signal/Slot Concept in this Framework really helpful. I'm doing some GUI Programming and I'm trying to get some user Input and then do some actions with it. So I have some Qpush Buttons in my GUI when the User clicks one of them he is asked to insert a number or a String.

      How can I pass the Input to a Slot that works with it ?

      J Offline
      J Offline
      JonB
      wrote on 13 Jul 2020, 15:25 last edited by JonB
      #2

      @JohnSRV
      The slot for clicking the pushbutton does not itself do anything with/know about the number/string you are about to "ask for"; it hasn't happened yet! I trust you get that bit.

      When the user has clicked the button, how do you intend to "[ask user] to insert a number or a String"? How are you going to prompt for that? Only after that can we discuss how you might use signals/slots to forward the data input onto something else.

      J 1 Reply Last reply 13 Jul 2020, 15:46
      3
      • M Offline
        M Offline
        mrjj
        Lifetime Qt Champion
        wrote on 13 Jul 2020, 15:26 last edited by
        #3

        Hi
        How do you then get this number or string?

        1 Reply Last reply
        0
        • J JonB
          13 Jul 2020, 15:25

          @JohnSRV
          The slot for clicking the pushbutton does not itself do anything with/know about the number/string you are about to "ask for"; it hasn't happened yet! I trust you get that bit.

          When the user has clicked the button, how do you intend to "[ask user] to insert a number or a String"? How are you going to prompt for that? Only after that can we discuss how you might use signals/slots to forward the data input onto something else.

          J Offline
          J Offline
          JohnSRV
          wrote on 13 Jul 2020, 15:46 last edited by
          #4

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

          M J 2 Replies Last reply 13 Jul 2020, 15:54
          0
          • J JohnSRV
            13 Jul 2020, 15:46

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

            M Offline
            M Offline
            mrjj
            Lifetime Qt Champion
            wrote on 13 Jul 2020, 15:54 last edited by
            #5

            @JohnSRV
            How does you client function look like ?
            can you just add the new parameter and call it

            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);
            }
            
            J 1 Reply Last reply 14 Jul 2020, 12:39
            3
            • J JohnSRV
              13 Jul 2020, 15:46

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

              J Offline
              J Offline
              JonB
              wrote on 13 Jul 2020, 15:54 last edited by JonB
              #6

              @JohnSRV
              I will leave you in my colleague @mrjj's capable hands for guiding you now :)

              1 Reply Last reply
              2
              • M mrjj
                13 Jul 2020, 15:54

                @JohnSRV
                How does you client function look like ?
                can you just add the new parameter and call it

                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);
                }
                
                J Offline
                J Offline
                JohnSRV
                wrote on 14 Jul 2020, 12:39 last edited by
                #7

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

                M J 2 Replies Last reply 14 Jul 2020, 12:46
                0
                • J JohnSRV
                  14 Jul 2020, 12:39

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

                  M Offline
                  M Offline
                  mrjj
                  Lifetime Qt Champion
                  wrote on 14 Jul 2020, 12:46 last edited by
                  #8

                  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.html

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

                  https://doc.qt.io/qt-5/qfiledialog.html

                  1 Reply Last reply
                  3
                  • J JohnSRV
                    14 Jul 2020, 12:39

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

                    J Offline
                    J Offline
                    JonB
                    wrote on 14 Jul 2020, 12:48 last edited by
                    #9

                    @JohnSRV
                    QMessageBox::setText() simply sets the text for the message box, it does not show it. You need to call QMessageBox::exec() to do that.

                    1 Reply Last reply
                    3
                    • J Offline
                      J Offline
                      JohnSRV
                      wrote on 15 Jul 2020, 12:45 last edited by
                      #10

                      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 !

                      J 1 Reply Last reply 15 Jul 2020, 14:00
                      2
                      • J JohnSRV
                        15 Jul 2020, 12:45

                        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 !

                        J Offline
                        J Offline
                        JonB
                        wrote on 15 Jul 2020, 14:00 last edited by
                        #11

                        @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 inside QInputDialog::getText(), and what you see at this level is a synchronous program.

                        1 Reply Last reply
                        0
                        • J Offline
                          J Offline
                          JohnSRV
                          wrote on 23 Jul 2020, 13:03 last edited by
                          #12

                          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?

                          M 1 Reply Last reply 23 Jul 2020, 13:05
                          0
                          • J JohnSRV
                            23 Jul 2020, 13:03

                            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?

                            M Offline
                            M Offline
                            mrjj
                            Lifetime Qt Champion
                            wrote on 23 Jul 2020, 13:05 last edited by
                            #13

                            Hi
                            Just make
                            QString ProjectPath_UserInput;
                            a member of BoxConnector
                            so all slots and methods has access.

                            J 1 Reply Last reply 23 Jul 2020, 13:46
                            1
                            • M mrjj
                              23 Jul 2020, 13:05

                              Hi
                              Just make
                              QString ProjectPath_UserInput;
                              a member of BoxConnector
                              so all slots and methods has access.

                              J Offline
                              J Offline
                              JohnSRV
                              wrote on 23 Jul 2020, 13:46 last edited by
                              #14

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

                              M 1 Reply Last reply 23 Jul 2020, 13:47
                              0
                              • J JohnSRV
                                23 Jul 2020, 13:46

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

                                M Offline
                                M Offline
                                mrjj
                                Lifetime Qt Champion
                                wrote on 23 Jul 2020, 13:47 last edited by
                                #15

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

                                1 Reply Last reply
                                1
                                • J Offline
                                  J Offline
                                  JohnSRV
                                  wrote on 23 Jul 2020, 13:49 last edited by
                                  #16

                                  As a matter of fact I did make the classic Mistake. Thanks !!!

                                  M 1 Reply Last reply 23 Jul 2020, 13:57
                                  1
                                  • J JohnSRV
                                    23 Jul 2020, 13:49

                                    As a matter of fact I did make the classic Mistake. Thanks !!!

                                    M Offline
                                    M Offline
                                    mrjj
                                    Lifetime Qt Champion
                                    wrote on 23 Jul 2020, 13:57 last edited by
                                    #17

                                    @JohnSRV
                                    Thats is why its a classic, very easy to do :)

                                    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