Results 1 to 11 of 11

Thread: making a class aware of the gui

  1. #1
    Join Date
    Feb 2007
    Posts
    42
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default making a class aware of the gui

    hi,

    i have a form design that i implement using the multiple inheritance approach, so i have a class {i name it gui} that represents my form

    Qt Code:
    1. class gui : public QDialog, private Ui::mainFormGUI
    2. {
    3. Q_OBJECT
    4.  
    5. //the constructor
    6. public:
    7. gui (QWidget *parent = 0);
    8.  
    9. //the buttons of the functions....
    10. private slots:
    11.  
    12. private:
    13. typeA test;
    14. };
    To copy to clipboard, switch view to plain text mode 

    the problem is that i want to make typeA access the gui interface....{for example write on a QTextEdit..... How can i do this?

    i thought that i could pass to the typeA class a {this}pointer of gui {i.e. gui* p_gui} and then access the QTextEdit from p_gui but unfortunatelly it doesn't work....

    any other ideas?

    thanks in advance ,
    N.A

  2. #2
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: making a class aware of the gui

    Is the testA object in another thread. Because if it is, then you will need some synchronization.

    To access a QTextEdit from the interface, you could add public wrapper functions in the gui class for the QTextEdit members that you need ( because the text edit is private in the gui, you cannot access it from the testA object ).

    For example, for the setText method:

    void gui::setText( QString text )
    {
    mTextEdit->setText( text );
    }

    Another solution would be to change the inheritance from the UI from private to public.

    Hope this helps.

    Marcel.

  3. #3
    Join Date
    Mar 2006
    Location
    Mountain View, California
    Posts
    489
    Thanks
    3
    Thanked 74 Times in 54 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: making a class aware of the gui

    If I understand your question correctly, then make typeA a friend of class gui.

  4. #4
    Join Date
    Feb 2007
    Posts
    42
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Question Re: making a class aware of the gui

    thank you all for your answers
    unfortunatelly nothing worked....

    caseChangerClass.h
    Qt Code:
    1. #ifndef caseChangerClass_H
    2. #define caseChangerClass_H
    3.  
    4. #include "ui_caseChanger.h"
    5. #include "classA.h"
    6.  
    7. #include <QDialog>
    8. #include <QMenu>
    9.  
    10.  
    11. //class caseChangerClass : public QDialog, private Ui::mainFormCaseChanger
    12. class caseChangerClass : public QDialog, private Ui::mainFormCaseChanger
    13. {
    14. Q_OBJECT
    15.  
    16. //the constructor
    17. public:
    18. caseChangerClass(QWidget *parent = 0);
    19.  
    20.  
    21. private:
    22. typeA test;
    23.  
    24. public: void caseChangerClass::setText( QString text );
    25.  
    26. };
    27.  
    28. #endif
    To copy to clipboard, switch view to plain text mode 


    caseChangerClass.cpp
    Qt Code:
    1. #include "caseChangerClass.h"
    2.  
    3. #include <QtGui>
    4. #include <QString>
    5.  
    6. caseChangerClass::caseChangerClass(QWidget* parent):QDialog(parent),test(this)
    7. {
    8.  
    9. setupUi(this);
    10.  
    11.  
    12. }
    13.  
    14. void caseChangerClass::setText( QString text )
    15. {
    16.  
    17. textEdit->setText( text );
    18.  
    19. }
    To copy to clipboard, switch view to plain text mode 

    classA.h
    Qt Code:
    1. #ifndef classA_H
    2. #define classA_H
    3.  
    4. #include "caseChangerClass.h"
    5.  
    6. class caseChangerClass;
    7.  
    8. class typeA
    9. {
    10.  
    11.  
    12. //the constructor
    13. public:
    14. typeA(caseChangerClass* gui);
    15. ~typeA();
    16.  
    17. caseChangerClass* m_gui;
    18.  
    19. };
    20.  
    21. #endif
    To copy to clipboard, switch view to plain text mode 

    classA.cpp
    Qt Code:
    1. #include "classA.h"
    2.  
    3. typeA::typeA(caseChangerClass* gui);
    4. {
    5.  
    6. m_gui = gui;
    7.  
    8. //this is what i tryied to do earlier....
    9. //m_gui->textEdit->append("hi!");
    10.  
    11. gui->setText( "HI" );
    12.  
    13. }
    To copy to clipboard, switch view to plain text mode 

    main.cpp
    Qt Code:
    1. #include <QApplication>
    2.  
    3. #include "caseChangerClass.h"
    4.  
    5. int main(int argc, char *argv[])
    6. {
    7. QApplication app(argc, argv);
    8. caseChangerClass myApp;
    9. myApp.setWindowFlags(Qt::WindowStaysOnTopHint);
    10. myApp.show();
    11. return app.exec();
    12. }
    To copy to clipboard, switch view to plain text mode 

    ui_caseChanger.h
    Qt Code:
    1. /********************************************************************************
    2. ** Form generated from reading ui file 'caseChanger.ui'
    3. **
    4. ** Created: Mon 2. Apr 20:23:18 2007
    5. ** by: Qt User Interface Compiler version 4.2.2
    6. **
    7. ** WARNING! All changes made in this file will be lost when recompiling ui file!
    8. ********************************************************************************/
    9.  
    10. #ifndef UI_CASECHANGER_H
    11. #define UI_CASECHANGER_H
    12.  
    13. #include <QtCore/QVariant>
    14. #include <QtGui/QAction>
    15. #include <QtGui/QApplication>
    16. #include <QtGui/QButtonGroup>
    17. #include <QtGui/QTextEdit>
    18. #include <QtGui/QWidget>
    19.  
    20. class Ui_mainFormCaseChanger
    21. {
    22. public:
    23. QTextEdit *textEdit;
    24.  
    25. void setupUi(QWidget *mainFormCaseChanger)
    26. {
    27. mainFormCaseChanger->setObjectName(QString::fromUtf8("mainFormCaseChanger"));
    28. textEdit = new QTextEdit(mainFormCaseChanger);
    29. textEdit->setObjectName(QString::fromUtf8("textEdit"));
    30. textEdit->setGeometry(QRect(80, 20, 251, 171));
    31.  
    32. retranslateUi(mainFormCaseChanger);
    33.  
    34. QSize size(444, 207);
    35. size = size.expandedTo(mainFormCaseChanger->minimumSizeHint());
    36. mainFormCaseChanger->resize(size);
    37.  
    38.  
    39. QMetaObject::connectSlotsByName(mainFormCaseChanger);
    40. } // setupUi
    41.  
    42. void retranslateUi(QWidget *mainFormCaseChanger)
    43. {
    44. mainFormCaseChanger->setWindowTitle(QApplication::translate("mainFormCaseChanger", "test", 0, QApplication::UnicodeUTF8));
    45. Q_UNUSED(mainFormCaseChanger);
    46. } // retranslateUi
    47.  
    48. };
    49.  
    50. namespace Ui {
    51. class mainFormCaseChanger: public Ui_mainFormCaseChanger {};
    52. } // namespace Ui
    53.  
    54. #endif // UI_CASECHANGER_H
    To copy to clipboard, switch view to plain text mode 

    why this doesn't work??
    Attached Files Attached Files
    Last edited by aegis; 2nd April 2007 at 18:35. Reason: updated contents

  5. #5
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: making a class aware of the gui

    I noticed typeA* test, in classChangerClass, is initialized in the constructor list. This cannot be a good thing because setupUi is called after you set the text in the text edit, in the constructor of typeA.

    Try constructing "test" after setupUi.

    Marcel

  6. #6
    Join Date
    Feb 2007
    Posts
    42
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: making a class aware of the gui

    unfortunatelly i cant setup test after because c++ will try to initialize it implicitly with a default constructor...and since i dont have a default constructor i will get an error message....

    any other ideas??

  7. #7
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: making a class aware of the gui

    Don't call setText in the constructor of testA!
    Make another method that calls setText, which will be called after setupUI. It's not really that hard to make this work.

  8. #8
    Join Date
    Feb 2007
    Posts
    42
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: making a class aware of the gui

    Quote Originally Posted by marcel View Post
    Don't call setText in the constructor of testA!
    Make another method that calls setText, which will be called after setupUI. It's not really that hard to make this work.
    i didn't understand correctly what you said, but i do now and i get the same error that typeA does not name a type...
    Last edited by aegis; 2nd April 2007 at 23:40. Reason: spelling error

  9. #9
    Join Date
    Feb 2007
    Posts
    42
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: making a class aware of the gui

    if in classA.h i remove the line4 {#include "caseChangerClass.h"}
    then it proceeds with compilation and i get this error:

    classA.cpp:9: error: invalid use of undefined type `struct caseChangerClass'

    any ideas...?

  10. #10
    Join Date
    Mar 2006
    Location
    Mountain View, California
    Posts
    489
    Thanks
    3
    Thanked 74 Times in 54 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: making a class aware of the gui

    caseChangerClass has a typeA member, but the typeA class takes a caseChangerClass in the constructor. Though it is permitted in C++, it is a sign of a bad design.

  11. #11
    Join Date
    Feb 2007
    Posts
    42
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: making a class aware of the gui

    Quote Originally Posted by Brandybuck View Post
    caseChangerClass has a typeA member, but the typeA class takes a caseChangerClass in the constructor. Though it is permitted in C++, it is a sign of a bad design.
    yes but i cant find any other way to make typeA now about the gui.....in particular about the textEdit field of the gui...

Similar Threads

  1. Replies: 2
    Last Post: 16th March 2007, 09:04
  2. ("Dynamic") Class in DLL
    By durbrak in forum Qt Programming
    Replies: 5
    Last Post: 29th January 2007, 14:43
  3. Replies: 13
    Last Post: 15th December 2006, 11:52
  4. Replies: 2
    Last Post: 4th May 2006, 19:17
  5. How to propagate from one class to another
    By mahe2310 in forum Qt Programming
    Replies: 15
    Last Post: 20th March 2006, 01:27

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.