Results 1 to 6 of 6

Thread: newby c++ & Qt : connect

Threaded View

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

    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

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.