Results 1 to 4 of 4

Thread: connect between function of two differents QWidget

  1. #1
    Join Date
    Aug 2009
    Posts
    30
    Thanks
    13
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11

    Default connect between function of two differents QWidget

    I have two classes : classA and classB

    I would like to connect a function F() from classB with a click event from a button ui of the classA.

    I know there is the possibitlity to have the classB member of the class A. But there is another to do it if the class are separe?

    The connect function shoud be in the class A connect(ui->button,SIGNAL(clicked(),b->F())); ???

    Qt Code:
    1. class classA : public QWidget
    2. {
    3. Q_OBJECT
    4.  
    5. public:
    6. classA(QWidget *parent = 0);
    7. ~classA();
    8.  
    9. private:
    10. Ui::board *ui;
    11. };
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. class classB: public QWidget
    2. {
    3. Q_OBJECT
    4.  
    5. public:
    6. classB(QWidget *parent = 0);
    7. ~classB();
    8. void F();
    9. };
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: connect between function of two differents QWidget

    Try using slots:

    Qt Code:
    1. class classA : public QWidget
    2. {
    3. Q_OBJECT
    4.  
    5. public:
    6. classA(QWidget *parent = 0);
    7. ~classA();
    8.  
    9. private:
    10. Ui::board *ui;
    11. classB *b;
    12. };
    13.  
    14. classA::classA(...) : QWidget(...)
    15. {
    16. b = new classB(this);
    17.  
    18. connect(ui->button, SIGNAL(clicked()), b, SLOT(f()));
    19. }
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. class classB: public QWidget
    2. {
    3. Q_OBJECT
    4.  
    5. public:
    6. classB(QWidget *parent = 0);
    7. ~classB();
    8.  
    9. public slots:
    10. void f();
    11. };
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Aug 2009
    Posts
    30
    Thanks
    13
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11

    Default Re: connect between function of two differents QWidget

    Quote Originally Posted by tbscope View Post
    Try using slots:

    Qt Code:
    1. class classA : public QWidget
    2. {
    3. Q_OBJECT
    4.  
    5. public:
    6. classA(QWidget *parent = 0);
    7. ~classA();
    8.  
    9. private:
    10. Ui::board *ui;
    11. classB *b;
    12. };
    13.  
    14. classA::classA(...) : QWidget(...)
    15. {
    16. b = new classB(this);
    17.  
    18. connect(ui->button, SIGNAL(clicked()), b, SLOT(f()));
    19. }
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. class classB: public QWidget
    2. {
    3. Q_OBJECT
    4.  
    5. public:
    6. classB(QWidget *parent = 0);
    7. ~classB();
    8.  
    9. public slots:
    10. void f();
    11. };
    To copy to clipboard, switch view to plain text mode 
    This is the solution simplest solution. But I dont want to have the object B (from classB) membres of the classA.

    I don't know if there is a solution to list all the Qwidgets present in an application to call their functions ????

  4. #4
    Join Date
    Jun 2010
    Location
    Salatiga, Indonesia
    Posts
    160
    Thanks
    11
    Thanked 32 Times in 29 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    1

    Default Re: connect between function of two differents QWidget

    You can create custom signal in your classA, for example buttonClicked(), that will be emitted whenever ui->button clicked() signal is emitted. So, you can connect buttonClicked() signal from classA instance to F() slot from classB instance without classB instance to be a member of classA.

    Or you can create a function that return a pointer to ui->button.

    Qt Code:
    1. class classA : public QWidget
    2. {
    3. Q_OBJECT
    4. public:
    5. classA(QWidget *parent = 0);
    6. QPushButton *button();
    7. ~classA();
    8.  
    9. signals:
    10. void buttonClicked();
    11.  
    12. private:
    13. Ui::board *ui;
    14. };
    15.  
    16. classA::classA(QWidget *parent) : QWidget(parent), ui(new Ui::board)
    17. {
    18. ui->setupUi(this);
    19. connect(ui->button, SIGNAL(clicked()), this, SIGNAL(buttonClicked()));
    20. }
    21.  
    22. QPushButton* classA::button()
    23. {
    24. return ui->button;
    25. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. class classB: public QWidget
    2. {
    3. Q_OBJECT
    4.  
    5. public:
    6. classB(QWidget *parent = 0);
    7. ~classB();
    8.  
    9. public slots:
    10. void F();
    11. };
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. classA a;
    2. classB b;
    3. QObject::connect(&a, SIGNAL(buttonClicked()), &b, SLOT(F()));
    4. // or
    5. QObject::connect(a.button(), SIGNAL(clicked()), &b, SLOT(F()));
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. connect a QPushButton matrix with a function
    By harmodrew in forum Newbie
    Replies: 6
    Last Post: 6th August 2010, 11:11
  2. Replies: 4
    Last Post: 7th May 2009, 08:27
  3. Replies: 1
    Last Post: 12th January 2009, 18:05
  4. Displaying QWidget from a function.
    By raghvendramisra in forum Qt Programming
    Replies: 2
    Last Post: 23rd January 2008, 04:02
  5. function 'connect' always look super class for slots.
    By Intaek Lim in forum Qt Programming
    Replies: 7
    Last Post: 12th March 2007, 13:38

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
  •  
Qt is a trademark of The Qt Company.