Results 1 to 5 of 5

Thread: How to access TextEditBox of one class in another class in another file

  1. #1
    Join Date
    Sep 2012
    Posts
    2
    Qt products
    Qt3

    Default How to access TextEditBox of one class in another class in another file

    Hii Friends.

    I am developing a virtualization based GUI.

    In this application I am building a serial port GUI app.

    In my application I am having to .cpp files namely serial_port.cpp and CVmThread.cpp
    My serial_port.cpp is having a ui form having following files.
    serial_port.cpp
    serial_port.h
    serial_port.ui

    But CVmThread.cpp is a non-GUI form having following files.
    CVmTread.cpp
    CVmThread.h

    The code which I am using is given below
    serial_port.h
    Qt Code:
    1. #ifndef SERIAL_PORT_H
    2. #define SERIAL_PORT_H
    3.  
    4. #include <QWidget>
    5. #include <QTextEdit>
    6.  
    7.  
    8. namespace Ui {
    9. class Serial_Port;
    10. }
    11.  
    12. class Serial_Port : public QWidget
    13. {
    14. Q_OBJECT
    15.  
    16.  
    17. public:
    18. explicit Serial_Port(QWidget *parent = 0);
    19. ~Serial_Port();
    20.  
    21. QTextEdit* RxtextEdit;
    22.  
    23. private:
    24. Ui::Serial_Port *ui;
    25. };
    26.  
    27. #endif // SERIAL_PORT_H
    To copy to clipboard, switch view to plain text mode 

    serial_port.cpp
    Qt Code:
    1. #include "serial_port.h"
    2. #include "ui_serial_port.h"
    3.  
    4. Serial_Port::Serial_Port(QWidget *parent) :
    5. QWidget(parent),
    6. ui(new Ui::Serial_Port)
    7. {
    8. ui->setupUi(this);
    9. }
    10.  
    11. Serial_Port::~Serial_Port()
    12. {
    13. delete ui;
    14. }
    To copy to clipboard, switch view to plain text mode 

    CVmThread.h
    Qt Code:
    1. #ifndef __CVMTHREAD_H
    2. #define __CVMTHREAD_H
    3.  
    4. #include <qapplication.h>
    5. #include <QWidget>
    6. #include <qthread.h>
    7. #include "serial_port.h"
    8. #include "ui_serial_port.h"
    9.  
    10. #if defined(QT_NO_THREAD)
    11. # error Thread support not enabled.
    12. #endif
    13.  
    14. class CVmThread : public QThread
    15. {
    16. public:
    17.  
    18. CVmThread(QWidget *theThWidget);
    19. Serial_Port *theser;
    20. ~CVmThread();
    21.  
    22. void RecDataformClientPort();
    23. void run();
    24. };
    25. #endif
    To copy to clipboard, switch view to plain text mode 

    CVmThread.cpp
    Qt Code:
    1. #include "CVmThread.h"
    2. CVmThread::CVmThread(QWidget*theThWidget):ThreadWidgetReciver(theThWidget),bThreadStopped(FALSE)
    3. {
    4. SocPort.nComfd=thetvm.ComportOpen(0);
    5. qDebug("Comport:%d",SocPort.nComfd);
    6. }
    7. CVmThread::~ CVmThread()
    8. {
    9.  
    10. }
    11.  
    12. void CVmThread::run()
    13. {
    14. RecDataformClientPort();
    15. }
    16.  
    17. void CVmThread::RecDataformClientPort()
    18. {
    19. theser->RxtextEdit->setplaintext("TEXT");
    20. }
    To copy to clipboard, switch view to plain text mode 

    My question is that in serial_port.ui I am having a TextEditBox which I need to access in CVmThread.cpp file.
    I need to print some predefined text.
    I am creating an object of serial_port class and calling it in CVmThread.cpp but am unable to access the TextEditBox.

    Please do help me in this regard.

  2. #2
    Join Date
    Feb 2013
    Posts
    71
    Thanks
    6
    Thanked 3 Times in 3 Posts
    Platforms
    Windows

    Default Re: How to access TextEditBox of one class in another class in another file

    why dont you include the header of the seriel port on the thread c++ file

  3. #3
    Join Date
    Sep 2012
    Posts
    2
    Qt products
    Qt3

    Default Re: How to access TextEditBox of one class in another class in another file

    I have included the header file of serial_port.h in CVmThread.cpp
    Am able to execute the statement to print text in TextEditBox
    But still am unable to print data in the TextEditBox

  4. #4
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: How to access TextEditBox of one class in another class in another file

    You cannot access GUI objects (QTextEdit) from another thread (QThread::run()).

    Instead send signal from the Serial_Port object and receive it from a slot of CVmThread.
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

  5. #5
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to access TextEditBox of one class in another class in another file

    Quote Originally Posted by Santosh Reddy View Post
    You cannot access GUI objects (QTextEdit) from another thread (QThread::run()).

    Instead send signal from the Serial_Port object and receive it from a slot of CVmThread.
    Or the other way around, depending on which of the two classes produces the data and which one consumes it.

    So instead of calling setPlainText() on the text edit you would emit a signal carrying a QString and connect that to the setPlainText() slot or to a slot that in turn calls setPlainText()

    Cheers,
    _

    P.S.: even if it weren't for the threading issue, accessing some other classes internal members is almost always a bad idea. Methods in Serial_Port that then all methods if Serial_Port internals is way cleaner

Similar Threads

  1. How to access the UI from another class ?
    By fitzy in forum Qt Programming
    Replies: 22
    Last Post: 20th July 2016, 14:21
  2. Access from other class
    By champ in forum Newbie
    Replies: 2
    Last Post: 12th June 2010, 16:04
  3. Replies: 4
    Last Post: 29th May 2010, 12:56
  4. Replies: 3
    Last Post: 27th December 2008, 19:34
  5. Replies: 5
    Last Post: 14th July 2006, 22:42

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.