Results 1 to 12 of 12

Thread: QGraphicsScene...How to have your own format for button,etc..???

  1. #1
    Join Date
    Jan 2008
    Location
    Finland /Pakistan
    Posts
    216
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    20
    Thanked 1 Time in 1 Post

    Default QGraphicsScene...How to have your own format for button,etc..???

    I am trying to develop a simple UI using QGraphicsScene...this UI has some buttons which i have included in the UI by using addWidget() function ,also i have used addText() function to add some text..the problem i am facing is that ,that the text and buttons are placed at the same position ,so the text is hiden behind the button and is invisible...
    does any one suggest some thing how can i specify my own layout ,my own way of presenting button and text on the View..
    Thanks In advance

  2. #2
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    8
    Thanked 334 Times in 317 Posts

    Default Re: QGraphicsScene...How to have your own format for button,etc..???

    In graphics scene you can specify a z-order value for the graphicsitems. The items with more zvalue are displayed above other levels.
    So you can set higher zvalue/zorder for the text item.

    As for the layout, I dont think you can set layout for a scene. What you can do is add them in another widget with layout and add that widget to the scene.(Qt 4.4 - widgets in scene).

    Hope this helps

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

    Default Re: QGraphicsScene...How to have your own format for button,etc..???

    QGraphicsItem::setPos() might be helpful as well

  4. #4
    Join Date
    Jan 2008
    Location
    Finland /Pakistan
    Posts
    216
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    20
    Thanked 1 Time in 1 Post

    Default Re: QGraphicsScene...How to have your own format for button,etc..???

    Both setPos() and z-order are solution ....now i have to read into setPos()...because this seems to be one of those qt function whose implementation is not that easy ...

  5. #5
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    8
    Thanked 334 Times in 317 Posts

    Default Re: QGraphicsScene...How to have your own format for button,etc..???

    Why dou have to worry about implementation ??
    YOu just need to create a graphics item, set its z-order, and setPos. thats it,

  6. #6
    Join Date
    Jan 2008
    Location
    Finland /Pakistan
    Posts
    216
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    20
    Thanked 1 Time in 1 Post

    Default Re: QGraphicsScene...How to have your own format for button,etc..???

    i cannt understand how to use setPos() function,it says about the parent/child relationship ,which is somewhat confusing ..can anyone give a sample of code to how to implement it simply by showing couple of things on a GraphicsView and how does this work..any help would be appreciated..Thanks

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

    Default Re: QGraphicsScene...How to have your own format for button,etc..???

    Think of it as an anchor which you can move and drop on different parts of the parent item. The robot example that is bundled with Qt should help you understand it. I suggest viewing Andreas Hanssen's presentation about QGraphicsView from the last DevDays.

  8. The following user says thank you to wysota for this useful post:

    salmanmanekia (13th June 2008)

  9. #8
    Join Date
    Jan 2008
    Location
    Finland /Pakistan
    Posts
    216
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    20
    Thanked 1 Time in 1 Post

    Default Re: QGraphicsScene...How to have your own format for button,etc..???

    'I suggest viewing Andreas Hanssen's presentation about QGraphicsView from the last DevDays'...where can i find it ?

  10. #9
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    8
    Thanked 334 Times in 317 Posts

    Default Re: QGraphicsScene...How to have your own format for button,etc..???

    I guess here

  11. The following user says thank you to aamer4yu for this useful post:

    salmanmanekia (13th June 2008)

  12. #10
    Join Date
    Jan 2008
    Location
    Finland /Pakistan
    Posts
    216
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    20
    Thanked 1 Time in 1 Post

    Default Re: QGraphicsScene...How to have your own format for button,etc..???

    I have created two graphics items object and tried to set position of them by using setPos but surprsingly it doesnt work...one item is for displaying text and one item is for displaying a widget...when i only create a object for displaying text ,it works fine and set pos works well,but if i only create a instant for widget item and use setPos it doesnt work,and also if i try to use both of them so that both are shown in the view at the same time ,,this also doesnt works...!!!..may be code would be much more helpful

    // MAIN FUNCTION //

    Qt Code:
    1. #include "exclass.h"
    2. #include <QApplication>
    3. #include <QGraphicsScene>
    4.  
    5. int main(int argc, char *argv[])
    6. {
    7. QApplication a(argc, argv);
    8.  
    9.  
    10. exclass *lay = new exclass;
    11. Horlay *txt = new Horlay;
    12.  
    13.  
    14.  
    15. lay->Widgt(&scene);
    16. txt->Strng(&scene);
    17.  
    18. lay->setPos(5,5);
    19. txt->setPos(500,500);
    20.  
    21. view.setBackgroundBrush(QPixmap(":/images/background.JPG"));
    22.  
    23. view.setScene(&scene);
    24.  
    25. view.show();
    26.  
    27. return a.exec();
    28.  
    29. }
    To copy to clipboard, switch view to plain text mode 

    // HEADER CLASS //

    Qt Code:
    1. #ifndef EXCLASS_H
    2. #define EXCLASS_H
    3.  
    4. #include <QtGui/QWidget>
    5. #include <QGraphicsScene>
    6. #include <QGraphicsView>
    7. #include <QGraphicsItem>
    8. #include <QPainter>
    9. #include <QImage>
    10. #include <QPushButton>
    11. #include <QHBoxLayout>
    12.  
    13. class exclass : public QGraphicsItem
    14. {
    15.  
    16. public:
    17. QPushButton *stopButton;
    18. QPushButton *playButton;
    19. QHBoxLayout *hboxLay;
    20.  
    21. QWidget *widg;
    22.  
    23. exclass(QGraphicsItem *parent = 0);
    24.  
    25. void Widgt(QGraphicsScene *);
    26.  
    27. QRectF boundingRect() const;
    28. void paint(QPainter *painter,const QStyleOptionGraphicsItem *option,QWidget *widget);
    29.  
    30. };
    31.  
    32. class Horlay :public QGraphicsItem
    33. {
    34. public:
    35.  
    36. QString str;
    37.  
    38. Horlay(QGraphicsItem *parent = 0);
    39.  
    40. void Strng(QGraphicsScene *);
    41.  
    42. QRectF boundingRect() const;
    43. void paint(QPainter *painter,const QStyleOptionGraphicsItem *option,QWidget *widget);
    44.  
    45. };
    46.  
    47. #endif // EXCLASS_H
    To copy to clipboard, switch view to plain text mode 

    // SOURCE CLASS //

    Qt Code:
    1. #include "exclass.h"
    2.  
    3. exclass::exclass(QGraphicsItem *parent):QGraphicsItem(parent)
    4. {
    5. hboxLay = new QHBoxLayout;
    6. playButton = new QPushButton;
    7. stopButton = new QPushButton;
    8. widg = new QWidget;
    9. playButton->setIcon(QIcon(":/images/play.JPG"));
    10. stopButton->setIcon(QIcon(":/images/stop.JPG"));
    11. }
    12.  
    13. void exclass::Widgt(QGraphicsScene *scene)
    14. {
    15. hboxLay->addWidget(playButton);
    16. hboxLay->addWidget(stopButton);
    17. widg->setLayout(hboxLay);
    18. scene->addWidget(widg);
    19. }
    20.  
    21. QRectF exclass::boundingRect() const
    22. {
    23.  
    24. }
    25.  
    26. void exclass::paint(QPainter *painter,const QStyleOptionGraphicsItem *option,QWidget *widget)
    27. {
    28.  
    29. }
    30.  
    31.  
    32. Horlay::Horlay(QGraphicsItem *parent):QGraphicsItem(parent)
    33. {
    34.  
    35. }
    36.  
    37. void Horlay::Strng(QGraphicsScene *scene)
    38. {
    39. str = "salman" ;
    40. scene->addText(str);
    41. }
    42.  
    43. void Horlay::paint(QPainter *painter,const QStyleOptionGraphicsItem *option,QWidget *widget)
    44. {
    45.  
    46. }
    47.  
    48. QRectF Horlay::boundingRect() const
    49. {
    50.  
    51. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by wysota; 16th June 2008 at 07:11. Reason: missing [code] tags

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

    Default Re: QGraphicsScene...How to have your own format for button,etc..???

    To be honest your code doesn't make much sense and is invalid and won't work... Those methods you have to implement are there for a reason, you can't leave them empty.

    Why not do it this way?
    Qt Code:
    1. int main(int argc, char **argv){
    2. QApplication app(argc, argv);
    3. QGraphicsScene *scene = new QGraphicsScene(&view);
    4. view.setScene(scene);
    5. QWidget *wgt = new QWidget;
    6. QHBoxLayout *l = new QHBoxLayout(wgt);
    7. l->addWidget(new QPushButton("play"));
    8. l->addWidget(new QPushButton("stop"));
    9. QGraphicsProxyWidget *wgtItem = scene->addWidget(wgt);
    10. QGraphicsTextItem *txtItem = scene->addText("salman");
    11. wgtItem->setPos(100,100);
    12. txtItem->setPos(200, 300);
    13. view.show();
    14. return app.exec();
    15. }
    To copy to clipboard, switch view to plain text mode 

  14. #12
    Join Date
    Jan 2008
    Location
    Finland /Pakistan
    Posts
    216
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    20
    Thanked 1 Time in 1 Post

    Default Re: QGraphicsScene...How to have your own format for button,etc..???

    Ya,thanks,great,not only the problem is solved but things are also clear..
    In your code you implemented a Widget in main function itself ,it worked fine...
    i tried to make a class derived from QWidget and define the play and stop button there and called the object of that class in main

    exclass *lay = new exclass;

    QGraphicsProxyWidget *widgItem = scene->addWidget(lay);

    widgItem->setPos(20,20);

    but in this case it shows an empty gray screen with in my view which has purple background...

Similar Threads

  1. Is there a known problem with QMimeData on Mac OS X?
    By Wurgl in forum Qt Programming
    Replies: 8
    Last Post: 27th February 2008, 22:21
  2. Some very weird compilation warnings
    By MarkoSan in forum Qt Programming
    Replies: 21
    Last Post: 23rd January 2008, 16:48

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.