Thanks for getting me started but I'm still having an issue understanding the signals and slots use with a radio button. I'm not understanding how we can read a radio button as a signal.
Thanks for getting me started but I'm still having an issue understanding the signals and slots use with a radio button. I'm not understanding how we can read a radio button as a signal.
As I said, you need to understand event-driven programming. In event-driven programming, your code is not in charge, and it usually does not "read" anything. Instead, your program contains code that reacts to things happening in the user interface. When something you are interested in happens, the code you have written to react to it gets executed.I'm not understanding how we can read a radio button as a signal.
Your code does not sit there spinning its wheels and continually asking the UI if anything has changed. Your code does nothing until the UI sends it a signal (by calling one of your functions) to let you know something has changed. Once your code is done reacting to that change, it goes back to doing nothing until the next UI signal or event comes along. (Behind the scenes, the Qt event loop is running and looking for things like key clicks and mouse actions, but your program does not need to be aware of or care about that. It just waits for its slots to get executed).
You do not "read" radio buttons in this scenario. A QRadioButton (actually its base class, QAbstractButton) has a signal (member function) named "clicked()". The signal also passes a Boolean parameter that is "true" if the radio button is checked, false if it is not. When the user clicks the radio button, it executes the clicked() function. That function determines if there are any slots connected to it, and if so, calls those slots (which are just member functions as well) and passes the Boolean "checked" value.
In your dialog / main window class, you define slots, call them whatever you want, that you will connect to the two radio buttons' signals. Say you call these slots "onMLClicked( bool bChecked )" and "onOZClicked( bool bChecked )". You also create a member variable for your class, a Boolean named "mlToOz" for example. In your slots, you examine the value of bChecked. In "onMLClicked()" if bChecked is true, then you set mlToOz to true also because that's the radio button that is turned on. If bChecked is false, you set mlToOz to false. In the other slot (onOzClicked()), you do exactly the opposite - set the variable to false if bChecked is true and vice-versa, because if OZ is checked, then you want to do the opposite conversion.
To wire things up, when you create your radio buttons, you call QObject::connect() to connect the radio button signal to the corresponding slot in your class. In C++ this looks like the following; you'll have to learn the corresponding Python syntax:
Qt Code:
// Usually this code goes in the MyDialog constructor; in Python, that's the _init_ method, I think connect( mlButton, &QRadioButton:clicked, this, &MyDialog::onMLClicked ); // And say you also have a Calculate button, you connect its signal to your slot connect( calculateBtn, &QPushButton::clicked, myDialog, &MyDialog::onCalculate ); // And the slots look like this void MyDialog::onMLClicked( bool bChecked ) { if ( bChecked ) mlToOz = true; else mlToOz = false; } void MyDialog::onCalculate() { if ( mlToOz ) // perform the ML -> OZ conversion of the value in the input line edit else // perform the OZ -> ML conversion // and set the result (after formatting to a string) into the output line edit }To copy to clipboard, switch view to plain text mode
Why don't you study this tutorial? It is not so much different from what you want to do.
<=== The Great Pumpkin says ===>
Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.
Bookmarks