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

How to use multiple focus setFocusItem



  • Hey, I'm using keyPressEvent to build a space invader's game. My problem is because I want to focus 2 items on Dialog, I just can focus one of them, what should I use to focus 2 or more items?
    //
    item3-> setFlag(QGraphicsItem::ItemIsFocusable);
    item4-> setFlag(QGraphicsItem::ItemIsFocusable);
    scene->setFocusItem(item3);
    scene->setFocusItem(item4);
    // principal code

    #include "dialog.h"
    #include "ui_dialog.h"
    #include"minhanave.h"
    #include"atirador.h"
    #include"combatente.h"
    #include<QGraphicsItem>
    #include "bala.h"
    Dialog::Dialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::Dialog)
    {
    ui->setupUi(this);

    scene = new QGraphicsScene(this);
    ui->graphicsView->setScene(scene);
    ui->graphicsView->setRenderHint(QPainter:: Antialiasing);
    // caixa delimitadora
    scene->setSceneRect(-200,-200,300,300);
    
    QPen mypen = QPen (Qt::black);
    
    QLineF TopLine(scene->sceneRect().topLeft(),scene->sceneRect().topRight());
    QLineF LeftLine(scene->sceneRect().topLeft(),scene->sceneRect().bottomLeft());
    QLineF RightLine(scene->sceneRect().topRight(),scene->sceneRect().bottomRight());
    QLineF BottomLine(scene->sceneRect().bottomLeft(),scene->sceneRect().bottomRight());
    
    scene->addLine(TopLine,mypen);
    scene->addLine(LeftLine,mypen);
    scene->addLine(RightLine,mypen);
    scene->addLine(BottomLine,mypen);
    // caixa delimitadora
    
    //add Item
        MinhaNave *item = new MinhaNave();
        //Atirador *item2 = new Atirador();
        Combatente *item3 = new Combatente();
        Bala *item4 = new Bala();
        scene->addItem(item);
        //scene->addItem(item2);
        scene->addItem(item3);
        scene->addItem(item4);
    // make rect focusable
    item3-> setFlag(QGraphicsItem::ItemIsFocusable);
    item4-> setFlag(QGraphicsItem::ItemIsFocusable);
    scene->setFocusItem(item3);
    scene->setFocusItem(item4);
    
    //add Item
    timer = new QTimer(this);
    connect(timer,SIGNAL(timeout()), scene,SLOT(advance()));
    //celera jesus
    timer->start(100);
    

    }

    Dialog::~Dialog()
    {
    delete ui;
    }


  • Lifetime Qt Champion

    Hi and welcome to devnet,

    What is your goal exactly with these two objects ?


Log in to reply