Results 1 to 4 of 4

Thread: Signals and Slots questions

  1. #1
    Join Date
    Dec 2009
    Posts
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Signals and Slots questions

    1) I have 2 classes, first is MyForm, and second classTwo. MyForm have private object of type classTwo named objTwo - Is that a good idea? or better to create both of it in main.c as coexistent?

    2) In MyForm constructor I try to connect SIGNAL from MyForm to SLOT from classTwo - it's not working, why?
    Qt Code:
    1. connect(widget.buttonX, SIGNAL(clicked()), objTwo, SLOT(slotX(widget.edit->text())));
    To copy to clipboard, switch view to plain text mode 

    As a workaround I declared new SLOT in MyForm which call objTwo.slotX() - this way it works, but Im sure this is bad practice, or isn't it?

    The real problem comes when I want to connect SIGNAL from classTwo to MyForm class (in MyForm constructor). The only way I see is to cross include class headers, but I do want to make it properly, so obviously i miss something, as cross including is bad practice?

    I need to send something to classTwo from MyForm and then wait for signal to read answer back.

    Please help.
    Last edited by Janek; 29th December 2009 at 19:02.

  2. #2
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Signals and Slots questions

    The SLOT can only contain the function signature. It can not accept arguments.

    Qt Code:
    1. connect(widget.buttonX, SIGNAL(clicked()), objTwo, SLOT(slotX()));
    To copy to clipboard, switch view to plain text mode 

    If you check the output window, you will notice an error message being printed with your version.

  3. #3
    Join Date
    Jan 2008
    Location
    Poland
    Posts
    687
    Thanks
    4
    Thanked 140 Times in 132 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Signals and Slots questions

    Quote Originally Posted by Janek View Post
    1) I have 2 classes, first is MyForm, and second classTwo. MyForm have private object of type classTwo named objTwo - Is that a good idea? or better to create both of it in main.c as coexistent?

    2) In MyForm constructor I try to connect SIGNAL from MyForm to SLOT from classTwo - it's not working, why?
    Qt Code:
    1. connect(widget.buttonX, SIGNAL(clicked()), objTwo, SLOT(slotX(widget.edit->text())));
    To copy to clipboard, switch view to plain text mode 
    in SIGNAL() and SLOT() macros you give a signature of a method so the name of a method an type names of arguments, for example:
    when you have class:
    Qt Code:
    1. class A : public QObject
    2. {
    3. Q_OBJECT
    4. public:
    5. A(QObject *parent = 0);
    6. public slots:
    7. void myMethod(int a, const QString str);
    8. };
    To copy to clipboard, switch view to plain text mode 
    and you want to connect some signal to the slot myMethod you have to write it like this:
    Qt Code:
    1. SLOT(myMethod(int, QString))
    To copy to clipboard, switch view to plain text mode 
    as you see just names of types, so you can't pass any arguments to slots in a connect() statement.
    whats more you can't connect signal with no arguments (like clicked()) with a slot which has arguments, in general, you can't connect signal with less arguments then slot which you connect to.

    So in your situation solution looks like:
    1. Make a slot with no arguemnts, lets say slotClicked()
    2. connect with signal clicked():
    Qt Code:
    1. connect(widget.buttonX, SIGNAL(clicked()), objTwo, SLOT(slotClicked()));
    To copy to clipboard, switch view to plain text mode 
    3. in slotClicked() do something with that thing what you want.
    4. but I dont know why you have a slot in different class to process text from a lineEdit() in different class... maybe make that slot in MyForm like this:
    Qt Code:
    1. void MyForm::slotClicked()
    2. {
    3. objTwo->doSomething(widget.edit->text());
    4. }
    To copy to clipboard, switch view to plain text mode 
    that would be the easiest way.


    P.S. Ohh and of course try reading some documentation and examples before trying to code, because all I said is written there with more examples and more clearly.
    I would like to be a "Guru"

    Useful hints (try them before asking):
    1. Use Qt Assistant
    2. Search the forum

    If you haven't found solution yet then create new topic with smart question.

  4. #4
    Join Date
    Dec 2009
    Posts
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Signals and Slots questions

    Thank You very much, that clears up a lot, and of course I have to read a lot more about Qt

Similar Threads

  1. Signals and Slots Problem
    By GenericProdigy in forum Qt Programming
    Replies: 4
    Last Post: 2nd February 2009, 09:06
  2. Signals and Slots
    By 83.manish in forum Qt Programming
    Replies: 3
    Last Post: 30th June 2008, 10:31
  3. Problem with SpinBox signals and slots
    By ramstormrage in forum Newbie
    Replies: 4
    Last Post: 2nd May 2008, 01:45
  4. signals and slots in plugins
    By anderl in forum Qt Programming
    Replies: 1
    Last Post: 10th October 2007, 13:57
  5. Signals and Slots question
    By Thoosle in forum Qt Programming
    Replies: 5
    Last Post: 5th December 2006, 00:24

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.