PDA

View Full Version : QPaint Class and QGraphicsScene ....I am stuck !!



salmanmanekia
28th May 2008, 08:02
Hi,

I have written a program to show some buttons and use QGraphicsscene and paint class but when i run this program the output is only a 'WHITE BLANK SCREEN '

Below is the implementation of this class and main function,any help/suggestion would be welcome.....pls give it a look !!



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

paintClass paint;
paint.addScene();
paint.setIcons();
paint.drawBGround();
paint.show();


return app.exec();
}





#ifndef PAINTCLASS_H
#define PAINTCLASS_H

#include <QGraphicsScene>
#include <QGraphicsView>

#include <QGraphicsItem>
#include <QGraphicsEllipseItem>

#include <QCursor>
#include <QRectF>

#include <QPushButton>
#include <QGraphicsProxyWidget>

#include <QIcon>

#include <QVBoxLayout>

#include <QPainter>
#include <QWidget>

class paintClass:public QPainter,public QGraphicsView
{
public:
QRectF r1;
QRectF r2;
QGraphicsItem *parent;
QPainter painter;
QGraphicsScene scene;
QWidget *widg ;
QPushButton *stopButton;
QPushButton *playButton;
QHBoxLayout *hLayout;
QGraphicsProxyWidget *proxy;

paintClass(QGraphicsView *parent = 0);
void drawBGround();
void addScene();
void setIcons();
};

#endif






#include "paintClass.h"

paintClass::paintClass(QGraphicsView *parent):QGraphicsView(parent)
{
r1.setRect(100,200,100,160);
r2.setRect(0,0,300,300);
widg = new QWidget;
stopButton = new QPushButton;
playButton = new QPushButton;
hLayout = new QHBoxLayout;
}

void paintClass::drawBGround()
{
painter.setPen(Qt::black);
scene.addText("Hello, world!");
scene.setBackgroundBrush(Qt::blue);
}

void paintClass::addScene()
{
scene.addText("Hello, world!");

hLayout->addWidget(stopButton);
hLayout->addWidget(playButton);
widg->setLayout(hLayout);
scene.addWidget(widg);
}

void paintClass::setIcons()
{
playButton->setIcon(QIcon("./pics/play.JPG"));
stopButton->setIcon(QIcon("./pics/stop.JPG"));

QGraphicsView view(&scene);
}

jpn
28th May 2008, 16:23
The code above is not very well structured. It almost looks like you are randomly trying things. You should start with going through the Qt tutorial. Then, you can take a look at graphics view examples.

Brandybuck
28th May 2008, 20:15
There is a new Graphics View webcast from ICS Network. It is available for viewing online, or video download. You can find it at: <http://www.ics.com/icsnetwork/>. There is also a brand new thread for the webcast here at QtCenter: <http://www.qtcentre.org/forum/f-icsnetwork-25.html>.

This is a good place to start learning about Qt's Graphics View framework, starting with a simple "hello world" program and advancing to animation and optimization.

salmanmanekia
29th May 2008, 10:53
Hi..
thanks for the reply ..i have just starting streaming the video..i hope it would be helpful...any ways ,the problem with this code is more with the class structure i think (as mentioned by jpn ),initialy i was trying these things in the main function and all was working nice but then i had to use QT'S drawBackground() function which is virtual and protected so i had to create a class in which i could be using it...so...the code in main which looks like this works fine...


#include <QApplication>
#include <QGraphicsScene>
#include <QGraphicsView>

#include <QGraphicsItem>
#include <QGraphicsEllipseItem>

#include <QCursor>
#include <QRectF>

#include <QPushButton>
#include <QGraphicsProxyWidget>

#include <QIcon>

#include <QVBoxLayout>

#include <QPainter>

#include "paintClass.h"

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

QGraphicsScene scene;

const QRectF r1(100,200,100,160);
const QRectF r2(0,0,300,300);


QGraphicsItem *parent = 0;
QGraphicsEllipseItem ellipseItem(r1,parent);
ellipseItem.setFlag(QGraphicsItem::ItemIsFocusable ,true);
ellipseItem.setSelected(true);

ellipseItem.setVisible(true);

scene.addItem(&ellipseItem);

scene.addText("Hello, world!");

QWidget *widg = new QWidget;
QPushButton *stopButton = new QPushButton;
stopButton->setIcon(QIcon("C:/pics/stop.JPG"));

QPushButton *playButton = new QPushButton;
playButton->setIcon(QIcon("C:/pics/play.JPG"));

QHBoxLayout *hLayout = new QHBoxLayout;
hLayout->addWidget(stopButton);
hLayout->addWidget(playButton);
widg->setLayout(hLayout);
QGraphicsProxyWidget *proxy = scene.addWidget(widg);

paintClass *paint ;
paint->drawBGround(&scene);

QGraphicsView view(&scene);
view.show();

return app.exec();
}
..and in my first post the code is almost same except that (i think so )the class is not properly called...any advice would be welcomed...thanks again ...!!1

jpn
29th May 2008, 10:58
the code in main which looks like this works fine...


paintClass *paint ;
paint->drawBGround(&scene);


Are you sure? :)

salmanmanekia
29th May 2008, 11:00
sorry man ...i forgot to comment it...!!...you know , for now there is no paintClass ..so ...
// 1. paintClass *paint ;
// 2. paint->drawBGround(&scene);
and everything works fine....:)

jpn
30th May 2008, 14:23
So, have you looked at graphics view examples (http://doc.trolltech.com/4.4/examples.html#graphics-view) and did you see the webcast as proposed? It makes no sense inherit something from QPainter and QGraphicsView. Also, writing something like:


{
...
QGraphicsView view(&scene);
}

has no effect at all. The QGraphicsView object gets immediately destructed, according to normal C++ rules.

I would recommend taking one of the graphics view examples as a base. That would give you a properly constructed base where you can start adding your own things.

salmanmanekia
30th May 2008, 15:04
yes i have taken look at webcast ....what i think is that ,that i can understand the graphic related api in qt,the problem lies in how to manage classes ,in this case its graphics ...it could have been a network related api and i would have the same problem...
still i understand that this statement executes and ends at the same time...so no effect at all
1. {
2. ...
3. QGraphicsView view(&scene);
4. }

can you guide me to some good resource/tutorial for the class heirachy like how inheritance works in qt ...also if you can pinpoint the wrong things/concept which i have in my code ,so that i can look into solution to that..!!!!..because i am still not crystal clear that wht the problem is what exactly is the way to solve it...thanks again....