PDA

View Full Version : Output is there..but code doesnt look to be Perfect..!



salmanmanekia
12th June 2008, 17:16
Hi..
I had tried to show a layout of buttons on View..it worked fine,but the return function ( retWidg() ) which i have specified in the main doesnt seems to be necessary..i mean to say is there any other solution by which i can avoid these statement and with it be more cleared in approach...



#include "exclass.h"

#include <QApplication>
#include <QGraphicsScene>

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

exclass *w = new exclass;

QWidget *mainW = new QWidget;
QGraphicsScene scene;
QGraphicsView view;

mainW = w->retWidg();
scene.addWidget(mainW);
scene.addItem(w);

view.setScene(&scene);

view.show();
return a.exec();
}


#ifndef EXCLASS_H
#define EXCLASS_H

#include <QtGui/QWidget>
#include <QGraphicsScene>
#include <QGraphicsView>
#include <QGraphicsItem>
#include <QPainter>
#include <QImage>
#include <QPushButton>
#include <QHBoxLayout>
//#include <QString>
//#include "ui_exclass.h"

class exclass :public QObject, public QGraphicsItem
{
Q_OBJECT

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

QWidget *widg;

exclass(QGraphicsItem *parent = 0);

QWidget *retWidg();

QRectF boundingRect() const;

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

};

#endif // EXCLASS_H


#include "exclass.h"

exclass::exclass(QGraphicsItem *parent):QGraphicsItem(parent)
{

hboxLay = new QHBoxLayout;
playButton = new QPushButton;
stopButton = new QPushButton;
widg = new QWidget;
hboxLay->addWidget(playButton);
hboxLay->addWidget(stopButton);
widg->setLayout(hboxLay);

}


QWidget* exclass::retWidg()
{
return widg;
}

QRectF exclass::boundingRect() const
{

}

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

}

aamer4yu
13th June 2008, 08:50
You could simply add Widget in the scene.

main()
{
QWidget *widget = new QWidget();
// set the layout on widget;
// add play styop buttons to widget;

QScene scene;
scene.addWidget(widget);
QGraphicsView view(&scene);

view.show();
}

salmanmanekia
13th June 2008, 09:04
so what i understand ,you are saying that i should implement everything in the main function,i started the things the same way but then to use drawBackGround() function which is protected i had to develop a class which look like this...i hope you understand..!!:)