You are not logged in.

Dear visitor, welcome to QtForum.org. If this is your first visit here, please read the Help. It explains in detail how this page works. To use all features of this page, you should consider registering. Please use the registration form, to register here or read more information about the registration process. If you are already registered, please login here.

1

Wednesday, December 19th 2007, 9:31am

Problem in displaying text at desired position

Hi,

I have a problem with displaying the text on QGraphicScene.

I have used the following code for this purpose:

Source code

1
2
3
4
5
QGraphicsTextItem *item;
qDebug()<<item->scenePos(); // It gives (0,0) 
item->setPos(20,20);
item->mapToScene(item->pos());
qDebug() << item->scenePos(); //Now it gives (20,20)


But still the text is drawing at the original position .........

Can any body help me how to change the coordinates of text so the It can be drawn at desired position.

Thanks

2

Wednesday, December 19th 2007, 9:48am

RE: Problem in displaying text at desired position

more complete code, please
Nicolas

3

Wednesday, December 19th 2007, 10:07am

RE: Problem in displaying text at desired position

Thanks for your help !

Actually what I am trying to do is that I want that Text should be displayed with image on QGraphicScene.......

I am working on a project that show images with label on them.

QGraphicsTextItem *item = new QGraphicsTextItem(text->label()); //text->label() function return the label for that image

Actually a for loop is run in which every time new label is got from text->label() and displayed with that particular image.

It is difficult to make understandable but in layman language I'll said that if there are 10 images then 10 different label will displayed

corresponding to each image.

Then I use the following above code and map ot to Scene. After that I add it to group as follow:

Source code

1
QGraphicsItem *show=group->addToGroup(item);


This value is return to QGraphicsItem and after that add to QGraphicScene

Source code

1
2
QGraphicsScene    *scene;
scene->addItem(show);


Text are displaying properly but not according to desired location....

Thanks

4

Wednesday, December 19th 2007, 12:16pm

RE: Problem in displaying text at desired position

if I well understand ????

try to create text items with as parent the associated image item
then move them absolute (ie:relative to the parent item)

like this

Source code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <QtGui/QApplication>

#include <QtGui/QGraphicsItem>
#include <QtGui/QGraphicsPixmapItem>
#include <QtGui/QGraphicsTextItem>
#include <QtGui/QGraphicsScene>
#include <QtGui/QGraphicsView>

#include <QtGui/QPixmap>

int main( int argc, char * * argv )
{
        QApplication      app( argc, argv );
        QGraphicsScene    scene;
        QGraphicsView     view( &scene );
        QString           imageName( "path/to/your/image" );
        QPixmap           pixmap( imageName );

        QGraphicsPixmapItem pixmapItem( pixmap );
        scene.addItem( &pixmapItem );
        QGraphicsTextItem text( imageName, &pixmapItem );

        pixmapItem.setPos( 100, 100 );
        text.setPos( pixmap.width() / 2 - text.boundingRect().width() / 2, pixmap.height() );

        view.show();

        return( app.exec() );
}
Nicolas

This post has been edited 4 times, last edit by "Nicolas SOUCHON" (Dec 19th 2007, 6:03pm)