Results 1 to 5 of 5

Thread: Hiding QGraphicsRectItems

  1. #1
    Join Date
    Jan 2011
    Posts
    3
    Qt products
    Qt4
    Platforms
    Windows

    Post Hiding QGraphicsRectItems

    I am trying to use hide() show() or setVisible(false) function with QGraphicsRectItem objects. I can display them on the scene but no way I can do anything to hide them from screen. I have initialised them in the following code :

    Qt Code:
    1. rectItems = new QVector<QGraphicsRectItem *>();
    2.  
    3. for (int raw=0;raw<this->qElementsHeight;raw++){
    4. for(int column=0;column<this->qElementsWidth;column++){
    5. QRectF rectF( raw*48, column*48, 48, 48 );
    6. rect->setPen(pen);
    7. rectItems->push_back(rect);
    8. rectItems->last()->setVisible(false);
    9. this->addRect(rectItems->last()->rect(),pen);
    10. }
    11. }
    To copy to clipboard, switch view to plain text mode 

    then tried to use hide() in a slot function:

    Qt Code:
    1. for (int c=0;c<rectItems->count();c++){
    2. QGraphicsRectItem *temp =rectItems->at(c);
    3. if(bGrid) {
    4. //temp->setVisible(true);
    5. //temp->show();
    6. rectItems[0].at(c)->setVisible(true);
    7. }
    8. else {
    9. //temp->setVisible(false);
    10. //temp->hide();
    11. rectItems[0].at(c)->setVisible(false);
    12. }
    13. }
    To copy to clipboard, switch view to plain text mode 

    none of it hides the rectangles in the scene. Is there a way of hiding them ?
    Last edited by ddze; 28th January 2011 at 23:52. Reason: change the title

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Hiding QGraphicsRectItems

    Your code shouldn't even compile. Please check it out.
    It should probably be:
    Qt Code:
    1. rectItems[i]->hide();
    To copy to clipboard, switch view to plain text mode 
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Jan 2011
    Posts
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Hiding QGraphicsRectItems

    Well it compiles perfectly.

    QVector<QGraphicsRectItem *> *rectItems;
    Last edited by ddze; 29th January 2011 at 00:40. Reason: updated contents

  4. #4
    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: Hiding QGraphicsRectItems

    First please reconsider what wysota has told you:
    Qt Code:
    1. rectItems[0].at(c)
    To copy to clipboard, switch view to plain text mode 
    won't work like you expect it, second your pointer are all meaningless. Why? Because addRect() creates new items which are added to the scene. You have to store the pointers addRect() returns or simply use addItem().

  5. #5
    Join Date
    Jan 2011
    Posts
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Hiding QGraphicsRectItems

    Why should I consider wysota ?

    This is corrected code with little bit of mix pointer formats for him and you revising your comments.

    Correct Code:
    Qt Code:
    1. void QSystem::drawGrid() {
    2. bGrid=!bGrid;
    3. for (int c=0;c<rectItems->count();c++){
    4. if(bGrid) {rectItems->at(c)->show();}
    5. else {rectItems[0].at(c)->hide();}
    6. }
    7. }
    To copy to clipboard, switch view to plain text mode 

    Correct addRect:
    Qt Code:
    1. QGraphicsRectItem *rect=this->addRect(rectF,pen);
    To copy to clipboard, switch view to plain text mode 

    My mistake : I was in rush and did not read the QT Class QGraphicsScene addRect properly.
    QGraphicsRectItem * QGraphicsScene::addRect ( const QRectF & rect, const QPen & pen = QPen(), const QBrush & brush = QBrush() )
    I have mistakenly used QGraphicsRectItem instead of QRectF.

    Wrong:
    Qt Code:
    1. this->addRect(rectItems->last()->rect(),pen);
    To copy to clipboard, switch view to plain text mode 

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.