Qt World Summit: Register Today!

Refering to other coordinate systems QGraphicsItems

  • Hello everyone

    I'm learning Qt Creator atm and I encountered a problem I can't really solve on my own. I am building a board game with a GUI with figures and the board field overall and where u can see all movements that were made. Now my problem is that im using QGraphicScene and my own QGraphicItem which is inheriting from QGraphicEllipseItem (these I use for my figures) and if I want to move the figures around but the reference is always to the local coordinate system for example if I'm doing:

    figur.setPos(QPoint p) or figur.moveBy(QPoint p)

    the (0,0) is always in the center of my figure.But I want it to be at the top left of the scene for example. Because of that my boardfield is different for every figure for each figur it always uses their local coordinate system ,so i can't define my boardfields for everyone. I tried mapping and transforming but wasnt too succesful. I just wanna access the scene coordinate system. Thanks for reading and sorry for my bad english I hope my problem was understandable.

  • Moderators

    how do you create (the ellipse) your QGraphicEllipseItem?

  • I use scene->addItem(QGraphicsItem *item)

    *edit: I also use the new operator and set the rect and the pen of course

  • Moderators

    please show the code where you set the rect (and its values)

  • So im making a game that uses 4 figures for each player each player has a different color and the numbers of players can change from 2-4 players(The game is a simple version of Ludo).I'm using two own classes first the matchfield class where I wanna save the positions from each field(a matchfield has around 40 fields where the figures move through similiar to a Ludo gamefield) like I did here my houseFields(here matchfield.H) where the figures start etc.
    So my housefields are declared to the correct coordinate system here it works all fine.The first code below works correctly. The second one where I use ellipse.setPos(qreal ax,qreal ay) doesn't. It uses the coordinate system of the item and i don't want that I still wanna use the scene coordinate system.`So I can use only set my boardgame positions in my matchfield class and use it from every figur position.

    And just for undestanding I'm using an array of *figures ellipse[16] (because 16 is the max amount of figures with 4 players)``

    // First code
    for(int playerCount=0;playerCount<player*4;playerCount++){
            matchfield.ellipse[playerCount]=new figures();
    //Second code. Here is an example where i wanna use the scene coordinate system and not the local coordinate system from the QGraphicsitem
    void MainWindow::on_pushButton_2_clicked()
        int i=ui->doubleSpinBox->value();

Log in to reply