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 :
this->parent()->getData
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
//myData.h
//---------------------------------------------------
#ifndef MYDATA_H
#define MYDATA_H
#include <QObject>
#include <QString>
{
Q_OBJECT
public:
public slots:
QString* getData
() const{return datastring;
} private:
};
#endif // MYDATA_H
//myData.cpp
//---------------------------------------------------
#include "mydata.h"
{
}
{
datastring = s;
}
// myMessage.h
//---------------------------------------------------
#ifndef MYMESSAGE_H
#define MYMESSAGE_H
#include <QObject>
#include <QMessageBox>
{
Q_OBJECT
public:
myMessage
(QObject *parent
= 0);
//, QString *str = new QString("default"));public slots:
void showMesssage();
private:
};
#endif // MYMESSAGE_H
//myMessage.cpp
//---------------------------------------------------
#include "mymessage.h"
myMessage
::myMessage(QObject *parent
):{
message->setText("i got this Text by the Constructor");
}
void myMessage::showMesssage()
{
message->show();
}
void myMessage
::showMesssage(QString *s
) {
message->setText(*s);
message->show();
}
void myMessage
::newMeesageText(QString *s
) {
message->setText(*s);
}
//MainWindow.h
//---------------------------------------------------
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QLineEdit>
#include <QGroupBox>
#include <QHBoxLayout>
#include <mymessage.h>
#include "mydata.h"
{
Q_OBJECT
public slots:
public:
private:
myMessage *MBox;
myData *data;
private slots:
void theThingIWantToDo();
};
#endif // MAINWINDOW_H
//MainWindow.cpp
//---------------------------------------------------
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow
::MainWindow(QWidget *parent
) :{
data = new myData(this);
hlay->addWidget(line);
box->setLayout(hlay);
setCentralWidget(box);
MBox = new myMessage(this);
connect(line,SIGNAL(returnPressed()),
MBox,SLOT(showMesssage()));
connect(line,SIGNAL(returnPressed()),
this,SLOT(theThingIWantToDo()));
}
void MainWindow::theThingIWantToDo()// but not from here since this seems to be to complicated
{
*s = line->text();
MBox->newMeesageText(s);
}
//myData.h
//---------------------------------------------------
#ifndef MYDATA_H
#define MYDATA_H
#include <QObject>
#include <QString>
class myData : public QObject
{
Q_OBJECT
public:
myData(QObject *parent = 0);
public slots:
void setData(QString*);
QString* getData() const{return datastring;}
private:
QString *datastring;
};
#endif // MYDATA_H
//myData.cpp
//---------------------------------------------------
#include "mydata.h"
myData::myData(QObject *parent) :
QObject(parent)
{
datastring = new QString;
}
void myData::setData(QString *s)
{
datastring = s;
}
// myMessage.h
//---------------------------------------------------
#ifndef MYMESSAGE_H
#define MYMESSAGE_H
#include <QObject>
#include <QMessageBox>
class myMessage : public QObject
{
Q_OBJECT
public:
myMessage(QObject *parent = 0);//, QString *str = new QString("default"));
public slots:
void showMesssage();
void showMesssage(QString *s);
void newMeesageText(QString *s);
private:
QMessageBox *message;
};
#endif // MYMESSAGE_H
//myMessage.cpp
//---------------------------------------------------
#include "mymessage.h"
myMessage::myMessage(QObject *parent):
QObject(parent)
{
message = new QMessageBox();
message->setText("i got this Text by the Constructor");
}
void myMessage::showMesssage()
{
message->show();
}
void myMessage::showMesssage(QString *s)
{
message->setText(*s);
message->show();
}
void myMessage::newMeesageText(QString *s)
{
message->setText(*s);
}
//MainWindow.h
//---------------------------------------------------
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QLineEdit>
#include <QGroupBox>
#include <QHBoxLayout>
#include <mymessage.h>
#include "mydata.h"
class MainWindow : public QMainWindow
{
Q_OBJECT
public slots:
public:
MainWindow(QWidget *parent = 0);
private:
QLineEdit *line;
myMessage *MBox;
myData *data;
private slots:
void theThingIWantToDo();
};
#endif // MAINWINDOW_H
//MainWindow.cpp
//---------------------------------------------------
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent)
{
data = new myData(this);
line = new QLineEdit;
QHBoxLayout *hlay = new QHBoxLayout;
hlay->addWidget(line);
QGroupBox *box = new QGroupBox("Groupox");
box->setLayout(hlay);
setCentralWidget(box);
MBox = new myMessage(this);
connect(line,SIGNAL(returnPressed()),
MBox,SLOT(showMesssage()));
connect(line,SIGNAL(returnPressed()),
this,SLOT(theThingIWantToDo()));
}
void MainWindow::theThingIWantToDo()// but not from here since this seems to be to complicated
{
QString *s = new QString;
*s = line->text();
MBox->newMeesageText(s);
}
To copy to clipboard, switch view to plain text mode
Bookmarks