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
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 :)
Re: QGraphicsScene...How to have your own format for button,etc..???
QGraphicsItem::setPos() might be helpful as well :)
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 ...;)
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, :D
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
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.
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 ?
Re: QGraphicsScene...How to have your own format for button,etc..???
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 //
Code:
#include "exclass.h"
#include <QApplication>
#include <QGraphicsScene>
int main(int argc, char *argv[])
{
exclass *lay = new exclass;
Horlay *txt = new Horlay;
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 //
Code:
#ifndef EXCLASS_H
#define EXCLASS_H
#include <QtGui/QWidget>
#include <QGraphicsScene>
#include <QGraphicsView>
#include <QGraphicsItem>
#include <QPainter>
#include <QImage>
#include <QPushButton>
#include <QHBoxLayout>
{
public:
};
{
public:
};
#endif // EXCLASS_H
// SOURCE CLASS //
Code:
#include "exclass.h"
{
playButton
->setIcon
(QIcon(":/images/play.JPG"));
stopButton
->setIcon
(QIcon(":/images/stop.JPG"));
}
{
hboxLay->addWidget(playButton);
hboxLay->addWidget(stopButton);
widg->setLayout(hboxLay);
scene->addWidget(widg);
}
QRectF exclass
::boundingRect() const {
}
{
}
{
}
{
str = "salman" ;
scene->addText(str);
}
{
}
QRectF Horlay
::boundingRect() const {
}
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?
Code:
int main(int argc, char **argv){
view.setScene(scene);
QGraphicsProxyWidget *wgtItem = scene->addWidget(wgt);
wgtItem->setPos(100,100);
txtItem->setPos(200, 300);
view.show();
return app.exec();
}
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...