Results 1 to 10 of 10

Thread: Problem with painting QTable cell

  1. #1
    Join Date
    Apr 2007
    Posts
    76
    Thanks
    6
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Problem with painting QTable cell

    Hello!

    I try to paint special cell (dynamicaly of course) and i get this error for each cell i execute paint on it:
    "QGVector::remove: Index -7 out of range"
    and the cell does not change color and it is whitout any data.

    Qt Code:
    1. void UAutomat::PopulateVrednosti(int nRow)
    2. {
    3. tblRezultat->setText(nRow,0,QString("%1").arg(_ucenje->GetPovKaz(),0,'f',4));
    4. for(int i=0;i<_ucenje->_igralec.GetMaxAkc();i++)
    5. {
    6. if (i== _ucenje->GetAkcija())
    7. {
    8. MyItem it(tblRezultat,QTableItem::Never,QString("%1 \%").arg(_ucenje->_igralec.GetVerN(i)*100,0,'f',2));
    9.  
    10. if (_ucenje->GetKazen()) it.SetBkgColor(Qt::red);
    11. else it.SetBkgColor(Qt::green);
    12.  
    13. QColorGroup cg = colorGroup();
    14. QPainter p(tblRezultat);
    15. it.paint(&p,cg,tblRezultat->cellGeometry(nRow,i),false);
    16. }
    17. else
    18. tblRezultat->setText(nRow,i+1,QString("%1 \%").arg(_ucenje->_igralec.GetVerN(i)*100,0,'f',2));
    19. }
    20.  
    21. tblRezultat->showRow(nRow);
    22. }
    To copy to clipboard, switch view to plain text mode 

    tblRezultat is a pointer to QTable

    MyItem code is here
    Last edited by high_flyer; 27th April 2007 at 13:24. Reason: missing [code] tags

  2. #2
    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: Problem with painting QTable cell

    "QGVector::remove: Index -7 out of range"
    I didn't read your code very carefuly, but it looks ok.
    I'd say the above error is the problem.
    It looks like you have a problem with your indexes.
    Are you sure that nRow is a positive int, and that it is in the table row index range?
    ==========================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.

  3. #3
    Join Date
    Apr 2007
    Posts
    76
    Thanks
    6
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Problem with painting QTable cell

    yes i'm sure!
    otherwise others cells will be empty to, right?
    i printed out index value and it is ok.

  4. #4
    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: Problem with painting QTable cell

    otherwise others cells will be empty to, right?
    Not necessarily - only those who have bad indexes.
    Never the less, you most certainly have an index problem indicated by the error you posted.
    And the fact this error comes only when the cell painting is not working, suggests that there is a connection.
    Could you show the code whre you use QGVecotr::remove()?
    ==========================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.

  5. #5
    Join Date
    Apr 2007
    Posts
    76
    Thanks
    6
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Problem with painting QTable cell

    Could you show the code whre you use QGVecotr::remove()?
    I don't use this function anywhere, nor the QGVector.

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Problem with painting QTable cell

    Does this message disappear when you comment out line 15. (the one with "it.paint(...)")?

    Also what is that MyItem class and when do you call PopulateVrednosti()?

  7. #7
    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: Problem with painting QTable cell

    Well, could you show the loop in which PopulateVrednosti() is called?
    ==========================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.

  8. #8
    Join Date
    Apr 2007
    Posts
    76
    Thanks
    6
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Problem with painting QTable cell

    Well, could you show the loop in which PopulateVrednosti() is called?
    Qt Code:
    1. void UAutomat::PopulateVrednosti(int nRow)
    2. {
    3. tblRezultat->setText(nRow,0,QString("%1").arg(_ucenje->GetPovKaz(),0,'f',4));
    4. for(int i=0;i<_ucenje->_igralec.GetMaxAkc();i++)
    5. {
    6. if (i== _ucenje->GetAkcija())
    7. {
    8. // MyItem it(tblRezultat,QTableItem::Never,QString("%1 \%").arg(_ucenje->_igralec.GetVerN(i)*100,0,'f',2));
    9.  
    10. // if (_ucenje->GetKazen()) it.SetBkgColor(Qt::red);
    11. // else it.SetBkgColor(Qt::green);
    12.  
    13. QColorGroup cg = colorGroup();
    14. QPainter p(tblRezultat);
    15. // it.paint(&p,cg,tblRezultat->cellGeometry(nRow,i),false);
    16. // tblRezultat->setItem(nRow,i+1,&(QTableItem)it);
    17. }
    18. else
    19. tblRezultat->setText(nRow,i+1,QString("%1 \%").arg(_ucenje->_igralec.GetVerN(i)*100,0,'f',2));
    20. }
    21.  
    22. Monitor->append(QString("Vrstica: %3 Izbrana akcija: %1 %2").arg(_ucenje->GetAkcija()).arg((_ucenje->GetKazen())?
    23. "kazen":"nagrada").arg(nRow,0,'f',0));
    24. tblRezultat->showRow(nRow);
    25. }
    To copy to clipboard, switch view to plain text mode 

    Does this message disappear when you comment out line 15. (the one with "it.paint(...)")?
    no. it disappeare when i comment out the line with the initialization of the object MyItem. it s definition you can find here
    Last edited by jacek; 28th April 2007 at 19:02. Reason: missing [code] tags

  9. #9
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Problem with painting QTable cell

    Quote Originally Posted by codebehind View Post
    MyItem it(tblRezultat,QTableItem::Never, ...);
    ...
    tblRezultat->setItem(nRow,i+1,&(QTableItem)it);
    You create MyItem on the stack, pass a pointer to it to setItem() and then that MyItem is destroyed, because it goes out of scope, leaving a dangling pointer. Also there is no point in painting the item when PopulateVrednosti() is called --- QTable will repaint itself on its own.

    Try:
    Qt Code:
    1. if( i == _ucenje->GetAkcija() ) {
    2. MyItem * it = new MyItem( 0, QTableItem::Never, QString( "%1 \%" ).arg( _ucenje->_igralec.GetVerN(i)*100, 0, 'f', 2 ) );
    3. if( _ucenje->GetKazen() ) {
    4. it->SetBkgColor( Qt::red );
    5. else {
    6. it->SetBkgColor( Qt::green );
    7. }
    8. tblRezultat->setItem( nRow, i+1, it );
    9. }
    To copy to clipboard, switch view to plain text mode 

    If the message still appears, check if it isn't caused by "_ucenje->_igralec.GetVerN(i)".

    Also, please, use [code] tags for code snippets.
    Last edited by jacek; 28th April 2007 at 20:21. Reason: typo

  10. The following user says thank you to jacek for this useful post:

    codebehind (28th April 2007)

  11. #10
    Join Date
    Apr 2007
    Posts
    76
    Thanks
    6
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Problem with painting QTable cell

    thanks. it works great!
    about my posts i'll listen to your advice. I have to lear that too!

Similar Threads

  1. Highlighting a cell in QTable
    By sumsin in forum Qt Programming
    Replies: 1
    Last Post: 18th April 2007, 06:28
  2. word wrapping in QTable Cell
    By joseph in forum General Discussion
    Replies: 0
    Last Post: 27th November 2006, 09:30
  3. QTable resize problem with large tables
    By Robzzz in forum Newbie
    Replies: 3
    Last Post: 22nd May 2006, 14:13
  4. QTable - Cell Thickness
    By Solarity in forum Newbie
    Replies: 4
    Last Post: 16th March 2006, 19:15
  5. Problem with QTable
    By zlatko in forum Qt Programming
    Replies: 1
    Last Post: 16th February 2006, 10:00

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.