Results 1 to 4 of 4

Thread: Signal and Slot

  1. #1
    Join Date
    Jan 2015
    Posts
    1
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default Signal and Slot

    Hi i am new in Qt and i try to join signal "clicked" from QPushbutton with my own slot "Slot" but it is not working, and i dont know what i doing wrong
    could you help me?

    window.h
    Qt Code:
    1. #ifndef Window_H
    2. #define Window_H
    3.  
    4. #include <QWidget>
    5.  
    6. class Window : public QWidget
    7. {
    8. Q_OBJECT
    9. public:
    10. explicit Window(QWidget *parent=0);
    11. ~Window();
    12. };
    13. #endif
    To copy to clipboard, switch view to plain text mode 

    value.h
    Qt Code:
    1. #ifndef VALUE_H
    2. #define VALUE_H
    3. #include <QObject>
    4.  
    5. class Value : public QObject
    6. {
    7. Q_OBJECT
    8. public:
    9. explicit Value(QObject *parent=0);
    10. ~Value();
    11. public:
    12. void Fu_1(){cx=76;}
    13. int Fu_2(){return cx;}
    14. public slots:
    15. void Slot();
    16. private:
    17. int cx;
    18. };
    19.  
    20. #endif // VALUE_H
    To copy to clipboard, switch view to plain text mode 

    window.cpp
    Qt Code:
    1. #include "window.h"
    2.  
    3. Window::Window(QWidget *parent):QWidget(parent)
    4. {
    5. }
    6.  
    7. Window::~Window()
    8. {
    9. }
    To copy to clipboard, switch view to plain text mode 

    value.cpp
    Qt Code:
    1. #include "value.h"
    2.  
    3. Value::Value(QObject *parent):QObject(parent)
    4. {
    5. }
    6.  
    7. Value::~Value()
    8. {
    9. }
    10.  
    11. void Value::Slot()
    12. {
    13. Fu_1();
    14. }
    To copy to clipboard, switch view to plain text mode 

    main.cpp
    Qt Code:
    1. #include <QApplication>
    2. #include "window.h"
    3. #include "value.h"
    4. #include <QtWidgets>
    5. #include <QHBoxLayout>
    6.  
    7.  
    8. int main(int argc, char*argv[])
    9. {
    10. QApplication app(argc,argv);
    11. Window *Win=new Window;
    12. QPushButton *Object_1=new QPushButton;
    13. QLCDNumber *Object_2=new QLCDNumber;
    14. Value *Object_3=new Value;
    15.  
    16. QObject::connect(Object_1,SIGNAL(clicked()),Object_3,SLOT(Slot())); // this is not working
    17. Object_3->Fu_1(); // but when i try to call Fu_1 through Object its working normaly
    18. int x;
    19. x=Object_3->Fu_2();
    20. Object_2->display(x); // it is working because value of cx was set on 76
    21.  
    22. Lay->addWidget(Object_1);
    23. Lay->addWidget(Object_2);
    24.  
    25. Win->setLayout(Lay);
    26. Win->show();
    27. return app.exec();
    28. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by anda_skoa; 19th January 2015 at 10:31. Reason: missing [code] tags

  2. #2
    Join Date
    Oct 2009
    Posts
    26
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Signal and Slot

    Win->show() is equivalent of setVisible(true). It shows up the QWindow.
    To start its events, you have to add after showing-up the window:
    Qt Code:
    1. Win->activateWindow();
    To copy to clipboard, switch view to plain text mode 

  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: Signal and Slot

    Quote Originally Posted by hayzel View Post
    Win->show() is equivalent of setVisible(true). It shows up the QWindow.
    To start its events, you have to add after showing-up the window:
    Qt Code:
    1. Win->activateWindow();
    To copy to clipboard, switch view to plain text mode 
    No, that's not true, you don't have to do anything like that.
    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
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Signal and Slot

    Qt Code:
    1. QObject::connect(Object_1,SIGNAL(clicked()),Object _3,SLOT(Slot())); // this is not working
    To copy to clipboard, switch view to plain text mode 
    How do you determine this? The slot is not called until the button is clicked, when the program is in app.exec(), and does not do anything that is outwardly visible.

Similar Threads

  1. Replies: 1
    Last Post: 14th August 2014, 17:08
  2. Replies: 6
    Last Post: 4th March 2014, 15:09
  3. Replies: 8
    Last Post: 7th November 2012, 14:10
  4. Replies: 2
    Last Post: 3rd May 2011, 20:22
  5. signal slot conection using a string, not a SLOT
    By rianquinn in forum Qt Programming
    Replies: 6
    Last Post: 5th February 2006, 18:52

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.