PDA

View Full Version : How to use connect() in IDE's(slots)



wenn32
3rd July 2010, 01:13
hello guys i need a small help how do i connect().In QT designer we add a slots in .ui file but when we are using IDE's(code blocks) how do i define a slot?

thank you

genomega
3rd July 2010, 01:56
If I understand your question correctly, look at your generated ui.mainwindow.h file and you will see.

Zlatomir
3rd July 2010, 10:01
You want to ask: how you define slots from code? In this case the answer is: just like a regular function (only that it have public slots: "access specifier") and it usually returns void (it can have a return value, but it is ignored when the slot is called from connection with signal, it can be used only if you call the slot like any other member function of your class)

wenn32
4th July 2010, 04:08
so i guess my code should be




public slots:
void my_slot()
{

/* some code here */

}


int main(int argc, char* argv[])
{

QApplication app(argc, argv);


QPushButton quit("Click Me");


QObject::connect(&quit,SIGNAL(clicked()),&app,SLOT(my_slot()));

quit.show();
return app.exec();
}



am i right.

and i am extremely sorry for not express my question properly.

Zlatomir
4th July 2010, 07:43
No, that code it's not working
The way if you really need to "add" a slot to the QApplication object is: create a class that derives from QApplication and declare the public slots in the .h file of the class, and define them in .cpp

But: I don't think you need that (at least not right now)

I think you need to connect your clicked() signal of the button to a already existing slot of your application, like this:


#include <QApplication>
#include <QtGui>

int main(int argc, char* argv[])
{

QApplication app(argc, argv);


QPushButton quit("Click Me");


QObject::connect(&quit,SIGNAL(clicked()),
&app,SLOT(quit())); // connect to already existing slot quit() of your QApplication object (meaning app)

quit.show();
return app.exec();
}


LE: i didn't said "politically correct" in the previous post, so:
this: "Slots are just like functions"
should be: "Slots are just like member functions (of some class)"

wenn32
6th July 2010, 15:59
The way if you really need to "add" a slot to the QApplication object is: create a class that derives from QApplication and declare the public slots in the .h file of the class, and define them in .cpp


can you explain me about this in detail.please!

all i want to do is when user clicks the push button it must update the old values and repaint so that new values appear.

nish
6th July 2010, 16:18
can you explain me about this in detail.please!

all i want to do is when user clicks the push button it must update the old values and repaint so that new values appear.

just look at the first example code provided with qt assistant

Zlatomir
6th July 2010, 16:21
I'm pretty sure that you don't want or need to subclass the QApplication, so tell us with more details what you want (like what widgets do you have, etc...) So we can give you a better advice.
Most likely you will need to just use signals and slots to connect your widgets and update values

LE:
Also post the code that doesn't do what you want (or some example with the same issue)

And for creating your own class by inherit from another, you need a little experience with c++, so you will need to do some tutorials or better read a book (maybe a c++ book first if you are not comfortable with it)