0

I'm learning the signals/slots in Qt and I have found a problem. I need to create my own slot that is called when items on QGraphicsScene (in QGraphicsView) are moved or selected.

I'm starting with a simple app that has one widget and on it is graphicsView and label. I've created a slot in my window and connected it to QGraphicsScene's signal, but it is not being used. Where is my mistake?

Here is the code:

//MainWindow.h
//as generated by QtCreator, just added one slot to it
...omitted for brevity...

public slots:
       void selectedItemChanged(QGraphicsItem * newItem, QgraphicsItem * oldItem);

..omitted for brevity...

//------------------------------------------------------------------

//MainWindow.cpp
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow)
{
   ui->setupUi(this);
   QGraphicsScene * scene = new QGraphicsScene();
   scene->setBackgroundBrush (QBrush(Qt::gray));
   ui->graphicsView->setScene (scene);

   for(int x = 10; x < 250; x+=20)
   {
      QGraphicsEllipseItem * item = scene->addEllipse (x,x,5,5,QPen(Qt::darkGreen),QBrush(Qt::darkGreen));
     item->setFlag (QGraphicsItem::ItemIsFocusable,true);
   }

   QObject::connect (scene,SIGNAL(focusItemChanged),this,SLOT(selectedItemChanged));
}

void MainWindow::selectedItemChanged (QGraphicsItem *newItem, QGraphicsItem *oldItem)
{
   qDebug()<<"called";
   if(newItem == 0)
   {
      ui->label->setText ("Není vybrán bod");
   }
   else
   {
      ui->label->setText (QString::number (newItem->scenePos ().x ()) + "," + QString::number (newItem->scenePos ().y ()));
   }
}

Now, when I run the probram it rins ok, but I cannot set Focus on the circles(ellipses) drawn on the scene and the slot is not used. I tried setting IsSelectable flag, but it does not help. Is there any other preferred way to get this done or solution to my problem?

mishan
  • 1,177
  • 3
  • 19
  • 42
  • 2
    Have you tried using the full signature? `void QGraphicsScene::focusItemChanged(QGraphicsItem * newFocus, QGraphicsItem * oldFocus, Qt::FocusReason reason)` – Marco A. Oct 17 '14 at 08:23
  • 1
    http://stackoverflow.com/questions/26422154/code-compiles-by-slot-is-not-invoked-called-used-working-executed will probably help you. – Silicomancer Oct 17 '14 at 09:44
  • Yup, thanks siliconmancer. Your answer was helpful. Although my mistake was even more basic level. Maybe you can add the bit about the function signature in the SLOT/SIGNAL, not just the name of it. – mishan Oct 17 '14 at 10:17

2 Answers2

3

You're not linking against the signal's right signature, according to the documentation:

void QGraphicsScene::focusItemChanged( QGraphicsItem * newFocus, QGraphicsItem * oldFocus,
                                       Qt::FocusReason reason)

and also notice that you can check the connection's success/failure status via the bool return type of the QObject::connect method

Marco A.
  • 43,032
  • 26
  • 132
  • 246
0

So, in the end i found the answer to my own question. It was a mistake on my side.

in the connect() i used the slots without parenthesis/parameters. It should have looked like:

QObject::connect (scene,
                  SIGNAL(focusItemChanged(QGraphicsItem*,QGraphicsItem*,Qt::FocusReason)),
                  this,
                  SLOT(selectedItemChanged(QGraphicsItem*,QGraphicsItem*)));
mishan
  • 1,177
  • 3
  • 19
  • 42