Results 1 to 14 of 14

Thread: Beginner problem with connect

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Thanks
    3
    Thanked 453 Times in 435 Posts
    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

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

    Default Re: Beginner problem with connect

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

  3. #3
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Thanks
    3
    Thanked 453 Times in 435 Posts
    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 

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

    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 05:04.

  5. #5
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Thanks
    3
    Thanked 453 Times in 435 Posts
    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()

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

    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

  7. #7
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Thanks
    3
    Thanked 453 Times in 435 Posts
    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, 20:48
  2. connect() problem
    By harmodrew in forum Newbie
    Replies: 14
    Last Post: 5th August 2010, 17:45
  3. Connect Problem
    By nrabara in forum Newbie
    Replies: 3
    Last Post: 4th May 2009, 12:19
  4. Problem with connect()
    By mrnor3 in forum Qt Programming
    Replies: 3
    Last Post: 23rd July 2008, 14:05
  5. Qfiledialog Problem - (Beginner)
    By kingslee in forum Qt Tools
    Replies: 3
    Last Post: 11th October 2006, 23: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
  •  
Qt is a trademark of The Qt Company.