Results 1 to 2 of 2

Thread: How to Use Objects

  1. #1
    Join Date
    Jan 2006
    Posts
    273
    Thanks
    42
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default How to Use Objects

    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
    Qt Code:
    1. //Vorlage für Dialoge
    2.  
    3. //Klassen:
    4. #include "mydialog.h"
    5. #include <QApplication>
    6.  
    7.  
    8. int main(int argc, char *argv[])
    9. {
    10. QApplication app(argc, argv);
    11. MyDialog mydialog;
    12.  
    13. mydialog.show();
    14. return app.exec();
    15. }
    To copy to clipboard, switch view to plain text mode 

    mydialog.h
    Qt Code:
    1. #include "ui_dialog.h"
    2.  
    3.  
    4. //My Dialog ist eine Vererbte Klasse von UI_Dialog
    5. class MyDialog : public QDialog
    6. {
    7. Q_OBJECT
    8.  
    9. public:
    10. MyDialog(QWidget *parent = 0);
    11. Ui::Dialog ui;
    12.  
    13. //Funktionen
    14. public slots:
    15. void showMessage();
    16. void showDialogSuchen();
    17.  
    18.  
    19. private:
    20. //Ui::<Name des Objekts, siehe Designer> ui
    21.  
    22.  
    23. };
    To copy to clipboard, switch view to plain text mode 

    mydialog.cpp
    Qt Code:
    1. //Klassen:
    2. #include "mydialog.h"
    3. #include "dialogsuchen.h"
    4. #include <QMessageBox>
    5.  
    6. //Konstruktor
    7. MyDialog::MyDialog(QWidget *parent)
    8. : QDialog(parent)
    9. {
    10. ui.setupUi(this);
    11.  
    12. connect(ui.okButton, SIGNAL(clicked()), this, SLOT(showMessage()));
    13. connect(ui.cancelButton, SIGNAL(clicked()), this, SLOT(close()));
    14. connect(ui.openButton, SIGNAL(clicked()), this, SLOT(showDialogSuchen()));
    15. }
    16.  
    17. //Diese Funktion zeigt eine MessageBox an
    18. void MyDialog::showMessage()
    19. {
    20. QMessageBox::information(this, "Application name",
    21. "Hallo Welt\n"
    22. "Tutorial 1");
    23. }
    24. void MyDialog::showDialogSuchen()
    25. {
    26. DialogSuchen dialogsuchen(this);
    27. dialogsuchen.exec();
    28.  
    29. }
    To copy to clipboard, switch view to plain text mode 

    dialogsuchen.h
    Qt Code:
    1. #include "ui_dialogsuchen.h"
    2.  
    3.  
    4. //My Dialog ist eine Vererbte Klasse von UI_Dialog
    5. class DialogSuchen : public QDialog
    6. {
    7. Q_OBJECT
    8.  
    9. public:
    10. DialogSuchen(QWidget *parent = 0);
    11.  
    12. //Funktionen
    13. public slots:
    14. void showMessage();
    15. void sendString();
    16.  
    17.  
    18. private:
    19. //Ui::<Name des Objekts, siehe Designer> ui
    20. Ui::DialogSuchen ui;
    21.  
    22. };
    To copy to clipboard, switch view to plain text mode 

    dialogsuchen.cpp
    Qt Code:
    1. //Klassen:
    2. #include "dialogsuchen.h"
    3. #include "mydialog.h"
    4. #include <QMessageBox>
    5.  
    6. //Konstruktor
    7. DialogSuchen::DialogSuchen(QWidget *parent)
    8. : QDialog(parent)
    9. {
    10. ui.setupUi(this);
    11.  
    12. connect(ui.okButton, SIGNAL(clicked()), this, SLOT(showMessage()));
    13. connect(ui.cancelButton, SIGNAL(clicked()), this, SLOT(close()));
    14. connect(ui.sendButton, SIGNAL(clicked()), this, SLOT(sendString()));
    15.  
    16. }
    17.  
    18. //Diese Funktion zeigt eine MessageBox an
    19. void DialogSuchen::showMessage()
    20. {
    21. QMessageBox::information(this, "Application name",
    22. "Hallo Welt\n"
    23. "Tutorial 1");
    24. }
    25. void DialogSuchen::sendString()
    26. {
    27. MyDialog *mydialog = qobject_cast<MyDialog*>(parent());
    28. QString text = ui.lineEdit->text();
    29.  
    30. mydialog->ui.textEdit->setPlainText(text);
    31.  
    32.  
    33. }
    To copy to clipboard, switch view to plain text mode 
    Think DigitalGasoline

  2. #2
    Join Date
    Jan 2006
    Posts
    667
    Thanks
    10
    Thanked 80 Times in 74 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to Use Objects

    I think it should be something like

    Qt Code:
    1. void MyDialog::showDialogSuchen()
    2. {
    3. DialogSuchen dialogsuchen(this);
    4. connect(&dialogsuchen,SIGNAL(sendString(QString)), this, SLOT(onSendString(QString)));
    5. dialogsuchen.exec();
    6. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. void DialogSuchen::onSendString()
    2. {
    3. emit sendString(ui.lineEdit->text());
    4. }
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. accessing form1's objects from form2
    By Philip_Anselmo in forum Newbie
    Replies: 5
    Last Post: 4th May 2006, 22:54
  2. Using DirectShow and COM objects
    By ToddAtWSU in forum Qt Programming
    Replies: 5
    Last Post: 27th February 2006, 15:35
  3. Help me to use QtSharedMemory to share the data objects
    By gtthang in forum Qt Programming
    Replies: 3
    Last Post: 17th February 2006, 11:50
  4. Objects and members
    By mickey in forum General Programming
    Replies: 8
    Last Post: 1st February 2006, 00:13
  5. Using K Objects in Qt project
    By dec0ding in forum Qt Programming
    Replies: 8
    Last Post: 17th January 2006, 18:44

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.