Results 1 to 8 of 8

Thread: Accessing the UI ! Help !

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

    Thumbs down Accessing the UI ! Help !

    Hi there !

    I am trying to change the value of a QProgressBar from a class named anotherClass (not from my gui class, which is guiClass).

    It builds, but the resulting build crashes !

    Here is the error in QT Creator :

    Qt Code:
    1. ... exited with code -1073741819
    To copy to clipboard, switch view to plain text mode 

    => -1073741819 indicates an access violation in windows. An access violation means your process has tried to access memory (ie dereference a pointer) that does not belong to it.


    Here is my code :


    Qt Code:
    1. class guiClass : public QMainWindow
    2. {
    3. Q_OBJECT
    4.  
    5. public:
    6. guiClass(QWidget *parent = 0);
    7. ~guiClass
    8. Ui::guiClass *ui;
    9.  
    10. };
    11.  
    12. class anotherClass : public QProgressBar {
    13.  
    14. Q_OBJECT
    15.  
    16. public:
    17. anotherClass(QObject* parent);
    18. void anything();
    19. guiClass *guiClass;
    20.  
    21. };
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. void anotherClass::anything(){
    2. this->guiClass->ui->progressBar->setValue(80); // THIS IS THE WRONG PART
    3. }
    To copy to clipboard, switch view to plain text mode 

  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: Accessing the UI ! Help !

    Did you initialize your ui and did you set the pointer guiClass right? Can we see the implementation?

    EDIT: why do you don't use signals and slots? Any particular reasons for that. Because your case seems to be classical case for using signals.
    Last edited by Lykurg; 28th November 2009 at 21:44. Reason: updated contents

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

    Default Re: Accessing the UI ! Help !

    Put a BP on the "wrong part" and check the values of guiclass and ui.

    You do initialise guiClass in anotherClass don't you?

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

    Default Re: Accessing the UI ! Help !

    .
    .
    Thank you !

    Did you initialize your ui and did you set the pointer guiClass right? Can we see the implementation?
    Qt Code:
    1. guiClass ::guiClass (QWidget *parent)
    2. : QMainWindow(parent), ui(new Ui::guiClass )
    3. {
    4. ui->setupUi(this);
    5.  
    6.  
    7. //this->ui->progressBar->setValue(80); // IT WORKS, but it is in guiClass
    8.  
    9. anotherClass *anotherclass = new anotherClass(this);
    10. anotherclass->anything();
    11.  
    12. }
    13.  
    14. guiClass ::~guiClass ()
    15. {
    16. delete ui;
    17. }
    To copy to clipboard, switch view to plain text mode 

    @Lykurg : I don't use SIGNALS and SLOTS because I don't know how to use them
    Moreover, I'd prefer to do it without signals and slots.

    @fatjuicymole : what is a BP ?

    PS: I'm a beginner.

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

    Default Re: Accessing the UI ! Help !

    BP = Breakpoint

    Where do you initialise guiClass in anotherClass?

  6. #6
    Join Date
    Jan 2008
    Location
    Poland
    Posts
    687
    Thanks
    4
    Thanked 140 Times in 132 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Accessing the UI ! Help !

    Quote Originally Posted by fitzy View Post
    I don't use SIGNALS and SLOTS because I don't know how to use them
    So maybe it is time to learn how to use them. And why you would like not to use signals and slots?
    I would like to be a "Guru"

    Useful hints (try them before asking):
    1. Use Qt Assistant
    2. Search the forum

    If you haven't found solution yet then create new topic with smart question.

  7. #7
    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: Accessing the UI ! Help !

    Quote Originally Posted by fitzy View Post
    Qt Code:
    1. guiClass ::guiClass (QWidget *parent)
    2. : QMainWindow(parent), ui(new Ui::guiClass )
    3. {
    4. ui->setupUi(this);
    5.  
    6.  
    7. //this->ui->progressBar->setValue(80); // IT WORKS, but it is in guiClass
    8.  
    9. anotherClass *anotherclass = new anotherClass(this);
    10. anotherclass->anything();
    11.  
    12. }
    To copy to clipboard, switch view to plain text mode 
    I knew it! you haven't set the pointer guiClass in anotherClass. So you accessing a null pointer!

    Qt Code:
    1. class anotherClass : public QProgressBar {
    2.  
    3. Q_OBJECT
    4.  
    5. public:
    6. anotherClass(QObject* parent);
    7. void anything();
    8. guiClass *guiClass;
    9. void setBackPointer(guiClass *p) {guiClass = p;} // <- add
    10.  
    11. };
    12.  
    13. // and then
    14.  
    15. guiClass ::guiClass (QWidget *parent)
    16. : QMainWindow(parent), ui(new Ui::guiClass )
    17. {
    18. ui->setupUi(this);
    19. anotherClass *anotherclass = new anotherClass(this);
    20. anotherclass->setBackPointer(this); // <- add
    21. anotherclass->anything();
    22. }
    To copy to clipboard, switch view to plain text mode 

    you can also use the parent of your constructor for setting the back pointer, but you really should learn signal and sots...

  8. The following user says thank you to Lykurg for this useful post:

    fitzy (29th November 2009)

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

    Default Re: Accessing the UI ! Help !

    You're right !
    I didn't set the pointer guiClass in anotherClass.

    Thank you so much !

Similar Threads

  1. QTableView : accessing the data
    By marvaneke in forum Newbie
    Replies: 10
    Last Post: 30th March 2012, 11:31
  2. QCachece accessing problem
    By rajini333 in forum Qt Programming
    Replies: 1
    Last Post: 5th June 2009, 19:15
  3. Accessing deleted memory, Producer Consumer app
    By ^NyAw^ in forum General Programming
    Replies: 3
    Last Post: 2nd August 2008, 13:58
  4. Accessing data from a worker thread
    By steg90 in forum Qt Programming
    Replies: 20
    Last Post: 25th May 2007, 10:20
  5. Accessing Environment Variables
    By mhoover in forum Qt Programming
    Replies: 6
    Last Post: 21st September 2006, 15:05

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.