Page 1 of 2 12 LastLast
Results 1 to 20 of 21

Thread: QGraphics View

  1. #1
    Join Date
    Sep 2013
    Posts
    107
    Thanks
    16
    Thanked 2 Times in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default QGraphics View

    Dear All,

    I have developed a application using QGraphicsScene and QGraphicPixmapItem. In the application I'm adding pixmaps on the graphic scene. Initially I'm inserting
    Pixmap based on timer event for 1 seconds. On the same scene I need to insert second pixmap manually by pushbutton event. Im sharing the code here.plz help me, where I'm making mistake. I have attached output screen shot also.
    Qt Code:
    1. SERVER::SERVER(QWidget *parent) :
    2. QMainWindow(parent),
    3. ui(new Ui::SERVER)
    4. {
    5. ui->setupUi(this);
    6.  
    7. scene=new QGraphicsScene(0,0,740,400);
    8. scene=new QGraphicsScene(ui->graphicsView);
    9.  
    10. int x=50;
    11. int y=50;
    12.  
    13. timer.start(1000);
    14.  
    15. //other declarations line edit, push buttons
    16. }
    17.  
    18. void SERVER::timeout_123()
    19. {
    20.  
    21. x=x+2;
    22. y=y+2;
    23. QPixmap pix(":/t23_1.png");
    24. QGraphicsPixmapItem *pix1= scene->addPixmap(pix);
    25. ui->graphicsView->setScene(scene);
    26. pix1->setPos(x,y);
    27.  
    28.  
    29. }
    30.  
    31. void SERVER::on_pushButton_clicked()
    32. {
    33. qDebug("inserting new item");
    34. QPixmap pix2(":/pin30_red.png");
    35. QGraphicsPixmapItem *GIpix2= scene->addPixmap(pix2);
    36. ui->graphicsView->setScene(scene);
    37. GIpix2->setPos(650,150);
    38.  
    39. }
    To copy to clipboard, switch view to plain text mode 

    If i compile this code, initial timer based pixmap and pushbutton clicked pixmap will loaded onto the scene. In the Timer event Im incrementing setPos coordinated by 2 for every 1 second. Here on the scene, timer based pixmap is getting shifting in position but clear movement of image is not there somewat blurred.Any problem in the decalration?

    Thanks in Advance.
    Attached Images Attached Images

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QGraphics View

    There is a couple of weird things in your code:

    - you create two QGraphicScene instances but use the same variable, so the first instance is just leaked
    - you define to local variables x and y, maybe you wanted to initialized the members x and y?
    - you repeatedly call view->setScene(), just do that once in the constructor

    Now regarding your problem: what are you expecting to get? The screenshot looks correct, several pixmaps with small offsets following a line towards bottom right, just like the offset is calculated.

    Cheers,
    _

  3. #3
    Join Date
    Sep 2013
    Posts
    107
    Thanks
    16
    Thanked 2 Times in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QGraphics View

    thanks for the reply.... actually my output should look like the timer based pixmap should be shifting by setpos not multiple pixmaps as of screenshot now. in between if i insert one more pixmap by button click event ... even that should be there at mentioned pos... and also the timer pixmap should be shifting without multiple pixmap just like a moving image on the scene.how to get this....
    thanks

  4. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QGraphics View

    Quote Originally Posted by Vivek1982 View Post
    actually my output should look like the timer based pixmap should be shifting by setpos not multiple pixmaps as of screenshot now.
    If you add a new pixmap every time the timer fires, you get a new pixmap every time the timer fires. Seems pretty obvious, no?

    So if you don't want to end up with one pixmap per timer event, don't add new pixmaps in the slot handling the timer's signal.

    If you want to move an already existing pixmap, call its setPos() method.

    Cheers,
    _

  5. #5
    Join Date
    Sep 2013
    Posts
    107
    Thanks
    16
    Thanked 2 Times in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QGraphics View

    ok... u mean to say...in timer slot just call only setpos not add pixmap or set scene etc.... write all code in constructor only setpos in timer slot.
    regarding setscene(scene ).that should be also written in timer slot or not and same at button click event.... or i need to write complete code at constructor and just call setpos in timer slot and add pixmap in button click slot.is this okay
    Last edited by Vivek1982; 28th January 2014 at 01:05.

  6. #6
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QGraphics View

    Quote Originally Posted by Vivek1982 View Post
    ok... u mean to say...in timer slot just call only setpos not add pixmap or set scene etc.... write all code in constructor only setpos in timer slot.
    Yes

    Quote Originally Posted by Vivek1982 View Post
    regarding setscene(scene ).that should be also written in timer slot or not
    It just has to be called once, so right after constructing the scene is a good place.

    Quote Originally Posted by Vivek1982 View Post
    or i need to write complete code at constructor and just call setpos in timer slot and add pixmap in button click slot.is this okay
    You don't need to write "complete code" in the constructor, just what makes sense there. Since you create the scene in the constructor, setting it on the view is a logical next step right there.

    If the scene always contains the pixmap that is moved by the timer, then add it in the constructor.
    If the pixmap is not added before the first timer, then add it in the timer slot, but only once. Any further call to the timer slot then just moves it.

    Cheers,
    _

  7. #7
    Join Date
    Sep 2013
    Posts
    107
    Thanks
    16
    Thanked 2 Times in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QGraphics View

    Hi.. I tried your suggestion, program compiles but it gets hang and suggests me to close the application. I had given view->setscene(scene) directly only once in the constructor. pixmap was not moving on the view bith timer based Pixmap and button click pixmap, Please have a look. Now the code is almost same but timer based pixmap and click event based pixmap both appears but the issue is. Timer based pixmap is coming like route trace mutilple images(as shown in the earlier posted image). Im searching how to set initially at the constructor level and just to call SetPos at timer slot on the graphicitempixmap.

    regarding the x,y co-ordinates, points are coming at client side. by getting the values of x,y at timer interval,it has to update on the application screen.

    In my code what changes are expected, to move the timer pixmap without continous adding og pixmap and inserting click based pixmap on the scene.
    Qt Code:
    1. SERVER::SERVER(QWidget *parent) :
    2. QMainWindow(parent),
    3. ui(new Ui::SERVER)
    4. {
    5. ui->setupUi(this);
    6.  
    7. scene=new QGraphicsScene(0,0,740,400);
    8. scene=new QGraphicsScene(ui->graphicsView);
    9.  
    10. timer.start(2000);
    11. ...............connection signal slot on timer out.....
    12.  
    13. void SERVER::timeout_123()
    14. {
    15.  
    16. x=x+2;
    17. y=y+2;
    18. QPixmap pix1(":/t23_1.png");
    19. QGraphicsPixmapItem *GIpix1=scene->addPixmap(pix1);
    20. ui->graphicsView->setScene(scene);
    21. GIpix1->setPos(x,y);
    22. }
    23.  
    24. void SERVER::on_pushButton_clicked()
    25. {
    26. qDebug("inserting new item");
    27. QPixmap pix2(":/pin30_red.png");
    28. QGraphicsPixmapItem *GIpix2= scene->addPixmap(pix2);
    29. ui->graphicsView->setScene(scene);
    30. GIpix2->setPos(650,150);
    31.  
    32. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. #ifndef SERVER_H
    2. #define SERVER_H
    3.  
    4. #include <QMainWindow>
    5. #include <QPushButton>
    6. #include <QGraphicsView>
    7. #include <QGraphicsScene>
    8. #include <QBrush>
    9. #include <QGraphicsItem>
    10. #include <QGraphicsPixmapItem>
    11. #include <QTimer>
    12. #include <QDebug>
    13. #include <QPixmap>
    14.  
    15.  
    16. class SERVER : public QMainWindow {
    17. Q_OBJECT
    18. public:
    19. SERVER(QWidget *parent = 0);
    20. ~SERVER();
    21.  
    22.  
    23.  
    24. protected:
    25. void changeEvent(QEvent *e);
    26.  
    27. private:
    28. Ui::SERVER *ui;
    29. QPixmap *pix1,*pix2;
    30. QGraphicsItem *GIpix1,*GIpix2;
    31. QTimer *timer;
    32. unsigned int x,y;
    33.  
    34. private slots:
    35. void on_pushButton_clicked();
    36. };
    37.  
    38. #endif // SERVER_H
    To copy to clipboard, switch view to plain text mode 
    Last edited by Vivek1982; 28th January 2014 at 10:37. Reason: updated contents

  8. #8
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QGraphics View

    Your code has not changed at all. Maybe you posted the wrong version again?

    Cheers,
    _

  9. #9
    Join Date
    Sep 2013
    Posts
    107
    Thanks
    16
    Thanked 2 Times in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QGraphics View

    no...actually i tried alot ... the program was getting hang.... my query is on my timer slot i can add pixmap in scene... but how to declare in constructor or writing full code in constructor and just call setpos in timer slot.if i do that program will hang...that is the reason for posting same code to suggest me.

    or i need to write like graphics pixmap item = new graphics pixmap item(pixmap(location of pixmap).
    next add item to scene.and setpos in timer slot... Im checking how to do it... Im trying all methods either by addpixmap in timer slot ,multiple pixmap will come or application gets hang... how to do it ?


    Added after 1 50 minutes:


    if any changes to be made in timer slot.plz let me know in procedures
    i will try on it.
    Last edited by Vivek1982; 28th January 2014 at 17:18.

  10. #10
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QGraphics View

    Quote Originally Posted by Vivek1982 View Post
    no...
    Yes, which you even say yourself here
    Quote Originally Posted by Vivek1982 View Post
    actually i tried alot ... the program was getting hang.... my query is on my timer slot i can add pixmap in scene... but how to declare in constructor or writing full code in constructor and just call setpos in timer slot.if i do that program will hang...that is the reason for posting same code to suggest me.
    If you just post the same code again, there is no new information for anyone reading this thread.
    So it was basically a useless posting. Well actually worse, because it needed another reply just and another useless posting and this reply.

    It might very well be that your changed code runs into an error, but without that code it is impossible to help you solve that error.

    Quote Originally Posted by Vivek1982 View Post
    or i need to write like graphics pixmap item = new graphics pixmap item(pixmap(location of pixmap).
    next add item to scene.and setpos in timer slot
    That's what I wrote, isn't it.

    Quote Originally Posted by Vivek1982 View Post
    if any changes to be made in timer slot.plz let me know in procedures
    i will try on it.
    Ok, again: add the pixmap to the scene in the constructor. Move it in the timer slot.

    Cheers,
    _

  11. #11
    Join Date
    Sep 2013
    Posts
    107
    Thanks
    16
    Thanked 2 Times in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QGraphics View

    no actually same code after changing in constructor and program is getting hang..how to approach now

    ok..sorry .....i will try on your suggestion ...plz can u tel me in procedures how to do it....with same code can we modify or new code to be written.

  12. #12
    Join Date
    Sep 2013
    Posts
    107
    Thanks
    16
    Thanked 2 Times in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QGraphics View

    Hi.. I have changed the code as per your suggestion but still program is getting hang, Initiallty I have created scene and on top of it loaded view. Based on logic of my application I created PIXMAP, GraphicsPixmapItem. On the created pointer of GraphicItem I'm calling the PIXMAP to set on the scene, calling setscene.

    In the Timer slot im calling setpos action to insert the Pixmap. Still Im confused, what as gone wrong. Even my button click Pixmap is not working now..
    Qt Code:
    1. SERVER::SERVER(QWidget *parent) :
    2. QMainWindow(parent),
    3. ui(new Ui::SERVER)
    4. {
    5. ui->setupUi(this);
    6.  
    7. scene=new QGraphicsScene(0,0,740,400);
    8. scene=new QGraphicsScene(ui->graphicsView);
    9. QPixmap pix1(":/t23_1.png");
    10. scene->addPixmap(pix1);
    11. GIpix1->setPixmap(pix1);
    12.  
    13. ui->graphicsView->setScene(scene);
    14. timer.start(2000);
    15. ...............connection signal slot on timer out.....
    16.  
    17. void SERVER::timeout_123()
    18. {
    19.  
    20. x=x+2;
    21. y=y+2;
    22. GIpix1->setPos(x,y);
    23. }
    24.  
    25. void SERVER::on_pushButton_clicked()
    26. {
    27. qDebug("inserting new item");
    28. QPixmap pix2(":/pin30_red.png");
    29. QGraphicsPixmapItem *GIpix2= scene->addPixmap(pix2);
    30. ui->graphicsView->setScene(scene);
    31. GIpix2->setPos(650,150);
    32.  
    33. }
    To copy to clipboard, switch view to plain text mode 


    Qt Code:
    1. #ifndef SERVER_H
    2. #define SERVER_H
    3.  
    4. #include <QMainWindow>
    5. #include <QPushButton>
    6. #include <QGraphicsView>
    7. #include <QGraphicsScene>
    8. #include <QBrush>
    9. #include <QGraphicsItem>
    10. #include <QGraphicsPixmapItem>
    11. #include <QTimer>
    12. #include <QDebug>
    13. #include <QPixmap>
    14.  
    15.  
    16. class SERVER : public QMainWindow {
    17. Q_OBJECT
    18. public:
    19. SERVER(QWidget *parent = 0);
    20. ~SERVER();
    21.  
    22.  
    23.  
    24. protected:
    25. void changeEvent(QEvent *e);
    26.  
    27. private:
    28. Ui::SERVER *ui;
    29. QPixmap *pix1,*pix2;
    30. QGraphicsItem *GIpix1,*GIpix2;
    31. QTimer *timer;
    32. unsigned int x,y;
    33.  
    34. private slots:
    35. void on_pushButton_clicked();
    36. };
    37.  
    38. #endif // SERVER_H
    To copy to clipboard, switch view to plain text mode 

  13. #13
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QGraphics View

    Ah, that's better.

    1) you are still creating two scenes, leaking the first one
    2) you are assigning the GraphicPixmapItem pointer to a local variable instead of the member that you are using in the timer slot

    Cheers,
    _

  14. #14
    Join Date
    Sep 2013
    Posts
    107
    Thanks
    16
    Thanked 2 Times in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QGraphics View

    Plz can u suggest now. what I need to do.. which line of code Im stuck up.. First I need to ask is..
    1.I have wriiten a code but while calling setpos in timer slot. Just GIPix->SetPos(x,y).will work or wat declaring al other logic in constructors. Always my application is getting hang.

    2.As suggested earlier post. In the timer slot,If i add pixmap to scene, that will come continous on veiw, or If i write in the constructor It will get hanged..

    plz correct me in code. where is my fault

  15. #15
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QGraphics View

    Quote Originally Posted by Vivek1982 View Post
    plz correct me in code. where is my fault
    We can only correct you code if you post the code. As long as you refuse to make any of the suggested adjustments and post the updated code, there is nothing further we can do.
    Repeating the very same suggested over and over will lead nowhere.

    Cheers,
    _

    P.S.: You keep on claiming that you have done the changes and that you observe your application "hanging", but I start to doubt whether you actually have done anything at all and just use the "hanging" as an excuse.

  16. #16
    Join Date
    Sep 2013
    Posts
    107
    Thanks
    16
    Thanked 2 Times in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QGraphics View

    sorry Im trying all changes to be made and compile all the time ... now Im checking line wise to address your suggestion like calling scene twice and timer slot member...please validate the logic for my application.then i will fix it....

    1. initially create scene in constructor.
    2.load on to veiw
    3.create pixmap
    4.create graphics pixmap item pointer on it add the scene and pixmap.
    5.set the scene on graphics veiw
    6.on timer slot call for the item setpos.

    if this logic is fine?i will work out to fix this

    thanks

  17. #17
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QGraphics View

    Yes, that sounds good.

    I think 2 and 5 are actually the same thing, though

    Cheers,
    _

  18. #18
    Join Date
    Sep 2013
    Posts
    107
    Thanks
    16
    Thanked 2 Times in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QGraphics View

    Thanks for your inputs on my code. Finally I was able to resolve one mistake is that I was calling setscene twice or many times. Now coming for second problem, moving the Pixmap. I have created scene and setscene on constructor only.When coming to timer slot. code follows
    Qt Code:
    1. ui->setupUi(this);
    2. server=new QTcpServer(this);
    3.  
    4.  
    5. scene=new QGraphicsScene(0,0,740,400);
    6. ui->graphicsView->setScene(scene);
    7.  
    8. x=100;
    9. y=150;
    10.  
    11. timer=new QTimer;
    12. timer->start(4000);
    13.  
    14. connect(timer,SIGNAL(timeout()),this,SLOT(timeout_123()));
    15.  
    16. void SERVER::timeout_123()
    17. {
    18.  
    19. x=x+4;
    20. y=y+4;
    21.  
    22. QGraphicsPixmapItem *GIPix1= new QGraphicsPixmapItem(QPixmap(":/t23_1.png"));
    23. scene->addItem(GIPix1);
    24. GIpix1->setPos(x,y);
    25.  
    26. }
    To copy to clipboard, switch view to plain text mode 

    Even I had told click based Pixmap insertion, that is also done.. THE ONLY issue is now to avoid multiple Pixmap appearing on screen based on timer slot. If write code like calling all declaration in conctrusctor and just only GIPix1->setPos(x,y) in Timer Slot. Application will not work.
    You had suggested me to check on the local variable initilization instead of slot members, here im fully confused, Totally IM Lost, I'm not getting any ideas beyond this.plz provide solution on this to me.

    If I remove scene->addItem(GIPix1), the program will not run only..So in timer slot I have given both scene add and setpos on PixmapItem pointer.
    Even gave attempt to check all the possibilities,can we give like scene->setPixmap in timer slot instead of scene->addItem.no that is not there. or still calling another slot just to setpOS, even that didnt work.. I'm fully lost.I need a solution for this. please

    Thanks in advance

  19. #19
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QGraphics View

    You have a member variable called GIpix1 of type QGraphicsItem*, see posting #12 http://www.qtcentre.org/threads/5781...156#post258156

    In the same post you have the following code in the constructor
    Qt Code:
    1. scene->addPixmap(pix1);
    2. GIpix1->setPixmap(pix1);
    To copy to clipboard, switch view to plain text mode 
    What you do here is create a local variable with the nam GIpix1, the member variable with the same name stays uninitialized. This is why you get a problem in the timer slot when you access the member variable again.

    So the newly created item pointer needs to be stored in the member variable

    Qt Code:
    1. GIpix1 = scene->addPixmap(QPixmap(":/t23_1.png"));
    To copy to clipboard, switch view to plain text mode 

    Cheers,
    _

  20. The following user says thank you to anda_skoa for this useful post:

    Vivek1982 (30th January 2014)

  21. #20
    Join Date
    Sep 2013
    Posts
    107
    Thanks
    16
    Thanked 2 Times in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QGraphics View

    Im sorry.plz can u give solution for this in coding.. Im totally mad... Im trying all the methods .. but nothing is going to work out i know but still. for time being i need to do it plz can u give solution for this logic.... then i can understand well.... i prefer good training for this....
    Last edited by Vivek1982; 30th January 2014 at 16:12.

Similar Threads

  1. QGraphics View
    By Vivek1982 in forum Newbie
    Replies: 13
    Last Post: 20th December 2013, 11:26
  2. Items in QGraphics View not starting from the top.
    By penny in forum Qt Programming
    Replies: 3
    Last Post: 1st April 2011, 13:10
  3. Qgraphics View
    By amagdy.ibrahim in forum Qt Programming
    Replies: 1
    Last Post: 15th June 2008, 10:10
  4. QGraphics view update : Help needed
    By kiranraj in forum Qt Programming
    Replies: 3
    Last Post: 3rd July 2007, 12:54
  5. QGraphics view problem
    By kiranraj in forum Qt Programming
    Replies: 5
    Last Post: 6th March 2007, 21:28

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.