PDA

View Full Version : How to Use Objects



raphaelf
2nd June 2006, 08:58
Hi everybody..
I have for examples 2 classes (2 Dialogs). My example send a text from a dialog to the other one. It Works.
But my Question is if my way are correct? Can somebody show me how the professional way works and what is not ok on my example?
my function sendString() are sending a text to the other dialog..

Thanks forwading

main.cpp


//Vorlage für Dialoge

//Klassen:
#include "mydialog.h"
#include <QApplication>


int main(int argc, char *argv[])
{
QApplication app(argc, argv);
MyDialog mydialog;

mydialog.show();
return app.exec();
}


mydialog.h


#include "ui_dialog.h"


//My Dialog ist eine Vererbte Klasse von UI_Dialog
class MyDialog : public QDialog
{
Q_OBJECT

public:
MyDialog(QWidget *parent = 0);
Ui::Dialog ui;

//Funktionen
public slots:
void showMessage();
void showDialogSuchen();


private:
//Ui::<Name des Objekts, siehe Designer> ui


};


mydialog.cpp


//Klassen:
#include "mydialog.h"
#include "dialogsuchen.h"
#include <QMessageBox>

//Konstruktor
MyDialog::MyDialog(QWidget *parent)
: QDialog(parent)
{
ui.setupUi(this);

connect(ui.okButton, SIGNAL(clicked()), this, SLOT(showMessage()));
connect(ui.cancelButton, SIGNAL(clicked()), this, SLOT(close()));
connect(ui.openButton, SIGNAL(clicked()), this, SLOT(showDialogSuchen()));
}

//Diese Funktion zeigt eine MessageBox an
void MyDialog::showMessage()
{
QMessageBox::information(this, "Application name",
"Hallo Welt\n"
"Tutorial 1");
}
void MyDialog::showDialogSuchen()
{
DialogSuchen dialogsuchen(this);
dialogsuchen.exec();

}


dialogsuchen.h


#include "ui_dialogsuchen.h"


//My Dialog ist eine Vererbte Klasse von UI_Dialog
class DialogSuchen : public QDialog
{
Q_OBJECT

public:
DialogSuchen(QWidget *parent = 0);

//Funktionen
public slots:
void showMessage();
void sendString();


private:
//Ui::<Name des Objekts, siehe Designer> ui
Ui::DialogSuchen ui;

};


dialogsuchen.cpp


//Klassen:
#include "dialogsuchen.h"
#include "mydialog.h"
#include <QMessageBox>

//Konstruktor
DialogSuchen::DialogSuchen(QWidget *parent)
: QDialog(parent)
{
ui.setupUi(this);

connect(ui.okButton, SIGNAL(clicked()), this, SLOT(showMessage()));
connect(ui.cancelButton, SIGNAL(clicked()), this, SLOT(close()));
connect(ui.sendButton, SIGNAL(clicked()), this, SLOT(sendString()));

}

//Diese Funktion zeigt eine MessageBox an
void DialogSuchen::showMessage()
{
QMessageBox::information(this, "Application name",
"Hallo Welt\n"
"Tutorial 1");
}
void DialogSuchen::sendString()
{
MyDialog *mydialog = qobject_cast<MyDialog*>(parent());
QString text = ui.lineEdit->text();

mydialog->ui.textEdit->setPlainText(text);


}

munna
2nd June 2006, 09:59
I think it should be something like




void MyDialog::showDialogSuchen()
{
DialogSuchen dialogsuchen(this);
connect(&dialogsuchen,SIGNAL(sendString(QString)), this, SLOT(onSendString(QString)));
dialogsuchen.exec();
}






void DialogSuchen::onSendString()
{
emit sendString(ui.lineEdit->text());
}