Results 1 to 7 of 7

Thread: QList append() problem

  1. #1
    Join Date
    Dec 2009
    Posts
    9
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default QList append() problem

    First here is my code:


    //My_Highlightingrule.h
    Qt Code:
    1. class My_Highlightingrule
    2. {
    3. public:
    4. My_Highlightingrule(QRegExp* ,QTextCharFormat* );
    5. QRegExp* getPattern();
    6. QTextCharFormat* getFormat();
    7.  
    8.  
    9. private:
    10. QRegExp* pattern;
    11. QTextCharFormat* format;
    12. };
    To copy to clipboard, switch view to plain text mode 


    //My_Highlightingrule_KeyWords.h
    Qt Code:
    1. class My_Highlightingrule_KeyWords : public QList<My_Highlightingrule*>
    2. {
    3. public:
    4. My_Highlightingrule_KeyWords();
    5. };
    To copy to clipboard, switch view to plain text mode 

    //My_Highlightingrule_KeyWords.cpp
    Qt Code:
    1. #include "my_highlightingrule_keyWords.h"
    2.  
    3.  
    4. My_Highlightingrule_KeyWords::My_Highlightingrule_KeyWords()
    5. {
    6. const int MAX = 51;
    7. std::string keyWords[MAX] = {"abstract", "continue", "for", "new",
    8. "switch", "assert", "default", "goto",
    9. "package", "synchronized", "boolean",
    10. "do", "if", "private", "this", "break",
    11. "double", "implements", "protected",
    12. "throw", "byte", "else", "import",
    13. "public", "throws", "case", "enum",
    14. "instanceof", "return", "transient",
    15. "catch", "extends", "int", "short",
    16. "try", "char", "final", "interface",
    17. "static", "void", "class", "finally",
    18. "long", "strictfp", "volatile", "const",
    19. "float", "native", "super", "while"};
    20.  
    21. QBrush brush(QColor(255,0,0,127));
    22. format.setForeground(brush);
    23.  
    24. QRegExp *pattern;
    25.  
    26. for(int i = 0; i < MAX; i++)
    27. {
    28. pattern = new QRegExp(QString("\\b%1\\b").arg(keyWords[i].c_str()));
    29. My_Highlightingrule *rule = new My_Highlightingrule(pattern,&format);
    30. insert(i,rule);
    31. }
    32.  
    33. std::cout << at(49)->getPattern()->pattern()->toStdString() << std::endl; //NO SEGMENTATION FAULT
    34.  
    35. delete(pattern);
    36. }
    To copy to clipboard, switch view to plain text mode 



    //My highlight function...does not highlight now

    Qt Code:
    1. void My_CppSyntaxHighlighter::highlight(const QString & text)
    2. {
    3. QList<My_Highlightingrule*> rules;
    4. My_Highlightingrule_KeyWords keyWords = y_Highlightingrule_KeyWords();
    5.  
    6. std::cout << keyWords.at(49)->getPattern()->pattern()->toStdString() << std::endl; //SEGMENTATION FAULT
    7.  
    8.  
    9. rules += keyWords; //KeyWords, and so on
    10. }
    To copy to clipboard, switch view to plain text mode 




    Ahhm, as you see I have not inserted the #includes and so on.
    What I want:
    I want a Class My_CppHIghlightingrules which derive from My_SyntaxHighlighting (Is only a Abstract class with a member highlight(const QString&). The function highlight(const QString& text) in My_CppHighlightingrules should add some Highlightingrules (e.g.: My_Highlighting_KeyWords) and then go with a loop through all rules and return the new text.
    Problem:
    My Problem is this. When I create an object of My_Highlightingrule_Keywords in highlight(const QString&) and I want the last Keyword "while" (see My_Highlightingrule_Keywords() ) then it gives me an Segmentation fault. But when I print the last Keyword "while" in My_Highlightingrule_KeyWords (see My_Highlighting_Keywords() ) then it gives me no Segmentation fault. WHY????? I don't understand it. Please help me.

  2. #2
    Join Date
    Sep 2009
    Location
    Tashkent, Uzbekistan
    Posts
    107
    Thanks
    1
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QList append() problem

    Did you try to debug? If you use Linux - try GDB.
    -- Tanuki

    per cauda vel vaculus cauda

  3. #3
    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: QList append() problem

    Are you sure that '49' is a valid value?

    Try the follwoing code:
    Qt Code:
    1. int iIndex = 49;
    2. if(iIndex < keyWords.size()){
    3. std::cout << keyWords.at(iIndex)->getPattern()->pattern()->toStdString() << std::endl;
    4. }else std::cout<<"The index:"<<iIndex<<" is not allowed to be larger than:"<< keyWords.size()<<std::endl;
    To copy to clipboard, switch view to plain text mode 
    ==========================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.

  4. #4
    Join Date
    Dec 2009
    Posts
    9
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QList append() problem

    I tried it in every constellation and it is for sure a right value. I mean it is a right value in the constructor My_Highlightingrule_Keywords, but in my Object it is not. I DON'T understand it. It is not logical for me.
    I am open for every possibility

  5. #5
    Join Date
    Jul 2009
    Posts
    139
    Thanks
    13
    Thanked 59 Times in 52 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QList append() problem

    Your My_Highlightingrule class definition suggests you are storing both pattern and format. However, you delete pattern soon after and format is allocated on the stack. Meaning neither can be stored unless you've done a deep copy. If you need further help, how about posting the constructor for My_Highlightingrule.

  6. #6
    Join Date
    Nov 2009
    Posts
    129
    Thanks
    4
    Thanked 29 Times in 29 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QList append() problem

    What happens if you remove the delete statement at the end of the My_Highlightingrule_KeyWords constructor?

    Are you sure you correctly copied MAX=51 (that it is not MAX=50)?

  7. #7
    Join Date
    Dec 2009
    Posts
    9
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QList append() problem

    Quote Originally Posted by numbat View Post
    Your My_Highlightingrule class definition suggests you are storing both pattern and format. However, you delete pattern soon after and format is allocated on the stack. Meaning neither can be stored unless you've done a deep copy. If you need further help, how about posting the constructor for My_Highlightingrule.
    Oh my god. I must be more careful by handling with pointers. Now it works fine.

    Thanks a lot for the replies.

    My new constructer:


    Qt Code:
    1. My_Highlightingrule_KeyWords::My_Highlightingrule_KeyWords()
    2. {
    3. const int MAX = 50;
    4. std::string keyWords[MAX] = {"abstract", "continue", "for", "new",
    5. "switch", "assert", "default", "goto",
    6. "package", "synchronized", "boolean",
    7. "do", "if", "private", "this", "break",
    8. "double", "implements", "protected",
    9. "throw", "byte", "else", "import",
    10. "public", "throws", "case", "enum",
    11. "instanceof", "return", "transient",
    12. "catch", "extends", "int", "short",
    13. "try", "char", "final", "interface",
    14. "static", "void", "class", "finally",
    15. "long", "strictfp", "volatile", "const",
    16. "float", "native", "super", "while" };
    17.  
    18. QBrush brush(QColor(255,0,0,127));
    19. format->setForeground(brush);
    20.  
    21. QRegExp *pattern;
    22.  
    23. for(int i = 0; i < MAX; i++)
    24. {
    25. pattern = new QRegExp(QString("\\b%1\\b").arg(keyWords[i].c_str()));
    26. My_Highlightingrule *rule = new My_Highlightingrule(pattern,format);
    27. insert(i,rule);
    28. }
    29. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by reuabreliz; 6th January 2010 at 13:31.

Similar Threads

  1. Unicode Font Display Problem
    By QbelcorT in forum Qt for Embedded and Mobile
    Replies: 0
    Last Post: 2nd September 2009, 06:11
  2. Problem with QAbstractListModel
    By eekhoorn12 in forum Qt Programming
    Replies: 3
    Last Post: 26th August 2009, 14:26
  3. QList Pointers
    By coderbob in forum Newbie
    Replies: 2
    Last Post: 20th November 2007, 18:50
  4. QList problem
    By acix in forum General Programming
    Replies: 6
    Last Post: 29th April 2006, 13:08
  5. Replies: 16
    Last Post: 7th March 2006, 15:57

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
  •  
Qt is a trademark of The Qt Company.