Results 1 to 4 of 4

Thread: GraphicsItem is flickering

  1. #1
    Join Date
    Jan 2008
    Posts
    91
    Thanks
    8

    Default GraphicsItem is flickering

    Dear Friends,
    I have a problem with setPos() function in QGraphicsItem class, I have created my own GraphicsItem class derived from QGraphicsItem. I loaded Iems in a Scene. Now I am locating Items, and trying to slide some pictures, but pictures are flickering.

    I am using a QTimer and it's slot for moving pictures, setPos() function .

    How can I prevend this problem.

  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: GraphicsItem is flickering

    Please provide a minimal compilable example reproducing the problem.
    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 2008
    Posts
    91
    Thanks
    8

    Default Re: GraphicsItem is flickering

    GraphicsItem.cpp

    Qt Code:
    1. #include "GraphicItem.h"
    2.  
    3. GraphicItem::GraphicItem(QPixmap image)
    4. {
    5. // TODO Auto-generated constructor stub
    6. myWidth = 150;
    7. myHeight =150;
    8. m_Selected = false;
    9. Org = image;
    10. makePhoto(Org);
    11. makeReflection();
    12. }
    13.  
    14. GraphicItem::GraphicItem()
    15. {
    16. myWidth = 150;
    17. myHeight =150;
    18. m_Selected = false;
    19. Org = NULL;
    20. makePhoto(Org);
    21. makeReflection();
    22. }
    23. GraphicItem::~GraphicItem() {
    24. // TODO Auto-generated destructor stub
    25. }
    26.  
    27.  
    28. QRectF GraphicItem::boundingRect() const
    29. {
    30. return QRectF(0.0f,0.0f,myWidth,myHeight);
    31. }
    32.  
    33.  
    34. void GraphicItem::SelectedStatus(bool Selected)
    35. {
    36. m_Selected = Selected;
    37. makePhoto(Org);
    38. makeReflection();
    39. }
    40.  
    41. void GraphicItem::MoveLeft()
    42. {
    43. SetRightPlace(pos().x()+200,pos().y());
    44. }
    45.  
    46. void GraphicItem::MoveRight()
    47. {
    48. SetRightPlace(pos().x()-200,pos().y());
    49. }
    50.  
    51. void GraphicItem::SetRightPlace(int x,int y)
    52. {
    53. RightX = x;
    54. RightY = y;
    55. }
    56.  
    57. bool GraphicItem::IsRightPlace()
    58. {
    59. return(RightX == pos().x()) && (RightY == pos().y());
    60. }
    61.  
    62. void GraphicItem::Animate()
    63. {
    64. int x = pos().x();
    65. int y = pos().y();
    66.  
    67. int dx = 6;
    68. int dy = 6;
    69.  
    70. if(pos().x() < RightX)
    71. {
    72. if((pos().x() + dx >= RightX))
    73. x = RightX;
    74. else
    75. x = pos().x() + dx;
    76. }
    77. else if(pos().x() > RightX)
    78. {
    79. if((pos().x() - dx <= RightX))
    80. x = RightX;
    81. else
    82. x = pos().x() - dx;
    83. }
    84.  
    85. if(pos().y() < RightY)
    86. {
    87. if((pos().y() + dy >= RightY))
    88. y = RightY;
    89. else
    90. y = pos().y() + dy;
    91. }
    92. else if(pos().y() > RightY)
    93. {
    94. if((pos().y() - dy <= RightY))
    95. y = RightY;
    96. else
    97. y = pos().y() - dy;
    98. }
    99. setPos(x,y);
    100. }
    101.  
    102.  
    103. void GraphicItem::ChangePixmap(QPixmap pixmap)
    104. {
    105. Org = pixmap;
    106. makePhoto(Org);
    107. makeReflection();
    108. }
    109.  
    110. void GraphicItem::paint(QPainter* painter,const QStyleOptionGraphicsItem*,
    111. {
    112. printf("void GraphicItem::paint\n");
    113. painter->drawPixmap(0,0,item);
    114. painter->drawPixmap(0,static_cast<int>(myHeight*0.5f),itemMirror);
    115. }
    116.  
    117. void GraphicItem::makePhoto(QPixmap image)
    118. {
    119. item = QPixmap(myWidth,static_cast<int>(myHeight*0.5f));
    120. if((image.width() > myWidth - 2) || (image.height() > myHeight*0.5f-2))
    121. image = image.scaled(myWidth-2,static_cast<int>(myHeight*0.5f-2),
    122. Qt::KeepAspectRatio,Qt::SmoothTransformation);
    123. QPainter* painter = new QPainter(&item);
    124. QPen pen;
    125. if(m_Selected)
    126. pen.setBrush(Qt::red);
    127. else
    128. pen.setBrush(Qt::darkGray);
    129.  
    130. pen.setWidth(5);
    131. painter->setPen(pen);
    132. QLinearGradient gradient;
    133. gradient = QLinearGradient(0,0,0,static_cast<int>(myHeight*0.5f));
    134. gradient.setColorAt(0.0f,QColor(175,175,175));
    135. gradient.setColorAt(0.5f,QColor(215,215,215));
    136. gradient.setColorAt(1.0f,QColor(175,175,175));
    137. painter->setBrush(QBrush(gradient));
    138. painter->drawRect(0,0,myWidth,static_cast<int>(myHeight*0.5f));
    139. painter->drawPixmap(static_cast<int>(myWidth*0.5f-(image.width()*0.5f)),
    140. static_cast<int>(myHeight*0.25f-(image.height()*0.5f)),
    141. image);
    142. delete painter;
    143. }
    144.  
    145. void GraphicItem::makeReflection()
    146. {
    147. itemMirror = QPixmap(myWidth,static_cast<int>(myHeight*0.5f));
    148. QPainter* painter = new QPainter(&itemMirror);
    149. painter->drawPixmap(0,0,QPixmap::fromImage(item.toImage()
    150. .mirrored(false,true)));
    151. painter->resetTransform();
    152. QPen pen(Qt::transparent);
    153. painter->setPen(pen);
    154. QLinearGradient gradient;
    155. gradient = QLinearGradient(0,0,0,static_cast<int>(myHeight*0.375f));
    156. gradient.setColorAt(0.0f,QColor(0,15,15,155));
    157. gradient.setColorAt(1.0f,QColor(0,15,15,255));
    158. painter->setBrush(QBrush(gradient));
    159. painter->drawRect(0,0,myWidth,static_cast<int>(myHeight*0.5f));
    160. delete painter;
    161. }
    To copy to clipboard, switch view to plain text mode 

    GraphicsItem.h

    Qt Code:
    1. /*
    2.  * GraphicItem.h
    3.  *
    4.  * Created on: Sep 29, 2009
    5.  * Author: root
    6.  */
    7.  
    8. #ifndef GRAPHICITEM_H_
    9. #define GRAPHICITEM_H_
    10.  
    11. #include <QGraphicsItem>
    12. #include <QGraphicsScene>
    13. #include <QPixmap>
    14. #include <QImage>
    15. #include <QLinearGradient>
    16. #include <QPainter>
    17.  
    18. class GraphicItem : public QGraphicsItem
    19. {
    20. public:
    21. GraphicItem(QPixmap image);
    22. GraphicItem();
    23. virtual ~GraphicItem();
    24.  
    25. QRectF boundingRect() const;
    26. void paint(QPainter* painter,const QStyleOptionGraphicsItem* option,
    27. QWidget* widget);
    28.  
    29. void SelectedStatus(bool Selected);
    30. void ChangePixmap(QPixmap pixmap);
    31.  
    32. void MoveRight();
    33. void MoveLeft();
    34.  
    35. void SetRightPlace(int x,int y);
    36. bool IsRightPlace();
    37. void Animate();
    38.  
    39. public:
    40. bool m_Selected;
    41. int RightX;
    42. int RightY;
    43. int myWidth;
    44. int myHeight;
    45. //Pixmap's for drawing methods
    46. QPixmap Org; // Original Pixmap
    47. QPixmap item; // Scaled and Framed
    48. QPixmap itemMirror; // Mirrored
    49.  
    50.  
    51. void makePhoto(QPixmap Image);
    52. void makeReflection();
    53. };
    54.  
    55. #endif /* GRAPHICITEM_H_ */
    To copy to clipboard, switch view to plain text mode 

    TestView derived from QGraphicsView

    Qt Code:
    1. #pragma once
    2.  
    3. #include "GenericView.h"
    4. #include "ITestView.h"
    5. #include "ITestCallBack.h"
    6. #include "GraphicItem.h"
    7. #include <QTimer>
    8.  
    9. class TestView : public GenericView , public ITestView
    10. {
    11. Q_OBJECT
    12. public:
    13. TestView();
    14. virtual~TestView();
    15. void SetCallBack(ITestCallBack *CallBack);
    16. void SetIndex(int Indx);
    17. int GetIndex();
    18. void SetList(std::list <Media> Liste);
    19. void ShowView();
    20.  
    21. private:
    22. std::vector <GraphicItem*> Items;
    23. std::vector <GraphicItem*>::iterator Itr;
    24.  
    25. int SelectIndx;
    26. void Key_Right();
    27. void Key_Left();
    28. void LoadData(std::list <Media> Liste);
    29. void LoadMenuItems(int Indx);
    30. void GetXYData(int Indx);
    31. QPoint *points;
    32. QTimer* timer;
    33. QTimer* timer2;
    34.  
    35. ITestCallBack *callback;
    36. std::list <Media> _Liste;
    37.  
    38. public slots:
    39. void Animate();
    40.  
    41.  
    42. protected:
    43. void showEvent(QShowEvent* event);
    44. };
    To copy to clipboard, switch view to plain text mode 



    Qt Code:
    1. #include "TestView.h"
    2. #include <QPixmap>
    3.  
    4. TestView::TestView()
    5. {
    6. timer = new QTimer(this);
    7. timer->setInterval(15);
    8. connect(timer,SIGNAL(timeout()),this,SLOT(Animate()));
    9.  
    10. }
    11.  
    12. TestView::~TestView()
    13. {
    14. delete callback;
    15. }
    16.  
    17.  
    18.  
    19. void TestView::Key_Right()
    20. {
    21. callback->Key_RightPressed();
    22. }
    23.  
    24. void TestView::Key_Left()
    25. {
    26. callback->Key_LeftPressed();
    27. }
    28.  
    29. void TestView::SetCallBack(ITestCallBack *CallBack)
    30. {
    31. callback = CallBack;
    32. }
    33.  
    34. void TestView::SetIndex(int Indx)
    35. {
    36. if(Indx < Items.size() && Indx >= 0)
    37. {
    38. if(!timer->isActive())
    39. {
    40. if(Indx > SelectIndx)
    41. {
    42. for(int i=0;i<Items.size();i++)
    43. Items[i]->MoveRight();
    44.  
    45. }
    46. else if(Indx < SelectIndx)
    47. {
    48. for(int i=0;i<Items.size();i++)
    49. Items[i]->MoveLeft();
    50. }
    51.  
    52. SelectIndx = Indx;
    53. timer->start();
    54. }
    55. }
    56. }
    57. int TestView::GetIndex()
    58. {
    59. return SelectIndx;
    60. }
    61.  
    62. void TestView::SetList(std::list <Media> Liste)
    63. {
    64. _Liste = Liste;
    65. LoadData(_Liste);
    66. }
    67. void TestView::ShowView()
    68. {
    69. show();
    70. }
    71.  
    72. void TestView::showEvent(QShowEvent* event)
    73. {
    74. if(!timer->isActive())
    75. timer->start();
    76. }
    77.  
    78. void TestView::LoadData(std::list <Media> Liste)
    79. {
    80. int i = 0;
    81. for(std::list<Media>::iterator itr=Liste.begin();itr != Liste.end();itr++)
    82. {
    83. QPixmap pixmap(QString().fromStdString(itr->_PicUrl));
    84. GraphicItem *item = new GraphicItem(pixmap);
    85. Items.push_back(item);
    86. m_GraphicsScene->addItem(item);
    87. item->setPos(25+i,150);
    88. i+=200;
    89. }
    90.  
    91. SelectIndx = 0;
    92. LoadMenuItems(SelectIndx);
    93. Items[SelectIndx]->SelectedStatus(true);
    94.  
    95. }
    96.  
    97. void TestView::Animate()
    98. {
    99. for(int i = 0; i < Items.size(); ++i)
    100. Items[i]->Animate();
    101.  
    102. bool stop = true;
    103. for(int i = 0; i < Items.size(); ++i)
    104. {
    105. if(!Items.at(i)->IsRightPlace())
    106. {
    107. stop = false;
    108. break;
    109. }
    110. }
    111. if(stop)
    112. timer->stop();
    113. }
    114.  
    115.  
    116. void TestView::LoadMenuItems(int Indx)
    117. {
    118. GetXYData(Indx);
    119. for(int i = 0; i < Items.size(); ++i)
    120. Items[i]->SetRightPlace(points[i].x(),points[i].y());
    121.  
    122. }
    123.  
    124. void TestView::GetXYData(int Indx)
    125. {
    126.  
    127. points = new QPoint[Items.size()];
    128.  
    129. points[Indx].setX(225);
    130. points[Indx].setY(150);
    131.  
    132. for(int i=Indx-1;i>=0;i--)
    133. {
    134. points[i].setX(points[i+1].x()-200);
    135. points[i].setY(150);
    136. }
    137.  
    138. for(int i=Indx+1;i<Items.size();i++)
    139. {
    140. points[i].setX(points[i-1].x()+200);
    141. points[i].setY(150);
    142. }
    143. for(int i=0;i<Items.size();i++)
    144. printf("%d = x=%d,y=%d*\n",i,points[i].x(),points[i].y());
    145.  
    146. }
    To copy to clipboard, switch view to plain text mode 

  4. #4
    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: GraphicsItem is flickering

    I think we have a different understanding of the word "minimal". Please strip it to the absolute minimum (not more than 30 lines in total) and post it as an attachment.
    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.


Similar Threads

  1. resize graphicsitem
    By rogerholmes in forum Newbie
    Replies: 2
    Last Post: 18th August 2009, 05:55
  2. how to show time in a GraphicsItem
    By wagmare in forum Qt Programming
    Replies: 9
    Last Post: 24th April 2009, 09:52
  3. Image Viewer Example and Flickering
    By babu198649 in forum Newbie
    Replies: 0
    Last Post: 7th August 2008, 15:42
  4. QFileDialog flickering
    By invictus in forum Newbie
    Replies: 8
    Last Post: 17th April 2008, 09:47
  5. how to avoid QCanvasText flickering (Qt336)
    By oob2 in forum Qt Programming
    Replies: 5
    Last Post: 5th September 2006, 19: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.