PDA

View Full Version : newby c++ & Qt : connect



pipapongo
10th April 2013, 07:56
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 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>

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);
}

Santosh Reddy
10th April 2013, 08:21
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


void myDataClass::setData(QString *s)
{
if(str)
delete str; //<<<<<<<<<<<<<<<<<<<<<
str = s;
emit DataChanged();
}

pipapongo
10th April 2013, 08:54
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?

Santosh Reddy
10th April 2013, 09:02
...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.

anda_skoa
11th April 2013, 10:05
Not entirely related, but don't use QString pointers. QStrings can easily and cheaply be copied around, raw pointers tend to leak.

Cheers,
_

BalaQT
11th April 2013, 13:02
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:


slot:
class myClass
{
QPushButton *btn;
..............
................
SLOTS:
void clickSlot();
}

........................
connect(btn,clicked(clicked()),this,SLOT(clickSlot ()));
........................


hope it helps,
Bala