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:
#ifndef CAROUSELWIDGET_H
#define CAROUSELWIDGET_H
#include <QWidget>
#include <QGraphicsView>
#include <QGraphicsScene>
#include <QGraphicsPixmapItem>
#include <QPixmap>
#include <QGraphicsItem>
class carouselWidget
: public QWidget{
Q_OBJECT
public:
explicit carouselWidget
(QWidget *parent
= 0);
protected:
// void mousePressEvent(QMouseEvent *event);
signals:
public slots:
};
#endif // CAROUSELWIDGET_H
#ifndef CAROUSELWIDGET_H
#define CAROUSELWIDGET_H
#include <QWidget>
#include <QGraphicsView>
#include <QGraphicsScene>
#include <QGraphicsPixmapItem>
#include <QPixmap>
#include <QGraphicsItem>
class carouselWidget : public QWidget
{
Q_OBJECT
QGraphicsPixmapItem *item;
public:
explicit carouselWidget(QWidget *parent = 0);
QGraphicsView * carousel_view;
QGraphicsScene * carousel_scene;
protected:
// void mousePressEvent(QMouseEvent *event);
signals:
public slots:
};
#endif // CAROUSELWIDGET_H
To copy to clipboard, switch view to plain text mode
carouselwidget.cpp:
#include "carouselwidget.h"
#include "slotbutton.h"
carouselWidget
::carouselWidget(QWidget *parent
) :{
carousel_view->setScene(carousel_scene);
//item = new QGraphicsPixmapItem(QPixmap("carouselfinger.png"),0, carousel_scene);
//item->setFlag(QGraphicsItem::ItemIsSelectable,true);
//item->rotate(30);
//item->setPos(50,50);
carousel_view->show();
slotButton foo(carousel_scene);
foo.setPos(50,50);
foo.rotate(30);
//foo.setScene(carousel_scene);
//carousel_scene = new QGraphicsScene(this);
//carousel_scene->addText("Hello World!");
//carousel_view = new QGraphicsView(carousel_scene);
//carousel_view->show();
}
//void carouselWidget::mousePressEvent(QMouseEvent *event)
//{
// item->setPixmap(QPixmap("carouselfinger_on.png"));
//}
#include "carouselwidget.h"
#include "slotbutton.h"
carouselWidget::carouselWidget(QWidget *parent) :
QWidget(parent)
{
carousel_scene = new QGraphicsScene(0,0,300,300,this);
carousel_view = new QGraphicsView(this);
carousel_view->setScene(carousel_scene);
//item = new QGraphicsPixmapItem(QPixmap("carouselfinger.png"),0, carousel_scene);
//item->setFlag(QGraphicsItem::ItemIsSelectable,true);
//item->rotate(30);
//item->setPos(50,50);
carousel_view->show();
slotButton foo(carousel_scene);
foo.setPos(50,50);
foo.rotate(30);
//foo.setScene(carousel_scene);
//carousel_scene = new QGraphicsScene(this);
//carousel_scene->addText("Hello World!");
//carousel_view = new QGraphicsView(carousel_scene);
//carousel_view->show();
}
//void carouselWidget::mousePressEvent(QMouseEvent *event)
//{
// item->setPixmap(QPixmap("carouselfinger_on.png"));
//}
To copy to clipboard, switch view to plain text mode
slotButton.h:
#ifndef SLOTBUTTON_H
#define SLOTBUTTON_H
#include <QWidget>
#include <QGraphicsPixmapItem>
#include <QGraphicsItem>
#include <QPainter>
#include <QStyleOptionGraphicsItem>
#include <QLabel>
#include <QString>
#include <QFont>
{
// Q_OBJECT
public:
protected:
signals:
public slots:
};
#endif // SLOTBUTTON_H
#ifndef SLOTBUTTON_H
#define SLOTBUTTON_H
#include <QWidget>
#include <QGraphicsPixmapItem>
#include <QGraphicsItem>
#include <QPainter>
#include <QStyleOptionGraphicsItem>
#include <QLabel>
#include <QString>
#include <QFont>
class slotButton : public QGraphicsItem
{
QGraphicsPixmapItem *graphic;
QGraphicsTextItem * num;
QGraphicsTextItem * name;
// Q_OBJECT
public:
explicit slotButton(QGraphicsScene *parent);
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option,QWidget *widget);
QRectF boundingRect() const;
protected:
void mousePressEvent(QMouseEvent *event);
signals:
public slots:
};
#endif // SLOTBUTTON_H
To copy to clipboard, switch view to plain text mode
SlotButton.cpp:
#include "slotbutton.h"
{
foo = "1";
num->setPos(30, 0);
num->setPlainText(foo);
num
->setFont
(QFont("Helvetica [Cronyx]",
12));
foo = "Quartz";
name->setPos(50,0);
name
->setFont
(QFont("Helvetica [Cronyx]",
12));
}
{
graphic
->setPixmap
(QPixmap("carouselfinger_on.png"));
}
{
graphic->paint(painter,option,widget);
}
QRectF slotButton
::boundingRect() const {
return graphic->boundingRect();
}
#include "slotbutton.h"
slotButton::slotButton(QGraphicsScene *parent) :
QGraphicsItem()
{
QString foo;
graphic = new QGraphicsPixmapItem(QPixmap("carouselfinger.png"),0, parent);
num = new QGraphicsTextItem(foo,0,parent);
foo = "1";
num->setPos(30, 0);
num->setPlainText(foo);
num->setFont(QFont("Helvetica [Cronyx]", 12));
foo = "Quartz";
name = new QGraphicsTextItem(foo,0,parent);
name->setPos(50,0);
name->setFont(QFont("Helvetica [Cronyx]", 12));
}
void slotButton::mousePressEvent(QMouseEvent *event)
{
graphic->setPixmap(QPixmap("carouselfinger_on.png"));
}
void slotButton::paint(QPainter *painter,const QStyleOptionGraphicsItem *option,QWidget *widget)
{
graphic->paint(painter,option,widget);
}
QRectF slotButton::boundingRect() const
{
return graphic->boundingRect();
}
To copy to clipboard, switch view to plain text mode
Bookmarks