PDA

View Full Version : Moving items in a scene via slots



ventura8
30th August 2011, 11:47
i have a class named display that creates a scene that is later added to a graphicsview
the display class has two slots, i want to move, hide , set text to items using the slots. the .h and .cpp for the display class is

as you can see i tried some move functions in the slots but the items do not move. what am i doing wrong?


#ifndef DISPLAY_H #define DISPLAY_H
#include <QGraphicsView>
#include <QPushButton>
#include <QGraphicsRectItem>
#include <QGraphicsBlurEffect>
class display
: public QObject
{
Q_OBJECT
public:
display();
QGraphicsScene *scene;
QPushButton *buttonExit;
QPushButton *buttonCalibrate;
QGraphicsRectItem *messagerect;
QString orientation;
QString anglestring;
QGraphicsSimpleTextItem *angleitem1;
QGraphicsRectItem *anglerect1;
public slots:
void setOrientation(QString neworientation);
void setAngles(double newanglexy,double newanglexz,double newangleyz);
private:
double anglexy ;
double anglexz ;
double angleyz ;
};
#endif // DISPLAY_H



#include "display.h" #include <QtGui/QApplication>
#include <QDebug>
#include <QDesktopWidget>
display::display()
{
anglexy = 0;
anglexz = 0;
angleyz = 0;
anglestring = "0";
const int screenwidth = QApplication::desktop()->width();
const int screenheight = QApplication::desktop()->height();
const int buttonheight = 60;
const int tubebackwidth = 300;
const int tubebackheight = 100;
qDebug() << "width: "+ QString::number(screenwidth) +" height: "+ QString::number(screenheight);
scene = new QGraphicsScene();
scene->setSceneRect(0,0,screenwidth,screenheight);
scene->setBackgroundBrush(Qt::black);
QGraphicsRectItem *tubeback = new QGraphicsRectItem(0, scene );
tubeback->setRect( screenwidth/2-tubebackwidth/2,screenheight/2-tubebackheight/2-buttonheight/2, tubebackwidth, tubebackheight );
//tubeback1->setFlag( QGraphicsItem::ItemIsMovable );
tubeback->setBrush(Qt::yellow);
tubeback->setGraphicsEffect(new QGraphicsBlurEffect());
QPen pen1;
pen1.setColor(Qt::black);
pen1.setWidth(10);
pen1.setStyle(Qt::DotLine);
QGraphicsEllipseItem *tubefront = new QGraphicsEllipseItem(0, scene );
tubefront->setRect( screenwidth/2-40,screenheight/2-tubebackheight/2-buttonheight/2+5, 80, tubebackheight-50 );
tubefront->setFlag( QGraphicsItem::ItemIsMovable );
tubefront->setBrush(Qt::white);
tubefront->setGraphicsEffect(new QGraphicsOpacityEffect());
tubefront->setGraphicsEffect(new QGraphicsBlurEffect());
QGraphicsLineItem *line1 = new QGraphicsLineItem(0,scene);
line1->setLine(screenwidth/2-tubebackwidth/6,screenheight/2-tubebackheight/2-buttonheight/2, screenwidth/2-tubebackwidth/6,screenheight/2-tubebackheight/2-buttonheight/2+tubebackheight);
line1->setPen(pen1);
QGraphicsLineItem *line2 = new QGraphicsLineItem(0,scene);
line2->setLine(screenwidth/2+tubebackwidth/6,screenheight/2-tubebackheight/2-buttonheight/2, screenwidth/2+tubebackwidth/6,screenheight/2-tubebackheight/2-buttonheight/2+tubebackheight);
line2->setPen(pen1);
//se intiaza un buton cu un apel calificat catre functia tr() pentru a traduce textul butonului
//: This name refers to the Calibrate Button's name
QPushButton *buttonCalibrate = new QPushButton(QObject::tr("Calibrate"));
#ifdef Q_WS_MAEMO
buttonCalibrate->setMaximumSize(screenwidth/2,buttonheight);
//#elif defined(Q_WS_MAEMO_5)
#else Q_OS_SYMBIAN
buttonCalibrate->setMaximumSize(screenwidth/2,buttonheight);
#endif
//: This name refers to the Exit Button's name
QPushButton *buttonExit = new QPushButton(QObject::tr("Exit"));
#ifdef Q_WS_MAEMO
buttonExit->setMaximumSize(screenwidth/2,buttonheight);
#else Q_OS_SYMBIAN
buttonExit->setMaximumSize(screenwidth/2,buttonheight);
#endif
scene->addWidget(buttonCalibrate);
scene->addWidget(buttonExit);
buttonCalibrate->move(0,screenheight-buttonheight);
buttonExit->move(screenwidth/2,screenheight-buttonheight);
QGraphicsRectItem *messagerect= new QGraphicsRectItem(0, scene );
messagerect->setRect( 0,0, 280, 80 );
messagerect->setFlag( QGraphicsItem::ItemIsMovable );
messagerect->setBrush(Qt::black);
QGraphicsSimpleTextItem *messageitem = new QGraphicsSimpleTextItem (messagerect );
messageitem->setText(QObject::tr(" Keep the phone steady and level\n about to calibrate the phone \n for this orientation..."));
messageitem->setBrush(Qt::white);
messagerect->moveBy(screenwidth/2-140,screenheight/2-buttonheight/2-40);
messagerect->hide();
anglerect1= new QGraphicsRectItem(0 , scene );
anglerect1->setRect( 0,0, 30, 30 );
anglerect1->setFlag( QGraphicsItem::ItemIsMovable );
anglerect1->setBrush(Qt::black);
angleitem1 = new QGraphicsSimpleTextItem (anglerect1 );
angleitem1->setText("anglestring""");
angleitem1->setBrush(Qt::white);
anglerect1->moveBy(screenwidth/2-5,screenheight/2-buttonheight/2-100);
//anglerect1->hide();
//The connect() method connects a signal to the slot. When we click on the quit button, the clicked() signal is generated. The qApp is a global pointer to the application object. It is defined in the <QApplication> header file. The quit() method is called, when the clicked signal is emitted.
QObject::connect(buttonExit, SIGNAL(clicked()), qApp, SLOT(quit()));
}
void display::setOrientation(QString neworientation)
{
if (neworientation != orientation) {
orientation = neworientation;
qDebug() << "display orientation: " << orientation;
// buttonCalibrate->move(0,0);

}
}
void display::setAngles(double newanglexy,double newanglexz,double newangleyz)
{
if (newanglexy != anglexy) {
anglexy = newanglexy;
anglestring.sprintf("%.0f", anglexy);

anglerect1->hide();
angleitem1->setText(anglestring);
qDebug() << "anglerecived "<<anglestring;
return anglerect1->hide();
return scene->update();
}
}

luisvaldes88
30th August 2011, 12:50
Hi.

I did not see you connecting this slots to any signal in the application.
You should create for example, two signals

signals:
void anglesChanged(double newanglexy,double newanglexz,double newangleyz)
void orientationChanged(QString neworientation)

and then:

connect(display, SIGNAL(anglesChanged(double,double,double)
this, SLOT(setAngles(double,double,double))))

connect(display, SIGNAL(orientationChanged(QString)
this, SLOT(setOrientationChanged(QString))));

You would probably emit your signals inside another slot connected to some ApplyChanges or something because I dont know where are you getting the values for the angles or the string for the orientation.

ventura8
30th August 2011, 15:16
i did it eventualy. thanks