Results 1 to 4 of 4

Thread: Signal and Slot

Threaded View

Previous Post Previous Post   Next Post Next Post
  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

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.