c++ - In Qt how do I get a button press to set a spinbox to a certain value? -


I'm trying to capture with the QT signal and slot mechanism. I have an app with QPushButton and a QSpinBox when I click on the button so I want to change the spin box to 20. Which signal and slot do I need to establish?

The code below shows the app, the connect function I have a problem. As I think the set slots of QSpinBox will not work here because QPushButton has a different signature in the clicked signal (hint), and how do I pass 20 values ​​for spinbox? Do I need to write some kind of helpful function to work as a slot, which calls spinbox-> setValue (20)? If so, what will it be?

  int main (int argc, char * argv []) {Q Application app (argc, argv); QWidget * Window = New QWidget; QSpinBox * spinbox = new QSpinBox; QPushButton * button = new QPushButton ("set to 20"); Spinbox- & gt; SetRange (0, 100); // What should I do below? QObject :: connect (button, signal (clicked)), spinbox, slot (???????)); QLayout * layout = new QHBoxLayout; Layout & gt; AddWidget (spinbox); Layout & gt; AddWidget (button); Window & gt; SetLayout (layout); Window & gt; Show (); Return app.exec (); }  

You can either:

  Class AuxSignals: public QObject {Q_OBJECT ... sign: zero value changed (int); Public slots: zero button pressed () {value (20); }}; ... // AuxSignals on main CPP * auxSignals = new AuxSignals; QObject :: connect (button, signal (clicked)), auxSignal, slot (buttonClicked)); QObject :: connect (aux signal, signal (value change), spinbox, slot (set value));  

or

  Class Aux Signal: Public QObject {Q_OBJECT ... QSpinBox * m_spinBox; Public: Aux Signal (QSpinbox * Spinbox): M-Spinbox Public Spots: Zero Button Clicks () {m_spinBox-> Set value (20); }}; ... // auxSignals * at main.cpp * auxSignals = new AuxSignals (spinbox); QObject :: connect (button, signal (clicked)), auxSignals, slot (buttonClicked ()));  

I prefer the first option because the oxsignal class does not require an indicator for specific QWidget.


Comments

Popular posts from this blog

python - Overriding the save method in Django ModelForm -

html - CSS autoheight, but fit content to height of div -

qt - How to prevent QAudioInput from automatically boosting the master volume to 100%? -