Results 1 to 4 of 4

Thread: Access form's widget's in other class

  1. #1
    Join Date
    May 2010
    Posts
    19
    Thanks
    1
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows Android

    Default Access form's widget's in other class

    Hello,

    i have a form manager.ui whose cpp file manager.cpp is as follows,

    Qt Code:
    1. #include "manager.h"
    2. #include "ui_manager.h"
    3.  
    4. manager::manager(QWidget *parent) :
    5. QMainWindow(parent),
    6. ui(new Ui::manager)
    7. {
    8. ui->setupUi(this);
    9. }
    10.  
    11.  
    12. manager::~manager()
    13. {
    14. delete ui;
    15. }
    16.  
    17. void manager::changeEvent(QEvent *e)
    18. {
    19. QMainWindow::changeEvent(e);
    20. switch (e->type()) {
    21. case QEvent::LanguageChange:
    22. ui->retranslateUi(this);
    23. break;
    24. default:
    25. break;
    26. }
    27. }
    28.  
    29. void manager::on_PB_General_clicked()
    30. {
    31. GetSystem_General();
    32. }
    To copy to clipboard, switch view to plain text mode 


    This GetSystem_General() is in file applysettings.cpp file as folows,

    Qt Code:
    1. #include "applysettings.h"
    2. #include "ui_manager.h"
    3. #include "../extras/Connections.h"
    4.  
    5.  
    6. void GetSystem_General()
    7. {
    8. //Here i want to access the manager class's widget QLabel for e.g LB_Cpu and set its text using setText method like,
    9. ui->LB_Cpu->setText("intel");
    10. }
    To copy to clipboard, switch view to plain text mode 

    so my question is how to access the manager class widgets in this applysettings.cpp.
    Last edited by wysota; 12th June 2010 at 13:36. Reason: missing [code] tags

  2. #2
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Access form's widget's in other class

    Quote Originally Posted by p3c0 View Post
    so my question is how to access the manager class widgets in this applysettings.cpp.
    Normaly you shouldn't do that. better use getter and setter methods or return a value from GetSystem_General() and set it to your line edit inside your class manager. But if you really whant to change your gui in that other function, just pass a pointer to your manager class to GetSystem_General().

  3. #3
    Join Date
    May 2010
    Posts
    19
    Thanks
    1
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows Android

    Default Re: Access form's widget's in other class

    Thanks for reply Lykurg,
    but the applysettings.cpp which contains GetSystem_General() is fully implemented. Restricted to use the getter setter methods.
    what pointer i have to pass to GetSystem_General() ?
    can you give an example

  4. #4
    Join Date
    May 2010
    Location
    Romania
    Posts
    1,021
    Thanks
    62
    Thanked 260 Times in 246 Posts
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Access form's widget's in other class

    I strongly recommend that you follow one of the first two recommendation that Lykurg gave you.

    The first is way to go, so use getter and setter functions for manager class (to access/set/modify the ui components)

    The pointer passing "stuff" won't be able to access the ui pointer (because is default private) and i don't recommend to make it public for this reason (when is another way to solve the problem, keep that pointer private )

Similar Threads

  1. Replies: 4
    Last Post: 29th May 2010, 12:56
  2. Controlling widget from form with class
    By strateng in forum Newbie
    Replies: 0
    Last Post: 6th April 2010, 01:56
  3. How can I access public variable parent Form
    By validator in forum Qt Programming
    Replies: 14
    Last Post: 18th December 2008, 21:12
  4. access the members of the Form from a loop.
    By cbarmpar in forum Qt Programming
    Replies: 2
    Last Post: 25th September 2008, 01:35
  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.