Results 1 to 1 of 1

Thread: how to implement rotatable custom button using QGraphicsview QGraphicspixmapitem ?

  1. #1
    Join Date
    Jan 2013
    Posts
    11
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default how to implement rotatable custom button using QGraphicsview QGraphicspixmapitem ?

    Hi All,

    Per some recommendations by Lykurg and Wysota a while back, I'm trying to implement a custom button using qgraphicsview stuff for an existing QT GUI app. The reason to go custom is that I need to be able to rotate the button by small increments (and have the bounding box rotate with it). The end use is to have an array of buttons like flower petals, where each petal is a slot in an instrument carousel. In each button I need a Qgraphicspixmapitem and two qgraphicstextitems. My thought was to create my own class called slotButton which has a graphic item (the petal), a text item for the slot number, and another text item for the name. I need to generate clicked signals-- this appears to be a bit of a trick, because Qgraphicsitems aren't qobjects, and don't use the Q_OBJECT macro. Here's what I have so far, and I'm wondering if I'm going down a rabbit hole, as ->rotate and ->setPos, and mousePressEvent aren't working.

    To summarize:
    Is this the right way to do this?
    Why aren't rotate, setPos, and mousepress working?

    I've included the source, a screenshot, and a concept scrawl.

    Thanks!
    Brian

    carouselwidget.h:

    Qt Code:
    1. #ifndef CAROUSELWIDGET_H
    2. #define CAROUSELWIDGET_H
    3.  
    4. #include <QWidget>
    5. #include <QGraphicsView>
    6. #include <QGraphicsScene>
    7. #include <QGraphicsPixmapItem>
    8. #include <QPixmap>
    9. #include <QGraphicsItem>
    10.  
    11. class carouselWidget : public QWidget
    12. {
    13.  
    14. Q_OBJECT
    15. public:
    16. explicit carouselWidget(QWidget *parent = 0);
    17. QGraphicsView * carousel_view;
    18. QGraphicsScene * carousel_scene;
    19. protected:
    20. // void mousePressEvent(QMouseEvent *event);
    21. signals:
    22.  
    23. public slots:
    24.  
    25.  
    26. };
    27.  
    28. #endif // CAROUSELWIDGET_H
    To copy to clipboard, switch view to plain text mode 

    carouselwidget.cpp:
    Qt Code:
    1. #include "carouselwidget.h"
    2. #include "slotbutton.h"
    3. carouselWidget::carouselWidget(QWidget *parent) :
    4. QWidget(parent)
    5. {
    6.  
    7.  
    8. carousel_scene = new QGraphicsScene(0,0,300,300,this);
    9. carousel_view = new QGraphicsView(this);
    10. carousel_view->setScene(carousel_scene);
    11.  
    12. //item = new QGraphicsPixmapItem(QPixmap("carouselfinger.png"),0, carousel_scene);
    13. //item->setFlag(QGraphicsItem::ItemIsSelectable,true);
    14.  
    15. //item->rotate(30);
    16. //item->setPos(50,50);
    17. carousel_view->show();
    18. slotButton foo(carousel_scene);
    19. foo.setPos(50,50);
    20. foo.rotate(30);
    21. //foo.setScene(carousel_scene);
    22. //carousel_scene = new QGraphicsScene(this);
    23. //carousel_scene->addText("Hello World!");
    24. //carousel_view = new QGraphicsView(carousel_scene);
    25. //carousel_view->show();
    26. }
    27.  
    28. //void carouselWidget::mousePressEvent(QMouseEvent *event)
    29. //{
    30. // item->setPixmap(QPixmap("carouselfinger_on.png"));
    31. //}
    To copy to clipboard, switch view to plain text mode 

    slotButton.h:
    Qt Code:
    1. #ifndef SLOTBUTTON_H
    2. #define SLOTBUTTON_H
    3.  
    4. #include <QWidget>
    5. #include <QGraphicsPixmapItem>
    6. #include <QGraphicsItem>
    7. #include <QPainter>
    8. #include <QStyleOptionGraphicsItem>
    9. #include <QLabel>
    10. #include <QString>
    11. #include <QFont>
    12. class slotButton : public QGraphicsItem
    13. {
    14. // Q_OBJECT
    15.  
    16. public:
    17. explicit slotButton(QGraphicsScene *parent);
    18. void paint(QPainter *painter, const QStyleOptionGraphicsItem *option,QWidget *widget);
    19. QRectF boundingRect() const;
    20. protected:
    21. void mousePressEvent(QMouseEvent *event);
    22. signals:
    23.  
    24. public slots:
    25.  
    26. };
    27.  
    28. #endif // SLOTBUTTON_H
    To copy to clipboard, switch view to plain text mode 
    SlotButton.cpp:
    Qt Code:
    1. #include "slotbutton.h"
    2.  
    3. slotButton::slotButton(QGraphicsScene *parent) :
    4. {
    5. QString foo;
    6. graphic = new QGraphicsPixmapItem(QPixmap("carouselfinger.png"),0, parent);
    7. num = new QGraphicsTextItem(foo,0,parent);
    8. foo = "1";
    9. num->setPos(30, 0);
    10. num->setPlainText(foo);
    11. num->setFont(QFont("Helvetica [Cronyx]", 12));
    12. foo = "Quartz";
    13. name = new QGraphicsTextItem(foo,0,parent);
    14. name->setPos(50,0);
    15. name->setFont(QFont("Helvetica [Cronyx]", 12));
    16.  
    17. }
    18. void slotButton::mousePressEvent(QMouseEvent *event)
    19. {
    20. graphic->setPixmap(QPixmap("carouselfinger_on.png"));
    21. }
    22.  
    23. void slotButton::paint(QPainter *painter,const QStyleOptionGraphicsItem *option,QWidget *widget)
    24. {
    25. graphic->paint(painter,option,widget);
    26. }
    27.  
    28. QRectF slotButton::boundingRect() const
    29. {
    30. return graphic->boundingRect();
    31. }
    To copy to clipboard, switch view to plain text mode 
    Attached Images Attached Images
    Attached Files Attached Files

Similar Threads

  1. Replies: 1
    Last Post: 11th July 2012, 13:26
  2. Replies: 7
    Last Post: 1st December 2011, 16:24
  3. Replies: 5
    Last Post: 19th November 2011, 22:05
  4. Will I learn to implement custom SIGNALS?
    By fatecasino in forum Newbie
    Replies: 14
    Last Post: 8th March 2011, 11:57
  5. Replies: 2
    Last Post: 19th February 2009, 19:37

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.