PDA

View Full Version : [noob] How can I have a widget event call a function?



Envergure
22nd November 2008, 17:26
I'm transitioning from Visual BASIC to Qt.

Let's say I want to have a button change colour when it's clicked. How do I arrange for the click to trigger a function that changes its colour?

wysota
22nd November 2008, 18:19
You need to use a feature called "signals and slots". Open Qt Assistant and enter "Signals and Slots" into the index tab.

Envergure
25th November 2008, 17:00
OK, interesting...

I connect my button's "clicked()" signal to its own "click()" slot, and I have myButton::click() change the colour? Or do I have to make a custom slot (and to do that do I need to make a custom "myQButton" class)?


Basically, can I have my button change colour by editing only main.cpp and mainWidow.cpp?

fnmblot
25th November 2008, 17:42
http://doc.trolltech.com/4.4/index.html is your best friend. I use it on a daily basis.

wysota
25th November 2008, 20:09
You need to create your own custom slot somewhere (like in mainWindow.cpp) and connect the signal to it. Inside the slot you need to perform the changes you want.

Envergure
27th November 2008, 00:17
Yay, it's working!

I had to call my button by "ui.pushButton" instead of just "pushButton". Otherwise I got "pushButton not declared in this scope."