Page 1 of 2 12 LastLast
Results 1 to 20 of 23

Thread: How to access the UI from another class ?

  1. #1
    Join Date
    Oct 2009
    Posts
    38
    Thanks
    13
    Platforms
    Unix/X11 Windows

    Default How to access the UI from another class ?

    Hi,

    I'd like to access the user interface from another class.

    If I call this in my main class "emulation", it works :
    Qt Code:
    1. ui->LOG->append("text");
    To copy to clipboard, switch view to plain text mode 

    But i want to do the same thing from another class "robot" :

    Qt Code:
    1. emulation::ui.LOG->append("text");
    To copy to clipboard, switch view to plain text mode 

    Unfortunately, it doesn't work !

    I really need your help !

  2. #2
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to access the UI from another class ?

    Typically, ui is private, so you can't access it from another class. You could make it public, but a better approach would be to add a public method to your gui class to log events and let that communicate with the ui.

  3. The following user says thank you to squidge for this useful post:

    fitzy (26th October 2009)

  4. #3
    Join Date
    Oct 2009
    Posts
    38
    Thanks
    13
    Platforms
    Unix/X11 Windows

    Default Re: How to access the UI from another class ?

    ty
    But i am a beginner and I don't really know how to call a function of my Main class from another class.

    Thanks in advance.

  5. #4
    Join Date
    Jan 2009
    Location
    Germany
    Posts
    131
    Thanks
    11
    Thanked 16 Times in 16 Posts
    Qt products
    Qt3 Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How to access the UI from another class ?

    Qt Code:
    1. void MyClass::method() {
    2.  
    3. AnotherClass* another_object = new AnotherClass();
    4. another_object->anotherMethod();
    5. }
    To copy to clipboard, switch view to plain text mode 

  6. The following user says thank you to nightghost for this useful post:

    fitzy (26th October 2009)

  7. #5
    Join Date
    Oct 2009
    Posts
    38
    Thanks
    13
    Platforms
    Unix/X11 Windows

    Default Re: How to access the UI from another class ?

    Actually it doesn't work

    My program runs but never shows up.

  8. #6
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: How to access the UI from another class ?

    Show the relevant code please.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  9. #7
    Join Date
    Oct 2009
    Posts
    38
    Thanks
    13
    Platforms
    Unix/X11 Windows

    Default Re: How to access the UI from another class ?

    robotShooter.cpp :

    Qt Code:
    1. class koala
    2. {
    3.  
    4. public:
    5. void log1(){
    6. robotShooter* another_object = new robotShooter();
    7. another_object->LOG("text");
    8. }
    9.  
    10. };
    11.  
    12. void robotShooter::LOG(QString text){
    13. ui->LOG->append(text);
    14. }
    15.  
    16. robotShooter::robotShooter(QWidget *parent)
    17. : QMainWindow(parent), ui(new Ui::robotShooter)
    18. {
    19. ui->setupUi(this);
    20.  
    21. koala *KO = new koala();
    22. KO->log1();
    23.  
    24. }
    To copy to clipboard, switch view to plain text mode 


    robotShooter.h

    Qt Code:
    1. #ifndef ROBOTSHOOTER_H
    2. #define ROBOTSHOOTER_H
    3.  
    4. #include <QtGui/QMainWindow>
    5.  
    6. namespace Ui
    7. {
    8. class robotShooter;
    9. }
    10.  
    11. class robotShooter : public QMainWindow
    12. {
    13. Q_OBJECT
    14.  
    15. public:
    16. robotShooter(QWidget *parent = 0);
    17. ~robotShooter();
    18.  
    19. void LOG(QString);
    20.  
    21. //private:
    22. Ui::robotShooter *ui;
    23. };
    24.  
    25. #endif // ROBOTSHOOTER_H
    To copy to clipboard, switch view to plain text mode 
    Last edited by fitzy; 26th October 2009 at 16:25.

  10. #8
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: How to access the UI from another class ?

    Amm... what it is you are trying to do?

    Your code is recursive:
    your robotShooter is creating a koala object, which in turn the only thing it does is create a new robotShooter (and sends a string to LOG()), and the cycle goes on...
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  11. #9
    Join Date
    Oct 2009
    Posts
    38
    Thanks
    13
    Platforms
    Unix/X11 Windows

    Default Re: How to access the UI from another class ?

    Quote Originally Posted by high_flyer View Post
    Amm... what it is you are trying to do?

    Your code is recursive:
    your robotShooter is creating a koala object, which in turn the only thing it does is create a new robotShooter (and sends a string to LOG()), and the cycle goes on...
    Well, i'm trying to add some text in a "text browser".

    fatjuicymole told me :
    add a public method to your gui class to log events and let that communicate with the ui.
    that's what I'm trying to do.

  12. #10
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to access the UI from another class ?

    Qt Code:
    1. class CA {
    2. public:
    3. void log (QString *str);
    4. };
    5.  
    6. class CB {
    7. public:
    8. void log (QString *str);
    9. };
    10.  
    11. CA firstclass;
    12. CB secondclass;
    13.  
    14. void CA::log(QString *str) {
    15. secondclass.log("foo");
    16. }
    17.  
    18. void CB::log(QString *str) {
    19. ui->LOG->append(str);
    20. }
    To copy to clipboard, switch view to plain text mode 

    You can also have firstclass and secondclass as pointers by using new to initialise them and use -> instead of '.'
    Last edited by squidge; 26th October 2009 at 20:28.

  13. The following user says thank you to squidge for this useful post:

    fitzy (26th October 2009)

  14. #11
    Join Date
    Oct 2009
    Posts
    38
    Thanks
    13
    Platforms
    Unix/X11 Windows

    Default Re: How to access the UI from another class ?

    I don't get it.

    I have pasted your example (by the way, I think you meant CA::log(QString *str) not A::log(QString *str) ) but it doesn't work.

    There are many errors :

    Qt Code:
    1. ISO C++ forbids declaration of `log' with no type
    2. CA::log(QString*)' does not match any in class `CA'
    3. ISO C++ forbids declaration of `log' with no type
    4. etc
    To copy to clipboard, switch view to plain text mode 

  15. #12
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: How to access the UI from another class ?

    Quote Originally Posted by fitzy View Post
    fatjuicymole told me :
    I will tell you this: get at least a glimpse of knowledge of C++ before doing more advaced things. Looking up "Thinking in C++" is a good place to start.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  16. #13
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to access the UI from another class ?

    Apologies, thats what you get for rush typing, but you get the idea.

    If you don't understand it you need to buy a book on C++ programming. You really need to understand C++ and OOP before working with Qt.

  17. The following user says thank you to squidge for this useful post:

    fitzy (27th October 2009)

  18. #14
    Join Date
    Oct 2015
    Posts
    3
    Qt products
    Qt5
    Platforms
    Unix/X11 Android

    Default Re: How to access the UI from another class ?

    function_in_class_A()
    {
    Class_B_Object.function_in_class_B(*ui->QLineEdit); // send qwidget to other class
    }

    function_in_class_B(QLineEdit &some_name)
    {
    some_name.setText("string_value"); // acces to qwidget in class B
    }

  19. #15
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: How to access the UI from another class ?

    Geez, not only do you bring back from the dead a 6+ years-old post, you post an absolutely terrible solution that is completely counter to what Qt signals and slots are all about.

    You should never, ever implement code like this in a Qt app.

    If you have two classes where one of them needs to send information that updates the UI on the other, then you do it this way:

    1 - Implement a slot on the UI class:

    Qt Code:
    1. void MyUIClass::updateText( const QString & newText )
    2. {
    3. ui->textEdit->setText( newText );
    4. }
    To copy to clipboard, switch view to plain text mode 

    2 - Implement a signal in the class that needs to send the text to the UI class:

    Qt Code:
    1. signals:
    2. void sendText( const QString & newText );
    To copy to clipboard, switch view to plain text mode 

    3 - Connect the signal to the slot:

    Qt Code:
    1. connect( myClass, SIGNAL( sendText( const QString & ) ), myUIClass, SLOT( updateText( const QString & ) ) );
    To copy to clipboard, switch view to plain text mode 

    4 - and every time "myClass" needs to display a new message on the UI class, it simply calls:

    Qt Code:
    1. emit sendText( "Here is new text" );
    To copy to clipboard, switch view to plain text mode 

  20. The following user says thank you to d_stranz for this useful post:

    anda_skoa (31st January 2016)

  21. #16
    Join Date
    Jul 2016
    Posts
    4
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Windows Android

    Default Re: How to access the UI from another class ?

    d_stranz

    I really appreciated your explanations in this thread but have few questions related:

    How could you use a Class (Myclass) to initiate the signal as follows?

    connect( myClass, SIGNAL( sendText( const QString & ) ), myUIClass, SLOT( updateText( const QString & ) ) );

    In which QT program did you insert this Qt code (connect)?

    Thks in advance,

  22. #17
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: How to access the UI from another class ?

    If you don't understand something as basic to Qt as how to implement signals and slots, then you should spend some time studying the Qt documentation on them, and look at some of the QWidget examples, like the Calculator example.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  23. #18
    Join Date
    Jul 2016
    Posts
    4
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Windows Android

    Default Re: How to access the UI from another class ?

    I do not have problem with signals and slots which I used quite often in the Mainwindow in my apps.
    My concern is how to generate signals and slots between different classes.

    I figured out that 'myClass' in your connect example is actually an object but can't find what 'myUIClass' refers to in your example, I tried MainWindow but did not work.

    I wrote the connect function in the secondary class as follows:

    QObject::connect(objetA, SIGNAL(SendPlot()),MainWindow, SLOT(addPlot()) );

    but MainWindow is not recognized despite the following includes:

    #include "ui_mainwindow.h"
    #include "mainwindow.h"

    Your help appreciated !

  24. #19
    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 the UI from another class ?

    Quote Originally Posted by jps3001 View Post
    In which QT program did you insert this Qt code (connect)?
    The connect() call can be in any code where you have access to both the sender and the receiver object.
    The other two arguments are basically just strings.

    Quote Originally Posted by jps3001 View Post
    My concern is how to generate signals and slots between different classes.
    No difference, really.

    Quote Originally Posted by jps3001 View Post
    I figured out that 'myClass' in your connect example is actually an object but can't find what 'myUIClass' refers to in your example, I tried MainWindow but did not work.
    It refers to a different object than the one "myClass" refers to.
    Both these arguments are just pointers to QObject.

    Quote Originally Posted by jps3001 View Post
    I wrote the connect function in the secondary class as follows:

    QObject::connect(objetA, SIGNAL(SendPlot()),MainWindow, SLOT(addPlot()) );

    but MainWindow is not recognized despite the following includes:
    Do you have a QObject pointer variable named MainWindow?
    Given its capitalization it looks awefully like the name of a class.

    Cheers,
    _

  25. #20
    Join Date
    Jul 2016
    Posts
    4
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Windows Android

    Default Re: How to access the UI from another class ?

    You are right, Mainwindow is a Class and not an Object

    However I can't figure out how to implement the following:

    I need to add a Widget to ui->verticalLayout->addWidget(plot0)

    as you know ui is private, I intend to use connect to access a SLOT which will add this Widget(plot0)

    but I do not have an Object available in ui.

    Should I have to create a dummy object just for this ?

    I'm really puzzled !

Similar Threads

  1. cannot access QLineEdit text in other methods of the class...
    By Leoha_Zveri in forum Qt Programming
    Replies: 2
    Last Post: 29th September 2009, 12:07
  2. Problem with QDialog class
    By sudheer168 in forum Qt Programming
    Replies: 4
    Last Post: 23rd December 2008, 09:03
  3. class QHBoxLayout
    By csvivek in forum Installation and Deployment
    Replies: 2
    Last Post: 10th April 2008, 07:57
  4. Replies: 4
    Last Post: 26th June 2007, 19:19
  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.