PDA

View Full Version : QGraphicsScene...How to have your own format for button,etc..???



salmanmanekia
5th June 2008, 09:27
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

aamer4yu
5th June 2008, 09:38
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 :)

wysota
5th June 2008, 10:01
QGraphicsItem::setPos() might be helpful as well :)

salmanmanekia
5th June 2008, 13:51
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 ...;)

aamer4yu
5th June 2008, 15:45
Why dou have to worry about implementation ??
YOu just need to create a graphics item, set its z-order, and setPos. thats it, :D

salmanmanekia
12th June 2008, 17:04
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

wysota
12th June 2008, 19:48
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.

salmanmanekia
13th June 2008, 07:46
'I suggest viewing Andreas Hanssen's presentation about QGraphicsView from the last DevDays'...where can i find it ?

aamer4yu
13th June 2008, 08:37
I guess here (http://labs.trolltech.com/page/Projects/DevDays/DevDays2007) :)

salmanmanekia
13th June 2008, 12:20
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 //


#include "exclass.h"
#include <QApplication>
#include <QGraphicsScene>

int main(int argc, char *argv[])
{
QApplication a(argc, argv);


exclass *lay = new exclass;
Horlay *txt = new Horlay;

QGraphicsScene scene;
QGraphicsView view;


lay->Widgt(&scene);
txt->Strng(&scene);

lay->setPos(5,5);
txt->setPos(500,500);

view.setBackgroundBrush(QPixmap(":/images/background.JPG"));

view.setScene(&scene);

view.show();

return a.exec();

}

// HEADER CLASS //


#ifndef EXCLASS_H
#define EXCLASS_H

#include <QtGui/QWidget>
#include <QGraphicsScene>
#include <QGraphicsView>
#include <QGraphicsItem>
#include <QPainter>
#include <QImage>
#include <QPushButton>
#include <QHBoxLayout>

class exclass : public QGraphicsItem
{

public:
QPushButton *stopButton;
QPushButton *playButton;
QHBoxLayout *hboxLay;

QWidget *widg;

exclass(QGraphicsItem *parent = 0);

void Widgt(QGraphicsScene *);

QRectF boundingRect() const;
void paint(QPainter *painter,const QStyleOptionGraphicsItem *option,QWidget *widget);

};

class Horlay :public QGraphicsItem
{
public:

QString str;

Horlay(QGraphicsItem *parent = 0);

void Strng(QGraphicsScene *);

QRectF boundingRect() const;
void paint(QPainter *painter,const QStyleOptionGraphicsItem *option,QWidget *widget);

};

#endif // EXCLASS_H

// SOURCE CLASS //


#include "exclass.h"

exclass::exclass(QGraphicsItem *parent):QGraphicsItem(parent)
{
hboxLay = new QHBoxLayout;
playButton = new QPushButton;
stopButton = new QPushButton;
widg = new QWidget;
playButton->setIcon(QIcon(":/images/play.JPG"));
stopButton->setIcon(QIcon(":/images/stop.JPG"));
}

void exclass::Widgt(QGraphicsScene *scene)
{
hboxLay->addWidget(playButton);
hboxLay->addWidget(stopButton);
widg->setLayout(hboxLay);
scene->addWidget(widg);
}

QRectF exclass::boundingRect() const
{

}

void exclass::paint(QPainter *painter,const QStyleOptionGraphicsItem *option,QWidget *widget)
{

}


Horlay::Horlay(QGraphicsItem *parent):QGraphicsItem(parent)
{

}

void Horlay::Strng(QGraphicsScene *scene)
{
str = "salman" ;
scene->addText(str);
}

void Horlay::paint(QPainter *painter,const QStyleOptionGraphicsItem *option,QWidget *widget)
{

}

QRectF Horlay::boundingRect() const
{

}

wysota
16th June 2008, 07:21
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?

int main(int argc, char **argv){
QApplication app(argc, argv);
QGraphicsView view;
QGraphicsScene *scene = new QGraphicsScene(&view);
view.setScene(scene);
QWidget *wgt = new QWidget;
QHBoxLayout *l = new QHBoxLayout(wgt);
l->addWidget(new QPushButton("play"));
l->addWidget(new QPushButton("stop"));
QGraphicsProxyWidget *wgtItem = scene->addWidget(wgt);
QGraphicsTextItem *txtItem = scene->addText("salman");
wgtItem->setPos(100,100);
txtItem->setPos(200, 300);
view.show();
return app.exec();
}

salmanmanekia
16th June 2008, 08:42
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...