Results 1 to 6 of 6

Thread: newby c++ & Qt : connect

  1. #1
    Join Date
    Apr 2013
    Location
    luxemburg
    Posts
    10
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default newby c++ & Qt : connect

    Hello,
    im new to c++ and Qt so i hope the question isn't too silly. i'm trying to understand the Signal Slot mechanism of Qt so i wrote a little programm which does what i wat it to, but it seems to be a bit too complicatetd:
    what i wat do to is:
    i have tree classes:
    - MainWindow
    -MyData
    -myMessageBox

    Main window has an instance of the oter two. the messageBox shall be representive for any class that does makes a reaction to a slot of aany other class.
    Q1.) can i connect a signal in myData with a Slot in myMessageBox without passing through MainWindow? the only thing they got in common is the same parent.
    Q2.) how do i get access from myMessageBox to myData. i tried something like :
    Qt Code:
    1. this->parent()->getData
    To copy to clipboard, switch view to plain text mode 
    but it doesnt work.
    myData has a public Slot that should return a pointer to the instance
    here comes an example. sorry that its a bit long i tried to keep it short. I added the program code in a zip. it shoul run without any problems

    thanks in advance

    Qt Code:
    1. //myData.h
    2. //---------------------------------------------------
    3. #ifndef MYDATA_H
    4. #define MYDATA_H
    5.  
    6. #include <QObject>
    7. #include <QString>
    8.  
    9. class myData : public QObject
    10. {
    11. Q_OBJECT
    12. public:
    13. myData(QObject *parent = 0);
    14. public slots:
    15. void setData(QString*);
    16. QString* getData() const{return datastring;}
    17. private:
    18. QString *datastring;
    19. };
    20.  
    21. #endif // MYDATA_H
    22.  
    23. //myData.cpp
    24. //---------------------------------------------------
    25. #include "mydata.h"
    26.  
    27. myData::myData(QObject *parent) :
    28. QObject(parent)
    29. {
    30. datastring = new QString;
    31. }
    32. void myData::setData(QString *s)
    33. {
    34. datastring = s;
    35. }
    36.  
    37. // myMessage.h
    38. //---------------------------------------------------
    39. #ifndef MYMESSAGE_H
    40. #define MYMESSAGE_H
    41.  
    42. #include <QObject>
    43. #include <QMessageBox>
    44.  
    45. class myMessage : public QObject
    46. {
    47. Q_OBJECT
    48. public:
    49. myMessage(QObject *parent = 0);//, QString *str = new QString("default"));
    50. public slots:
    51. void showMesssage();
    52. void showMesssage(QString *s);
    53. void newMeesageText(QString *s);
    54. private:
    55. QMessageBox *message;
    56. };
    57. #endif // MYMESSAGE_H
    58.  
    59. //myMessage.cpp
    60. //---------------------------------------------------
    61. #include "mymessage.h"
    62. myMessage::myMessage(QObject *parent):
    63. QObject(parent)
    64. {
    65. message = new QMessageBox();
    66. message->setText("i got this Text by the Constructor");
    67. }
    68. void myMessage::showMesssage()
    69. {
    70. message->show();
    71. }
    72. void myMessage::showMesssage(QString *s)
    73. {
    74. message->setText(*s);
    75. message->show();
    76. }
    77. void myMessage::newMeesageText(QString *s)
    78. {
    79. message->setText(*s);
    80. }
    81.  
    82. //MainWindow.h
    83. //---------------------------------------------------
    84. #ifndef MAINWINDOW_H
    85. #define MAINWINDOW_H
    86.  
    87. #include <QMainWindow>
    88. #include <QLineEdit>
    89. #include <QGroupBox>
    90. #include <QHBoxLayout>
    91. #include <mymessage.h>
    92.  
    93. #include "mydata.h"
    94. class MainWindow : public QMainWindow
    95. {
    96. Q_OBJECT
    97. public slots:
    98. public:
    99. MainWindow(QWidget *parent = 0);
    100. private:
    101. QLineEdit *line;
    102. myMessage *MBox;
    103. myData *data;
    104. private slots:
    105. void theThingIWantToDo();
    106. };
    107.  
    108. #endif // MAINWINDOW_H
    109.  
    110. //MainWindow.cpp
    111. //---------------------------------------------------
    112. #include "mainwindow.h"
    113. #include "ui_mainwindow.h"
    114.  
    115. MainWindow::MainWindow(QWidget *parent) :
    116. QMainWindow(parent)
    117. {
    118. data = new myData(this);
    119.  
    120. line = new QLineEdit;
    121. QHBoxLayout *hlay = new QHBoxLayout;
    122. hlay->addWidget(line);
    123. QGroupBox *box = new QGroupBox("Groupox");
    124. box->setLayout(hlay);
    125. setCentralWidget(box);
    126. MBox = new myMessage(this);
    127. connect(line,SIGNAL(returnPressed()),
    128. MBox,SLOT(showMesssage()));
    129. connect(line,SIGNAL(returnPressed()),
    130. this,SLOT(theThingIWantToDo()));
    131. }
    132. void MainWindow::theThingIWantToDo()// but not from here since this seems to be to complicated
    133. {
    134. QString *s = new QString;
    135. *s = line->text();
    136. MBox->newMeesageText(s);
    137. }
    To copy to clipboard, switch view to plain text mode 
    Attached Files Attached Files

  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: newby c++ & Qt : connect

    Q1.) can i connect a signal in myData with a Slot in myMessageBox without passing through MainWindow? the only thing they got in common is the same parent.
    Connection between a SIGNAL of myData and SLOT of myMessageBox better be made in MainWindow.

    Q2.) how do i get access from myMessageBox to myData. i tried something like :
    Don't do this, myData should not access myMessageBox, this is the whole point of SIGNAL and SLOTS, objects need not know each other.


    BTW just a small correction in code
    Qt Code:
    1. void myDataClass::setData(QString *s)
    2. {
    3. if(str)
    4. delete str; //<<<<<<<<<<<<<<<<<<<<<
    5. str = s;
    6. emit DataChanged();
    7. }
    To copy to clipboard, switch view to plain text mode 
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

  3. #3
    Join Date
    Apr 2013
    Location
    luxemburg
    Posts
    10
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: newby c++ & Qt : connect

    thanks for your fast reply and the correction.

    so all the connects shall be mad in the MainWindow ( in my case) ??
    on the one hand i have a gui that is fed by the user and everything he/she enters shall be written in my data.
    how i woud do it for now:
    the Gui widget emits a signal which will be cought by the main window by the corresponding public slot. this slot is then connected o the main windows funktion that calls the guiWidget with a pointer to the dataObject. this seems really complicated if you have a lot of GuiWidgets; or what i don't undestand?

  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: newby c++ & Qt : connect

    ...this slot is then connected o the main windows funktion that calls the guiWidget with a pointer to the dataObject.
    This is not a good idea. Instead pass the value in the signal as a parameter.
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

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

    pipapongo (10th April 2013)

  6. #5
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: newby c++ & Qt : connect

    Not entirely related, but don't use QString pointers. QStrings can easily and cheaply be copied around, raw pointers tend to leak.

    Cheers,
    _

  7. #6
    Join Date
    Aug 2009
    Location
    coimbatore,India
    Posts
    314
    Thanks
    37
    Thanked 47 Times in 43 Posts
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: newby c++ & Qt : connect

    hi,
    i'm trying to understand the Signal Slot mechanism of Qt
    Create a simple pushbutton and try to connect with a slot.
    To understand signals, you shall subclass QPushButton and add your own signal.

    ex:
    Qt Code:
    1. slot:
    2. class myClass
    3. {
    4. ..............
    5. ................
    6. SLOTS:
    7. void clickSlot();
    8. }
    9.  
    10. ........................
    11. connect(btn,clicked(clicked()),this,SLOT(clickSlot()));
    12. ........................
    To copy to clipboard, switch view to plain text mode 

    hope it helps,
    Bala

Similar Threads

  1. Replies: 2
    Last Post: 9th May 2011, 10:38
  2. Replies: 16
    Last Post: 16th February 2010, 13:17
  3. cant open file newby problem
    By lmguru in forum Qt Programming
    Replies: 3
    Last Post: 27th January 2009, 14:53
  4. Replies: 4
    Last Post: 10th November 2006, 15:38
  5. connect
    By mickey in forum Qt Programming
    Replies: 2
    Last Post: 27th May 2006, 09:45

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.