April 26, 2012

ahura_24 ahura_24
Ant Farmer
97 posts

signal slots question

 

hi everybody . i have one question

  1. #include <QtGui>
  2.  
  3. void slt()
  4. {
  5.     // do something
  6. }
  7.  
  8. int main(int argc, char *argv[])
  9. {
  10.     QApplication app(argc, argv);
  11.  
  12.     QPushButton btn;
  13.    
  14.     // connect btn signal(clicked()) to slt "function" !! i want connect to function not object!
  15.    
  16.     return app.exec();
  17. }

tnx for your help :)

6 replies

April 26, 2012

Flurite Flurite
Ant Farmer
92 posts

You must have a class that contains your slot, which is in this case, `void slot()`. Have you defined your class somewhere? If so, can we see it?

I’m no expert on this, but I think I may be able to help you here..

April 26, 2012

1+1=2 1+1=2
Hobby Entomologist
309 posts

What you need is some basic knowledge of Qt. And this will be useful for you www.catb.org/~esr/faqs/smart-questions.html

————————

What you provided are some Qt4’s code snippets, but your slt() is not a Qt SLOT at all!

However, you can do similar thing with Qt5 using the new SIGNAL & SLOT syntax.

Read the manual carefully.

April 27, 2012

ahura_24 ahura_24
Ant Farmer
97 posts

if i have too many button or …. and i want define for them different slots , what i must to do ? i must declare to many class or one global class !! if i have too many button and for each of them has been one slot , how can i declare this ?!!

April 27, 2012

sierdzio sierdzio
Area 51 Engineer
2330 posts

I think what you need is to:

  1. learn better English
  2. get some more practice in object oriented programming
  3. rethink and rephrase your questions so that other people – who want to help you – can understand them clearly
  4. read Qt documentation on signal and slot mechanism [qt-project.org], it’s really well written.

The answer to your questions, as far as I can get what you mean, is this: you can declare multiple slots in a single class, and connect your button signals to them (that is more or less the standard practice), or create one slot and handle all signals there (this is ugly, I won’t recommend).

About “how can I declare this”… really, really, DO read the docs. Here is a snippet:

  1. public slots:
  2.     void mySlot();

And in source somewhere:

  1. connect(someButton, SIGNAL(clicked()), someObject, SLOT(mySlot()));
  2.  
  3. .....
  4.  
  5. void MyClass::mySlot() {
  6.   /// code
  7. }

 Signature 

(Z(:^

April 27, 2012

Gerolf Gerolf
Area 51 Engineer
3210 posts

You can define one ore more classes with one or more slots.
signals of an object in Qt 4.x MUST be connected to a slot of an object. Whether you use one or more classes for the slots is up to you.

 Signature 

Nokia Certified Qt Specialist.
Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

April 27, 2012

sierdzio sierdzio
Area 51 Engineer
2330 posts

Damn, I do sound quite arrogant at times :( Sorry.

But I really mean it with reading the documentation.

 Signature 

(Z(:^

 
  ‹‹ Transfering QTextDocument’s contents to another      The program has unexpectedly finished ››

You must log in to post a reply. Not a member yet? Register here!