PDA

View Full Version : QGraphicsScene Position Problem



addu
22nd July 2009, 11:05
I added line edit, pushbutton,label to QGraphics View..

But the position of scene is mid of the QGraphicsView..

How do i change the the position of scene in QGraphics View..

I am trying with



QGraphicsScene *scene= new QGraphicsScene(this);
ui->graphicsView->setScene(scene);
ui->graphicsView->setCacheMode(QGraphicsView::CacheBackground);
Ui_Form *m_ui = new Ui_Form;
QWidget *wid = new QWidget;
m_ui->setupUi(wid);
QGraphicsProxyWidget *proxy = new QGraphicsProxyWidget;
QLabel *label = new QLabel;
label->setText("Hello World !!!");
QPushButton *button = new QPushButton;
button->setText("dfgdfgdsg");
QLineEdit *line = new QLineEdit;
line->setFocus();
proxy =scene->addWidget(line);
label->setScaledContents(true);
proxy = scene->addWidget(button);
proxy = scene->addWidget(label);
scene->addRect(QRectF(0, 0, 5, 30))->setBrush(Qt::green);
ui->graphicsView->setScene(scene);


Adavance Thanks

Yuvaraj R

yogeshgokul
22nd July 2009, 11:09
Use:


setAlignment(Qt::Alignment);

addu
22nd July 2009, 11:36
For this one is i have include any thing,

Because i am getting errors. like.



error: expected primary-expression before ')' token..
And one more..

for Qlabel and pushbutton, i am getting background shadow, please look at attached image.


Thanks

Yuvaraj R

wagmare
22nd July 2009, 11:44
error: expected primary-expression before ')' token..
and it will suggest a line number of the file ...
send that line also ... u may miss ; or , or any thing

addu
22nd July 2009, 11:55
ok..

It is showing error at line number 46

Here i post my code.


#include "widget.h"
#include "ui_widget.h"
#include "ui_form.h"
#include<QGraphicsScene>
#include <QGraphicsProxyWidget>
#include <QLabel>
#include <QPushButton>
#include <QLineEdit>
#include <QMessageBox>
QLabel *label,*label1;
QPushButton *button;
QLineEdit *line,*line1;
Widget::Widget(QWidget *parent)
: QWidget(parent), ui(new Ui::WidgetClass)
{
ui->setupUi(this);
QGraphicsScene *scene= new QGraphicsScene(this);
ui->graphicsView->setScene(scene);
ui->graphicsView->setCacheMode(QGraphicsView::CacheBackground);
Ui_Form *m_ui = new Ui_Form;
QWidget *wid = new QWidget;
m_ui->setupUi(wid);
QGraphicsProxyWidget *proxy = new QGraphicsProxyWidget;
QLabel *label = new QLabel;
label->setText("User Name");
QLabel *label1 = new QLabel;
label1->setText("PassWord");
label1->setGeometry(0,40,50,20);
proxy = scene->addWidget(label1);
QPushButton *button = new QPushButton;
button->setText("dfgdfgdsg");
label->setGeometry(0,0,50,20);
QLineEdit *line = new QLineEdit;
line->setGeometry(60,0,100,20);
QLineEdit *line1 = new QLineEdit;
line1->setGeometry(60,40,100,20);
proxy =scene->addWidget(line);
proxy =scene->addWidget(line1);
button->setGeometry(30, 80,50,50);
proxy = scene->addWidget(button);
proxy = scene->addWidget(label);
scene->addRect(QRectF(200, 0, 100, 100))->setBrush(Qt::green);
scene->setFocus();
line->setEnabled(true);
ui->graphicsView->setScene(scene);
ui->graphicsView->setAlignment(Qt::Alignment);
QObject::connect(button,SIGNAL(clicked()),this,SLO T(show_message1()));


}

Widget::~Widget()
{
delete ui;
}
void Widget::show_message1()
{
QMessageBox::information(0,"",QString(line->text()));
}

wagmare
22nd July 2009, 12:01
u have to give any one of this
Qt::AlignLeft or Qt::AlignRight or Qt::AlignHCenter or Qt::AlignJustify

and not Qt::Alignment
change ui->graphicsView->setAlignment(Qt::Alignment);

addu
22nd July 2009, 12:05
Thanks for your reply..


Previously i gave like that only..

It showed the Label, line edit at ccentre left of Graphics view


Any idea ?


Thanks

Yuvaraj R

wagmare
22nd July 2009, 12:11
why you are adding QWidgets like this ...
group the whole lineEdit and QPushButton into single QGroupBox

QVBoxLayout *layoout = new QVBoxLayout;
layoout->addWidget(pushButton);
layoout->addWidget(lineEdit);
groupBox->setLayout(layoout);

and then add the group box to proxy widget

QGraphicsProxyWidget *proxy = scene->addWidget(groupBox);
proxy->setPos(x,y); //position where u want in scene

addu
22nd July 2009, 12:17
Thanks

Have u seen attched image..

I am getting the background show for Qlabel, Qlineedit..


and one more thing ,if use the drived class slot the application crashing,but not for Base class

slots


Thanks

Yuvaraj R

wagmare
22nd July 2009, 12:21
and one more thing ,if use the drived class slot the application crashing,but not for Base class

slots

can u explain more in detail .. i cant get it ..

addu
22nd July 2009, 12:25
In above code if i click the button application is crashing, but if use the Close() slot ,application is not crashing.

If i set the
proxy->setPos(0,0) also it is displaying mid of the Qgraphics View

Sorry for my English


Thanks

Yuvaraj R

wagmare
22nd July 2009, 12:35
also it is displaying mid of the Qgraphics View

no it wont ..
see you should set geometrical value to QGraphicsScene
ex:

QRectF bounds(0, 0, 530, 305);
scene = new QGraphicsScene(bounds, this);

now the scene having a geometry of (0, 0, 530, 305)
now set the proxy item any where between the scene coordinates ..
for ex:
to set in middle
proxy->setPos(265, 153)
in top left
proxy->setPos(0,0)
in top right
proxy->setPos(500, 0)
in bottom right
proxy->setPos(500, 280)

EDIT: check the link
http://doc.trolltech.com/4.3/coordsys.html

addu
22nd July 2009, 12:40
Thanks for your reply

please pardon me, i had set the wrong align ment in UI... That why i didn't work..


my next thing is background shodow for Qlabel, Button.

Thanks

Yuvaraj R

yogeshgokul
22nd July 2009, 12:45
ui->graphicsView->setAlignment(Qt::Alignment);

Thasss cool, :cool::cool:

addu
22nd July 2009, 14:38
Can anybody suggest me..


Why,when adding the Label of push button to QGraphicsView.. It displaying with background

like in attached images..

please help avoid the background..


Thanks

Yuvaraj R