Results 1 to 14 of 14

Thread: Beginner problem with connect

  1. #1
    Join Date
    Jul 2011
    Posts
    8
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Beginner problem with connect

    So there's the source files that qt created for me (main.cpp and program.cpp). I made with Qt Designer the basics of a dialog, and now I'd like to do something like:
    Creating window1.cpp and window1.h;
    Creating a class like class wnd1 and put some functions and slots to use the connect function to handle buttons' signals.

    So far what I could do was handling this signals inside program.cpp, but everything I tried to do that at window1.cpp failed (like not having connect accept my class, slots not being allowed to be declared, etc). How could be an intelligent way to do that? I don't want to do that all inside program.cpp, it would be a mess because the program will have lots of tabs and controls. I hope my question is clear. If possible, I'd like some examples.

    Thanks.

  2. #2
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: Beginner problem with connect

    If window1 class has signals / slots then you should be using Q_OBJECT macro at the beginning of the class definition, check if you are missing this.

  3. #3
    Join Date
    Jul 2011
    Posts
    8
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Beginner problem with connect

    program.cpp
    Qt Code:
    1. #include "global.h"
    2. #include "program.h"
    3. #include "window1.h"
    4. #include "ui_program.h"
    5.  
    6. program::program(QWidget *parent) :
    7. QMainWindow(parent),
    8. ui(new Ui::program)
    9. {
    10. ui->setupUi(this);
    11. wnd1 w1;
    12. w1.Initialize();
    13.  
    14. }
    15.  
    16. program::~program()
    17. {
    18. delete ui;
    19. }
    To copy to clipboard, switch view to plain text mode 

    window1.h
    Qt Code:
    1. #ifndef WINDOW1_H
    2. #define WINDOW1_H
    3. #include <QObject>
    4.  
    5. class wnd1 : public QObject {
    6. Q_OBJECT
    7. public:
    8. void Initialize();
    9.  
    10. private slots:
    11. void test();
    12.  
    13.  
    14. };
    15. #endif // WINDOW1_H
    To copy to clipboard, switch view to plain text mode 

    window1.cpp
    Qt Code:
    1. #include "global.h"
    2. #include "window1.h"
    3. #include "program.h"
    4.  
    5. program oProg;
    6.  
    7.  
    8.  
    9. void wnd1::Initialize() {
    10. QPushButton *b = oProg.centralWidget()->findChild<QPushButton *>("button1"); // seems i cant use oProg. some reason
    11. wnd1 w1;
    12. w1.connect(b, SIGNAL(clicked()), SLOT(test()));
    13.  
    14. }
    15.  
    16.  
    17. void wnd1::test() {
    18. //do something
    19. }
    To copy to clipboard, switch view to plain text mode 

    in global.h
    Qt Code:
    1. #include <QtGui/QApplication>
    2. #include <QtGui/QMessageBox>
    To copy to clipboard, switch view to plain text mode 

    15: error: no matching function for call to ‘wnd1::connect(QPushButton*&, const char [11], const char [8])’

    Thanks.
    Last edited by lucastonon; 13th July 2011 at 20:59.

  4. #4
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: Beginner problem with connect

    Use it this way

    Qt Code:
    1. //program.cpp
    2. program::program(QWidget *parent) :
    3. QMainWindow(parent),
    4. ui(new Ui::program)
    5. {
    6. ui->setupUi(this);
    7. wnd1 * w1 = new wnd1(this);
    8. QPushButton *b = ui->pushButton;
    9.  
    10. connect(b, SIGNAL(clicked()), w1, SLOT(test()));
    11. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by Santosh Reddy; 13th July 2011 at 21:35.

  5. The following user says thank you to Santosh Reddy for this useful post:

    lucastonon (14th July 2011)

  6. #5
    Join Date
    Jul 2011
    Posts
    8
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Beginner problem with connect

    That line you asked me is the way I found at the internet to get a 'handle' to an object created by Qt Designer, instead of manually creating it with commands. It works fine in program.cpp but not in window1.cpp.
    Thanks.

  7. #6
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: Beginner problem with connect

    Looks like you still have to figure out the basic object creation and scope work (it's C++ stuff)

    Qt Code:
    1. //program.cpp
    2. wnd1 w1; //this will create object on stack and will be destroyed as soon as function returns
    3. w1.Initialize(); //this is ok
    To copy to clipboard, switch view to plain text mode 


    Qt Code:
    1. //window1.cpp
    2. program oProg; //this not good, you are creating a global QMainWindow, that means you are trying to create a widget even before QApplication is created
    3.  
    4. void wnd1::Initialize() {
    5. QPushButton *b = oProg.centralWidget()->findChild<QPushButton *>("button1"); // you are not supposed to get the member widgets of other class this way, it will work but will end up in problems later
    6. wnd1 w1; //this will create object on stack and will be destroyed as soon as function returns
    7. w1.connect(b, SIGNAL(clicked()), SLOT(test())); //there is no such version of connect function, hence the error, and moreover it does not make sense to connect a object which will be deleted before this function returns. So even if you were able to get the correct version of the function, your slot will never get called, as the object itself is destroyed before this function returns
    8. }
    To copy to clipboard, switch view to plain text mode 

    Please try, as I suggested in my earlier post

  8. The following user says thank you to Santosh Reddy for this useful post:

    lucastonon (14th July 2011)

  9. #7
    Join Date
    Jul 2011
    Posts
    8
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Beginner problem with connect

    Mmm I got it, but I wanted to do that (connecting) in another .cpp file, just because of organization, can't it be done?

    Another question using your example:
    Qt Code:
    1. //program.cpp
    2. program::program(QWidget *parent) :
    3. QMainWindow(parent),
    4. ui(new Ui::program)
    5. {
    6. ui->setupUi(this);
    7.  
    8. wnd1 *w1 = new wnd1;
    9. connect(ui->button1, SIGNAL(clicked()), w1, SLOT(test()));
    10.  
    11. }
    12.  
    13. //window1.cpp
    14. void wnd1::test() { //is it ok doing that in this function?[/COLOR]
    15. program *ep = new program;
    16. QMessageBox::information(ep, "a", "b");
    17. delete ep;
    18. }
    To copy to clipboard, switch view to plain text mode 

    Thanks
    Last edited by lucastonon; 14th July 2011 at 05:16.

  10. #8
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: Beginner problem with connect

    it can be done, (and is done most of times this way), you can create a signal of program class (instead of button signal) and connect to win1 class objects

  11. #9
    Join Date
    Jul 2011
    Posts
    8
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Beginner problem with connect

    You mean creating a signal for something like a tab? How to do that? :s
    Thanks

  12. #10
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: Beginner problem with connect

    here is an example
    Qt Code:
    1. //program.h
    2. #include <QtGui>
    3. #include "ui_program.h"
    4. class wnd1;
    5. class program : public QMainWindow, public
    6. {
    7. Q_OBJECT;
    8. public:
    9. explicit program(QWidget * parent = 0);
    10. signals:
    11. void openButtonClicked(void);
    12. private:
    13. Ui::program * ui;
    14. wnd1 * w1;
    15. };
    16.  
    17. //program.cpp
    18. #include "program.h"
    19. #include "window1.h"
    20.  
    21. program::program(QWidget *parent) :
    22. QMainWindow(parent),
    23. ui(new Ui::program()),
    24. w1(new wnd1(this))
    25. {
    26. ui->setupUi(this);
    27. connect(this, SIGNAL(openButtonClicked()), w1, SLOT(test()));
    28. }
    29.  
    30. program::~program()
    31. {
    32. delete ui;
    33. }
    34.  
    35. //window1.h
    36. #include <QtGui>
    37. class wnd1 : public QObject
    38. {
    39. Q_OBJECT;
    40. public:
    41. explicit wnd1(QObject * parent = 0);
    42. public slots:
    43. void test(void);
    44. };
    45.  
    46. //window1.cpp
    47. #include "window1.h"
    48. wnd1::wnd1(QObject * parent) : QObject(parent)
    49. {
    50. ;
    51. }
    52.  
    53. void wnd1::test(void)
    54. {
    55. ;//do something
    56. }
    To copy to clipboard, switch view to plain text mode 

  13. #11
    Join Date
    Jul 2011
    Posts
    8
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Beginner problem with connect

    Quote Originally Posted by Santosh Reddy View Post
    it can be done, (and is done most of times this way), you can create a signal of program class (instead of button signal) and connect to win1 class objects
    I don't think you got. What I understood from this quoted post was that if I had a tab widget, I could connect example: tab1 to wnd1(window1.cpp), tab2 to wnd2(window2.cpp); and then every control from the connected tab would signal the connected object, but forget that. I just wanted to do something like:
    Qt Code:
    1. //program.cpp
    2. program::program(QWidget *parent) :
    3. QMainWindow(parent),
    4. ui(new Ui::program)
    5. {
    6. ui->setupUi(this);
    7.  
    8. wnd1 w1;
    9. w1.Initialize();
    10.  
    11. }
    12.  
    13. //window1.cpp
    14. void wnd1::Initialize() {
    15. connect(ui->button1, SIGNAL(clicked()), this, SLOT(test1()));
    16. connect(ui->button2, SIGNAL(clicked()), this, SLOT(tes2t()));
    17. connect(ui->button3, SIGNAL(clicked()), this, SLOT(test3()));
    18. connect(ui->button4, SIGNAL(clicked()), this, SLOT(test4()));
    19. connect(ui->button5, SIGNAL(clicked()), this, SLOT(test5()));
    20. // but i don't know how to get a handle the buttons in window1.cpp since 'ui' is from program.cpp
    21. }
    To copy to clipboard, switch view to plain text mode 

    Sorry for the misunderstanding.
    Last edited by lucastonon; 14th July 2011 at 06:04.

  14. #12
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: Beginner problem with connect

    // but i don't know how to get a handle the buttons in window1.cpp since 'ui' is from program.cpp
    I was trying to tell you that, make the connections in program.cpp not is window1.cpp, move the connect statements to program class ctor (this is a standard way doing).
    In program.cpp you will have handles to buttons, and you also know that wnd1 class has an public slot testx()

  15. #13
    Join Date
    Jul 2011
    Posts
    8
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Beginner problem with connect

    Ok I'll make the connections at program.cpp. But in case in window1.cpp I need to get the text of a text edit I would need to have ui->textEdit1 in window1.cpp, but I can only use that in program.cpp

  16. #14
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: Beginner problem with connect

    You are still getting it worng, do not create wnd1 variable in program.cpp constructor. (it will not work) also, refer to my post (Post #4) for example to create objects

    I can see you are complicating your situation, Please review your code again.

    Problem: You are tyring to create a object of program class with in a slot connected to a signal emitted by program class it self, why do you need to do it this wa? I am not sure of your requrements, but you don't want this.

    Either create program class object in wnd1 class or create wnd1 class object in program class, but don't do both

Similar Threads

  1. Problem with connect
    By eekhoorn12 in forum Newbie
    Replies: 3
    Last Post: 21st December 2010, 21:48
  2. connect() problem
    By harmodrew in forum Newbie
    Replies: 14
    Last Post: 5th August 2010, 18:45
  3. Connect Problem
    By nrabara in forum Newbie
    Replies: 3
    Last Post: 4th May 2009, 13:19
  4. Problem with connect()
    By mrnor3 in forum Qt Programming
    Replies: 3
    Last Post: 23rd July 2008, 15:05
  5. Qfiledialog Problem - (Beginner)
    By kingslee in forum Qt Tools
    Replies: 3
    Last Post: 12th October 2006, 00:00

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.