Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

new class didn't work correcltly



  • Hello guys , i created a new class (Personnage ) which contains a QLabel , and in the Qlabel there are a Pixmap (the green ball ) after this i called this object from another program (it work ) and i created a function in this new class to move my object (i used move which come from QLabel ) but it didn't work , can someone help me please , thanks 1_1550064880338_Capture d’écran 2019-02-13 à 14.19.53.png

    0_1550064880338_Capture d’écran 2019-02-13 à 14.19.21.png



  • @Zunneh
    I shall be surprised if someone can discern exactly what you are saying here....

    after this i called this object from another program (it work )

    What does that mean? You can't call objects from other programs....

    i created a function in this new class to move my object (i used move which come from QLabel )

    So your code does not move the object (Personnage), it just moves the label within the Personnage widget.... To be specific, your test->move(0, m_y) moves the QLabel within your widget, while the this->move(60, 0) moves your Personnage, which one were you talking about...?

    but it didn't work

    What does that mean...?



  • i created the main program with Qt designer and after this i added a class in this project which contains the green ball , now from the main program (look at the previous screen shot ) i created the object (the green ball whose name is bat) and i want to control this object from main program , i tried but i can' move the object from the main program (i can move it only from the constructor of the class with test->move , me i want to move all the new object Personnage for example when i write bat->movetoUp() it move the object to Up

    0_1550066745903_Capture d’écran 2019-02-13 à 15.00.03.png ")



  • bat is local to the constructor so can't be referenced outside it. move the Personnage* bat; definition in the header (.h) and in the constructor just put bat = new Personnage(this);. Now you can use bat everywhere in your MainWindow.

    P.S.
    The constructor of Personnage does some weird stuff, basically the label inside Personnage and Personnage have completely distinct lives and that's unusual to say the least



  • i find the solutions , my problem that in the first i put QLabel who is inside Personnage the parent , now i put my Personnage the parent so i can move the personnage not the Qlabel inside the personnage, i added the command this->setParent(parent)
    by the way thank you guys for the help

    0_1550068753833_Capture d’écran 2019-02-13 à 15.36.50.png


  • Lifetime Qt Champion

    @Zunneh There is no need to call setParent, just call base class constructor and pass parent to it:

    Personnage::Personnage(QWidget* parent): QWidget(parent), m_x(0), m_y(0)
    {
    

Log in to reply