@BogBel

Как реализовать вращение элемента внутри QGraphicsScene?

Пишу тетрис, в коде очень много костылей и велосипедов, но в процессе выполнения , столкнулся с проблемой вращения элементов.
Собственный класс фигуры.
class enemy :public QObject, public QGraphicsRectItem, public QTransform//, public QGraphicsPolygonItem
{
    Q_OBJECT
private:
    QTimer *timer;
    QTransform ThisTransform=transform();
public:
    enemy();

    void keyPressEvent(QKeyEvent *event);
    void drawFigureRect();
    void drawFigureArrow();
    void drawFigureLine();
    void drawFigureL();
    
public slots:
    bool moving();
};

и cpp файл в котором это все реализовано

enemy::enemy()
{

    int a=rand()%3;
    switch (a)
    {
        case 0:this->setBrush(QBrush(Qt::green));
        this->setPen(QPen(Qt::gray));
        break;
    case 1:this->setBrush(QBrush(Qt::blue));
        this->setPen(QPen(Qt::gray));
        break;
    case 2:this->setBrush(QBrush(Qt::yellow));
        this->setPen(QPen(Qt::gray));
        break;

    }
    int b=rand()%2;
    switch(b)
    {
    case 0: this->drawFigureLine();
        this->setPos(0,0);
        break;
    case 1: this->drawFigureRect();
        this->setPos(0,0);
        break;
    }

    //this->setPos(scene()->width()/2,10);
   // this->setRect(0,0,40,40);
   // this->setPos(scene()->width()/2,10);
   // scene()->addItem(this);
    this->setFlag(QGraphicsItem::ItemIsFocusable);
    this->setFocus();
    qDebug()<<"New fcn enemy";
    timer=new QTimer();
    //qDebug()<<timer->stop();
    QObject::connect(timer,SIGNAL(timeout()),this,SLOT(moving()));
    //bool a=moving();
   // if(a)
    //timer->stop();
    //QObject::connect(timer,SIGNAL(timeout()),timer,SLOT(stop());
     timer->start(80);




}

void enemy::keyPressEvent(QKeyEvent *event)
{
    if(event->key()==Qt::Key_Up)
    {

        this->setTransformOriginPoint(this->rect().width()/2,this->rect().height()/2);
        ThisTransform.rotate(90);
        this->setTransform(ThisTransform);
    }
    if(event->key()==Qt::Key_Down)
    {

        qDebug()<<"rotation";
    }
    if(event->key()==Qt::Key_Left && pos().x()>0 && scene()->itemAt(this->x()-1,this->y()+rect().height(),QTransform())==0)
    {
        setPos(x()-30,y());
       qDebug()<<"yeah";
    }
    else if(event->key()==Qt::Key_Right && pos().x()+this->rect().width()<scene()->width() && scene()->itemAt(this->x()+rect().width()+1,this->y(),QTransform())==0)
    {
        setPos(x()+30,y());
        qDebug()<<"yeahh";
    }

}

void enemy::drawFigureRect()
{
this->setRect(0,0,60,60);
}

void enemy::drawFigureArrow()
{
//this->setPolygon(QPolygon(0,0,20,20,40,40,20,60));

}

void enemy::drawFigureLine()
{
    this->setRect(0,0,30,120);
}

void enemy::drawFigureL()
{

}

bool enemy::checkColliding(int x, int y)
{

    if(scene()->itemAt(x,y+this->rect().height()+1,QTransform())!=0 ||scene()->itemAt(x-1,y,QTransform())!=0||scene()->itemAt(x+this->rect().width()+1,y,QTransform())!=0 )
    {
        qDebug()<<1;
        return false;

    }
    qDebug()<<2;
    return true;


}

bool enemy::moving()
{

    QList<QGraphicsItem *> colliding=collidingItems();
    for(int i=0,n=colliding.size();i<n;i++)
    {
        if(typeid(*(colliding[i]))==typeid(enemy) && colliding[i]->x()==this->x())
     
            timer->stop();
          
           this->clearFocus();
           this->blockSignals(true);
           enemy *obj=new enemy();
           this->scene()->addItem(obj);
            return false;
        }
    }
    int a=this->rect().height();
    int h=this->scene()->height();
    //qDebug()<<h;
    if(this->y()+a<h && scene()->itemAt(this->x()+1,this->y()+rect().height()+1,QTransform())==0){
        setPos(this->x(),this->y()+5);


    }
    else
    {
        timer->stop();
       //this->killTimer(1000);
       this->clearFocus();
       this->blockSignals(true);
       enemy *obj=new enemy();
       this->scene()->addItem(obj);

       // delete this;
        
    }
}
а именно вот это:

void enemy::keyPressEvent(QKeyEvent *event)
{
    if(event->key()==Qt::Key_Up)
    {

        this->setTransformOriginPoint(this->rect().width()/2,this->rect().height()/2);
        ThisTransform.rotate(90);
        this->setTransform(ThisTransform);
    }
    if(event->key()==Qt::Key_Down)
    {

      
    }
    if(event->key()==Qt::Key_Left && pos().x()>0 && scene()->itemAt(this->x()-1,this->y()+rect().height(),QTransform())==0)
    {
        setPos(x()-30,y());
     
    }
    else if(event->key()==Qt::Key_Right && pos().x()+this->rect().width()<scene()->width() && scene()->itemAt(this->x()+rect().width()+1,this->y(),QTransform())==0)
    {
        setPos(x()+30,y());
       
    }

}

Вращение идет вокруг левой верхней вершины фигуры, хочется реализовать вращение на месте.
Извиняюсь за корявый код, открыт для предложений корректировки.
  • Вопрос задан
  • 1433 просмотра
Пригласить эксперта
Ответы на вопрос 2
gbg
@gbg
Любые ответы на любые вопросы
Уберите из родителей класса QTransform (его там быть не должно, его наличие - признак отвратительного дизайна класса) и компилятор вам сразу покажет, где ошибка (центр поворота задаете для одной трансформации, а фактически применяете другую).
Ответ написан
Комментировать
@DancingOnWater
О боже....
Вы не понимаете суть Qt Graphics Framework.

Чтоб повернуть Item вам достаточно вызвать метод setRotation
Ответ написан
Комментировать
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы