Results 1 to 11 of 11

Thread: Problem pointing to a QStringList

  1. #1
    Join Date
    Nov 2012
    Posts
    12
    Thanks
    6
    Qt products
    Qt4
    Platforms
    Windows

    Default Problem pointing to a QStringList

    I wanted to make my application logging, so I added a "QStringList l" to my Mainwindow Class.
    Because the Program has some sub-classes that also should be logging, I added Pointers to the mainwindow's QStringList into them.
    The program executes the Pledge-Algorithm for a robot to leave a room full of blocks, so I created the Classes "CPledge", "CRob" and "CMap".

    The problem: I get a SIGSEGV error when I reference the log-lists of CRob/CMap to the Mainwindow's logList.
    CODE:
    Mainwindow Class:
    Qt Code:
    1. #include <QMainWindow>
    2.  
    3. namespace Ui {
    4. class MainWindow;
    5. }
    6.  
    7. class MainWindow : public QMainWindow
    8. {
    9. Q_OBJECT
    10.  
    11. public:
    12. explicit MainWindow(QWidget *parent = 0);
    13. ~MainWindow();
    14.  
    15. // !The Log Object!
    16.  
    17. void saveLog(QString file);
    18.  
    19. private:
    20. Ui::MainWindow *ui;
    21.  
    22.  
    23. QString i(int i);
    24. QString dt();
    25.  
    26. private slots:
    27. void on_btStartThread_clicked();
    28. // etc.
    29. };
    30.  
    31.  
    32. #endif // MAINWINDOW_H
    To copy to clipboard, switch view to plain text mode 

    The CPledge Class (Gets the CRob out of a CMap):
    Qt Code:
    1. class CPledge : public QObject
    2. {
    3. Q_OBJECT
    4. public:
    5. explicit CPledge(QObject *parent = 0);
    6.  
    7. CMap *map;
    8. CRob *rob;
    9. void setBot(int x, int y);
    10. // Log list reference
    11.  
    12. private:
    13. void followLeftWallTilTurnIsZero();
    14.  
    15. public slots:
    16. void solve(QString filePath);
    17.  
    18. signals:
    19. void finished();
    20. };
    To copy to clipboard, switch view to plain text mode 

    The CMap Class (The Labyrinth):
    Qt Code:
    1. class CMap : public QObject
    2. {
    3. Q_OBJECT
    4. public:
    5. explicit CMap(QObject *parent = 0);
    6. // Log list reference
    7.  
    8. typedef QList<bool> StonesLine;
    9.  
    10. void loadFromFile(QString FilePath);
    11.  
    12. bool free(int x, int y);
    13. bool left(int x, int y, int turn);
    14. bool front(int x, int y, int turn);
    15. QPoint movedPos(int x, int y, int turn);
    16.  
    17. int cleanTurning(int turn);
    18.  
    19. QPoint *defaultRobPos;
    20.  
    21. private:
    22. // A 2D bool-list: 1->stone; 0-> No stone
    23. QList <StonesLine> stones;
    24. // The list of points where the rob is free
    25. QPoint freePoints[2];
    26.  
    27. void insertBlock(int x1, int y1, int x2, int y2);
    28. void initBlocks(int width, int height);
    29.  
    30. StonesLine horizontalStonesBorder(int width);
    31. StonesLine verticalStonesBorder(int width);
    32. };
    To copy to clipboard, switch view to plain text mode 

    The CRob Class (The Robot):
    Qt Code:
    1. class CRob : public QObject
    2. {
    3. Q_OBJECT
    4. public:
    5. explicit CRob(QObject *parent = 0);
    6. // Log list reference
    7.  
    8. bool free();
    9.  
    10. bool left();
    11. bool front();
    12.  
    13. void turnR();
    14. void turnL();
    15.  
    16. void move();
    17.  
    18. void setPosition(int newx, int newy);
    19.  
    20. int turning();
    21.  
    22. private:
    23. int x, y;
    24. CMap *map;
    25.  
    26. int fturning;
    27. };
    To copy to clipboard, switch view to plain text mode 


    The Error comes here at Mainwindow.cpp:
    Qt Code:
    1. // etc.
    2. void MainWindow::on_btStartThread_clicked()
    3. {
    4. CPledge *pledge = new CPledge();
    5. pledge->l = &l;
    6. // #########FATAL SIGSEGV ERROR################
    7. pledge->rob->l = &l;
    8. pledge->map->l = &l;
    9.  
    10.  
    11. pledge->solve(ui->edMapPath->text());
    12. }
    13. // etc.
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Sep 2011
    Posts
    1,241
    Thanks
    3
    Thanked 127 Times in 126 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Problem pointing to a QStringList

    Get your debugger out and then tell me the values of:

    pledge->rob
    pledge->map
    If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

  3. #3
    Join Date
    Nov 2012
    Posts
    12
    Thanks
    6
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Problem pointing to a QStringList

    Stopping at a breakpoint after "pledge->l = &l;", I get following content:


    &l <0 Elements> @0x22fe60 QStringList
    pledge->l <0 Elements> @0x22fe60 QStringList
    pledge->map 3131961357 @0xbaadf00d CMap
    pledge->rob 3131961357 @0xbaadf00d CRob


    pledge->rob->l <unavailable synchronous data>
    pledge->map->l <unavailable synchronous data>

    what does "<unavailable synchronous data>" mean?


    Added after 7 minutes:


    Oh, I forgot initializing the map and rob inside CPledge;
    Thank you
    Last edited by ljuhrich; 10th November 2012 at 16:22.

  4. #4
    Join Date
    Sep 2011
    Posts
    1,241
    Thanks
    3
    Thanked 127 Times in 126 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Problem pointing to a QStringList




    ...10 chars..
    If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

  5. #5
    Join Date
    Nov 2012
    Posts
    12
    Thanks
    6
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Problem pointing to a QStringList

    Quote Originally Posted by amleto View Post
    ...10 chars..
    what do you mean with this?

    but now there's another problem:

    In CRob functions, I cannot access the QStringList *l.
    Code as above, and an error comes here:
    Qt Code:
    1. bool CRob::free()
    2. {
    3. bool free = map->free(x, y);
    4. if (free) {
    5. l << "FREE";
    6. }
    7. return free;
    8. }
    To copy to clipboard, switch view to plain text mode 

    The error at l << etc. is
    "crob.cpp:17: error: invalid operands of types 'QStringList*' and 'const char [5]' to binary 'operator<<'"

  6. #6
    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: Problem pointing to a QStringList

    That l is a pointer to a QStringList isn't it? If so try de-referencing it: *l << "FREE"; and make sure that function isn't called before the pointer l is initialized.

    Also some of Qt's classes have implicit sharing, read about that and then decide if you really need to complicate your life with heap allocation of the container (or value and implicit-share or reference or const reference doesn't do the trick). Holding an container directly in a class doesn't increase the size of that class with the size of all elements allocated in the container (the elements are most likely heap allocated by the container), so you can use QStringList member instead of a pointer (like you did with the other l - also you should use better names for your variables )

  7. #7
    Join Date
    Sep 2011
    Posts
    1,241
    Thanks
    3
    Thanked 127 Times in 126 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Problem pointing to a QStringList

    Quote Originally Posted by ljuhrich View Post
    what do you mean with this?
    There is a 10 char minimum limit for posts so I wasn't allowed to reply with
    as it's only two chars.
    If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

  8. #8
    Join Date
    Nov 2012
    Posts
    12
    Thanks
    6
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Problem pointing to a QStringList

    Quote Originally Posted by Zlatomir View Post
    That l is a pointer to a QStringList isn't it? If so try de-referencing it: *l << "FREE"; and make sure that function isn't called before the pointer l is initialized.
    Sorry, I forgot writing that it also doesn't work with dereferencing - when I say '*l << "FREE"', the compiler also says "no match for operator<<".
    but it anyway seems strange that QtCreator doesn't recognize l being QStringList*, because it doesn't change "l." into "l->"...


    Added after 4 minutes:


    That works with the private CMap *map, but when I add a private QStringList *testlog, it doesn't work either...
    Last edited by ljuhrich; 10th November 2012 at 19:35.

  9. #9
    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: Problem pointing to a QStringList

    Make sure that you have #include <QStringList> in your .cpp file and not only a forward declaration of QStringList class.

  10. #10
    Join Date
    Sep 2011
    Posts
    1,241
    Thanks
    3
    Thanked 127 Times in 126 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Problem pointing to a QStringList

    Quote Originally Posted by ljuhrich View Post
    Sorry, I forgot writing that it also doesn't work with dereferencing - when I say '*l << "FREE"', the compiler also says "no match for operator<<".
    but it anyway seems strange that QtCreator doesn't recognize l being QStringList*, because it doesn't change "l." into "l->"...


    Added after 4 minutes:


    That works with the private CMap *map, but when I add a private QStringList *testlog, it doesn't work either...
    show your code. This should work fine. Proof:

    http://www.comeaucomputing.com/tryitout/

    Qt Code:
    1. #include <string>
    2.  
    3. class MockQStrList
    4. {
    5. public:
    6. MockQStrList();
    7. ~MockQStrList();
    8.  
    9. MockQStrList& operator<<(std::string const & )
    10. {
    11. return *this;
    12. }
    13. };
    14.  
    15. int main()
    16. {
    17.  
    18. MockQStrList* l = new MockQStrList;
    19.  
    20. *l << "";
    21.  
    22. }
    To copy to clipboard, switch view to plain text mode 
    If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

  11. #11
    Join Date
    Nov 2012
    Posts
    12
    Thanks
    6
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Problem pointing to a QStringList

    #include <QStringList> solved it...

    I did not add ti because QtCreator recognized QStringList, so I thought it would not be necessary...

    thanks for taking you time...

Similar Threads

  1. QStringList problem :/
    By hakermania in forum Qt Programming
    Replies: 9
    Last Post: 17th August 2010, 14:55
  2. qstringlist problem
    By stella1016 in forum Qt Programming
    Replies: 4
    Last Post: 17th December 2009, 10:16
  3. Pointing Two Dimensional Pointer to return value
    By babu198649 in forum General Programming
    Replies: 5
    Last Post: 24th April 2009, 20:16
  4. Replies: 4
    Last Post: 9th January 2009, 12:57
  5. Replies: 7
    Last Post: 2nd June 2006, 13:48

Tags for this Thread

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.