[Qt-interest] problem with QGraphicsView

Fred Duarte fred.duarte at openbossa.org
Thu Jun 25 18:09:40 CEST 2009


You should set the Position of the Pixmap after adding it to the scene.

By default, QGraphicsView positions all items at the (0, 0) point.

You can use the *setPos*( QPointF ) method or the *moveBy*( qreal , qreal )
method. The first
sets the Position of the Item on the scene and the second moves the item
from the current
position by a given offset. Both are methods from *QGraphicsItem* class.

Just pass the center point of the QGraphicsView object to one of these
methods. In the case
of the *moveBy* method, you need to separate the *X* and *Y* coordinates in
2 parameters.

*Example:*
...
currItm = scene->addPixmap(myPix);

QPointF center(400.0, 300.0);

currItem->setPos(center);
...

On Thu, Jun 25, 2009 at 6:07 AM, Sajauddin Shariff Mohammad <
shariff at mic.co.in> wrote:

> Dear Friends,
> Iam new to graphics programming in QT. I want to show the image selected by
> the user in graphics view widget. Ican show the image but a small problem
> with it.
>  My .h and .CPP files are as follows
>
> .H file
>
> class myWidget : public QWidget, Ui_Form
> {
>    Q_OBJECT
>
>    public:
>
>    QPixmap *pix;
>    QGraphicsScene *scene;
>    QString imgFilename;
>    QGraphicsItem *prvItm, *currItm;
>
>    myWidget(QWidget *parent = 0);
>    ~myWidget();
>    public slots:
>    void on_browseButton_clicked();
>    void on_showButton_clicked();
> };
>
> .cpp file
> myWidget::myWidget(QWidget *parent):QWidget(parent)
> {
>    pix = new QPixmap();
>    scene = new QGraphicsScene();
>    currItm = NULL;
>    setupUi(this);
>    graphicsView->setScene(scene);
>    graphicsView->setViewportUpdateMode(QGraphicsView::FullViewportUpdate);
>    graphicsView->setAlignment(Qt::AlignCenter);
> }
>
> myWidget::~myWidget()
> {
> }
>
> void myWidget::on_browseButton_clicked()
> {
>    QString filename;
>    filename = QFileDialog::getOpenFileName(this, tr("Select Image File"),
> "/data", tr("Images (*.bmp *.png *.jpg)"));
>    browseLE->setText(filename);
> }
>
> void myWidget::on_showButton_clicked()
> {
>    QPixmap myPix;
>    imgFilename = browseLE->text();
>    myPix.fill();
>    myPix.load(imgFilename);
>    if(currItm!=NULL)  scene->removeItem(currItm);
>    currItm = scene->addPixmap(myPix);
> }
>
> The image that is being displayed in graphics view is not alligned at the
> center.
>
> Pleas eguide me what should I do so that the image is shown at thecenter of
> the graphics view.
>
>
> Regards,
> Sajauddin Shariff
> shariff at mic.co.in
> +91-998-904-5546
> Confidentiality NoticeThe information contained in this Electronic Message
> and any ATTACHMENTs to this message are intended for the EXCLUSIVE USE of
> the ADDRESSEE(s) and may contain CONFIDENTIAL or PRIVILEGED information. If
> you are not the intended recipient, please notify the SENDER at MIC or
> itsupport at mic.co.in immediately and DESTROY all copies of this message and
> any attachments.
>
> _______________________________________________
> Qt-interest mailing list
> Qt-interest at trolltech.com
> http://lists.trolltech.com/mailman/listinfo/qt-interest
>
>


-- 
Frederico Simas Duarte
MLS - Mobile Linux Software
INDT - Nokia Institute

http://fredyduarte.net/blog/
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20090625/e2d3d718/attachment.html 


More information about the Qt-interest-old mailing list