Results 1 to 7 of 7

Thread: Templates and signals

  1. #1
    Join Date
    Dec 2010
    Posts
    3
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Templates and signals

    Hi!

    Code:

    Header
    Qt Code:
    1. template <class D>
    2. class Signal
    3. : public QObject
    4. {
    5. public:
    6. void signal(D);
    7. signals:
    8. void send(D);
    9. };
    To copy to clipboard, switch view to plain text mode 


    cpp
    Qt Code:
    1. template <class D> void Signal<D>::signal(D data)
    2. {
    3. emit send(data);
    4. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. namespace plot{
    2. Signal* xSignal;
    3. }
    4. //------------------------
    5. plot::xSignal = new Signal();
    6. QObject::connect(plot::xSignal, SIGNAL(send(double)), this, SLOT(notImportant(double)));
    To copy to clipboard, switch view to plain text mode 

    Everything still works to compile here but in next step something happend

    Qt Code:
    1. double data = 0;
    2. plot::xSignal->signal(data);
    To copy to clipboard, switch view to plain text mode 

    This code should send a signal to notImportant() like this:
    First to "Signal::signal(double)" which send signal to "Signal::send(double)" which is connected to "notImportant(double)".
    But I can't compile when I add the last line I got an error message.
    Line:
    Qt Code:
    1. plot::xSignal->signal(data);
    To copy to clipboard, switch view to plain text mode 
    Error message is:
    Qt Code:
    1. error: undefined reference to `Signal<double>::signal(double)''
    To copy to clipboard, switch view to plain text mode 

    I am new to templates so it is propably someting wrong there.

    Does any of you know whats wrong?

    Thanks
    Ubi

    Sorry for my english, I am from Sweden

  2. #2
    Join Date
    Sep 2010
    Posts
    145
    Thanks
    1
    Thanked 18 Times in 17 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Templates and signals

    Did you not mean
    Qt Code:
    1. plot::xSignal = new Signal<double>();
    To copy to clipboard, switch view to plain text mode 
    instead of
    Qt Code:
    1. plot::xSignal = new Signal();
    To copy to clipboard, switch view to plain text mode 

    Also where is your constructor for Signal?

  3. #3
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Templates and signals

    Just to warn you - Qt signals/slots mechanism is unable to cooperate with template classes.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  4. #4
    Join Date
    Dec 2010
    Posts
    3
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Templates and signals

    Quote Originally Posted by Timoteo View Post
    Did you not mean
    Qt Code:
    1. plot::xSignal = new Signal<double>();
    To copy to clipboard, switch view to plain text mode 
    instead of
    Qt Code:
    1. plot::xSignal = new Signal();
    To copy to clipboard, switch view to plain text mode 

    Also where is your constructor for Signal?
    Yes, somehow chrome removed <double> so I used firefox instead and forgot to add it again.

    Quote Originally Posted by wysota View Post
    Just to warn you - Qt signals/slots mechanism is unable to cooperate with template classes.
    Thanks for the warning.
    Then I do it in some other way

  5. #5
    Join Date
    Sep 2010
    Posts
    145
    Thanks
    1
    Thanked 18 Times in 17 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Templates and signals

    As an example of something you could do, I threw together some code real quick. This uses libsigc++.
    Qt Code:
    1. template<typename rt, typename T> class EmittingObject
    2. {
    3. std::tr1::shared_ptr<T> t;
    4. sigc::signal<rt> m_signal;
    5. public:
    6. EmittingObject()
    7. {
    8. t = std::tr1::shared_ptr<T>(new T);
    9. }
    10. sigc::signal<rt>& getSignal() { return m_signal; }
    11. std::tr1::shared_ptr<T> object() { return t;}
    12. };
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. /*class demonstrating a trivial connection*/
    2. class XWidget : public EmittingObject<void, QWidget>, public sigc::trackable
    3. {
    4. void handle_signal()
    5. {
    6. this->object()->close();
    7. }
    8. public:
    9. XWidget()
    10. {
    11. getSignal().connect(sigc::mem_fun(this, &XWidget::handle_signal));
    12. }
    13. void doSomethingToCauseAnEmit()
    14. {
    15. if(QMessageBox::question(object().get(), "Hello!", "Keep the widget open?", QMessageBox::Yes | QMessageBox::No) == QMessageBox::No)
    16. getSignal().emit();
    17. }
    18. };
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. /*quick test piece*/
    2. int main(int argc, char* argv[])
    3. {
    4. QApplication app(argc, argv);
    5. XWidget widget;
    6. widget.object()->show();
    7. while(widget.object()->isVisible())
    8. {
    9. widget.doSomethingToCauseAnEmit();
    10. }
    11. app.exit(0);
    12. }
    To copy to clipboard, switch view to plain text mode 

    Sorry for the poor formatting, but that is Visual Assist's fault.

    This is a silly example, I know. The basic concept is to sidestep Qt's signal/slot mechanism and employ another (if you truly need this type of thing, you may not!).

  6. #6
    Join Date
    Dec 2010
    Posts
    3
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Templates and signals

    Quote Originally Posted by Timoteo View Post
    As an example of something you could do, I threw together some code real quick. This uses libsigc++.
    Qt Code:
    1. template<typename rt, typename T> class EmittingObject
    2. {
    3. std::tr1::shared_ptr<T> t;
    4. sigc::signal<rt> m_signal;
    5. public:
    6. EmittingObject()
    7. {
    8. t = std::tr1::shared_ptr<T>(new T);
    9. }
    10. sigc::signal<rt>& getSignal() { return m_signal; }
    11. std::tr1::shared_ptr<T> object() { return t;}
    12. };
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. /*class demonstrating a trivial connection*/
    2. class XWidget : public EmittingObject<void, QWidget>, public sigc::trackable
    3. {
    4. void handle_signal()
    5. {
    6. this->object()->close();
    7. }
    8. public:
    9. XWidget()
    10. {
    11. getSignal().connect(sigc::mem_fun(this, &XWidget::handle_signal));
    12. }
    13. void doSomethingToCauseAnEmit()
    14. {
    15. if(QMessageBox::question(object().get(), "Hello!", "Keep the widget open?", QMessageBox::Yes | QMessageBox::No) == QMessageBox::No)
    16. getSignal().emit();
    17. }
    18. };
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. /*quick test piece*/
    2. int main(int argc, char* argv[])
    3. {
    4. QApplication app(argc, argv);
    5. XWidget widget;
    6. widget.object()->show();
    7. while(widget.object()->isVisible())
    8. {
    9. widget.doSomethingToCauseAnEmit();
    10. }
    11. app.exit(0);
    12. }
    To copy to clipboard, switch view to plain text mode 

    Sorry for the poor formatting, but that is Visual Assist's fault.

    This is a silly example, I know. The basic concept is to sidestep Qt's signal/slot mechanism and employ another (if you truly need this type of thing, you may not!).
    Thanks for the example! Taught me a lot!

    I'm not sure I will use it in this project though, because I don't really need it.

    But I am sure it will be usefull in my other projects!

  7. #7
    Join Date
    Sep 2010
    Posts
    145
    Thanks
    1
    Thanked 18 Times in 17 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Templates and signals

    Glad you liked it, but like I said it is not meant to be a great example - just something to show what could be a great alternative if enough time is put into it (and a better design - composition would have been more appropriate than inheritance for example).

Similar Threads

  1. operator<< and templates
    By mickey in forum General Programming
    Replies: 3
    Last Post: 30th May 2008, 17:13
  2. templates
    By mickey in forum General Programming
    Replies: 5
    Last Post: 16th January 2008, 15:25
  3. QThread and signals (linux/UNIX signals not Qt Signals)
    By Micawber in forum Qt Programming
    Replies: 1
    Last Post: 28th November 2007, 22:18
  4. dynamic_cast and templates
    By KShots in forum General Programming
    Replies: 7
    Last Post: 7th August 2007, 20:01
  5. Templates : Help Please
    By sunil.thaha in forum General Programming
    Replies: 4
    Last Post: 14th February 2006, 13:50

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.