Results 1 to 6 of 6

Thread: Array question

  1. #1
    Join Date
    Sep 2008
    Posts
    24
    Thanks
    8
    Qt products
    Qt4
    Platforms
    Windows

    Default Array question

    Hi,

    I have a qwidget, that has as a member a 2-D array of ints. Now I'm having trouble accessing the vales in member functions of the widget.

    Qt Code:
    1. // the .h file
    2. class holdingWidget : public QWidget
    3. {
    4. Q_OBJECT
    5. .
    6. .
    7. .
    8. .
    9. protected:
    10. void paintEvent(QPaintEvent *ev);
    11.  
    12. private:
    13. int myArrayOne[44][3];
    To copy to clipboard, switch view to plain text mode 

    Now in the class defintion
    Qt Code:
    1. holdingWidget::holdingWidget(QWidget* parent): QWidget(parent)
    2. {
    3. int myArrayOne[44][3] =....
    4. .
    5. .
    6. .
    7. }
    8.  
    9. void holdingWidget::paintEvent(QPaintEvent *ev)
    10. {
    11. .
    12. .
    13. int k = myArrayOne[0][0];
    14. int l = myArrayOne[0][1];
    15. }
    To copy to clipboard, switch view to plain text mode 

    Now in the paintEvent the values are nonsense numbers and not the ones I specified above. But if I define the array in the paintevent it works ok. What am I missing here? Any help appreciated.

  2. #2
    Join Date
    Mar 2009
    Posts
    98
    Thanks
    3
    Thanked 9 Times in 9 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Array question

    You define the int myArrayOne[44][3]; in the class header, so in the constructor you don't have to re-define myArrayOne.
    myArrayOne defined in the constructor is a private variable visible only in the constructor. It is different from the one defined in the class as private member.

    try

    Qt Code:
    1. holdingWidget::holdingWidget(QWidget* parent): QWidget(parent)
    2. {
    3. for (int r=0; r < 44; ++r)
    4. for (int c=0; c<3; ++c)
    5. myArrayOne[r][c] = 1;
    6.  
    7.  
    8. }
    To copy to clipboard, switch view to plain text mode 

    EDIT: There was a trivial error
    Last edited by PaceyIV; 17th August 2009 at 18:00.

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

    td (17th August 2009)

  4. #3
    Join Date
    Sep 2008
    Posts
    24
    Thanks
    8
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Array question

    Ah I knew it would be something stupid like that that I'd miss! Thanks a lot. Works now.

  5. #4
    Join Date
    Sep 2008
    Posts
    24
    Thanks
    8
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Array question

    Ok so when initialising the array

    Qt Code:
    1. for(int i=0;i<44;i++)
    2. {
    3. for(int j=0;j<3;j++)
    4. {
    5. qDebug() << "For " << i << " - " << j;
    6. myArrayOne[i][j] = 1;
    7. qDebug() << myArrayOne[i][j];
    8. }
    9. }
    To copy to clipboard, switch view to plain text mode 

    The program won't build and crashes. Get the following error

    Unhandled exception at 0x6703f24c (QtCored4.dll) in myProgram.exe: 0xC0000005: Access violation reading location 0x00000035.
    Any ideas?

  6. #5
    Join Date
    Mar 2009
    Posts
    98
    Thanks
    3
    Thanked 9 Times in 9 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Array question

    Use the debugger.
    I don't think that the error you reported it's here!

  7. #6
    Join Date
    Mar 2007
    Location
    Germany
    Posts
    229
    Thanks
    2
    Thanked 29 Times in 28 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Array question

    And give your class an upper case first letter.
    Qt Code:
    1. class HoldingWidget
    To copy to clipboard, switch view to plain text mode 
    It's a world wide accepted convention to do this .

Similar Threads

  1. Problems passing an array of pointers to objects to a function
    By Valheru in forum General Programming
    Replies: 16
    Last Post: 30th June 2008, 00:11
  2. Pointer Question related to QLineEdit
    By ChrisReath in forum Qt Programming
    Replies: 1
    Last Post: 23rd May 2008, 15:13
  3. Problem in converting QString to QChar array?
    By KaKa in forum Qt Programming
    Replies: 2
    Last Post: 19th March 2007, 00:38
  4. How to create QPixmap from unsigned character array?
    By rashidbutt in forum Qt Programming
    Replies: 4
    Last Post: 16th May 2006, 18:25

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.