signal is not working
-
Hi,
In my program there are 2 basic classes, DiagramScene and MainWindow.
What i'm trying to do is to emit a signal from the DiagramScene to my MainWindow but the sinal is not recieved.DiagramScene.h
void addtreeitem( QIcon, DiagramItem *item);
DiagramScene.cpp
emit addtreeitem(QIcon("C:/Users/Lukas/Desktop/simulight/images/barramento"),item);
MainWindow.h
public slots: void addtreeitem( QIcon, DiagramItem *item);
MainWindow.cpp
MainWindow::MainWindow() { scene = new DiagramScene(itemMenu, this); connect(scene, SIGNAL(addtreeitem(QIcon, DiagramItem *item);), this, SLOT(addtreeitem(QIcon, DiagramItem *item))); } ... void MainWindow::addtreeitem( QIcon icon, DiagramItem *item) { QString name=item->data(0).toString(); addTreeEquip(tree->selectedItems()[0],name,icon); QMessageBox msg; msg.setText("ok");//to test if the signal is being emitted msg.exec(); }
i don't know why this is wrong :(
-
Hi,
In my program there are 2 basic classes, DiagramScene and MainWindow.
What i'm trying to do is to emit a signal from the DiagramScene to my MainWindow but the sinal is not recieved.DiagramScene.h
void addtreeitem( QIcon, DiagramItem *item);
DiagramScene.cpp
emit addtreeitem(QIcon("C:/Users/Lukas/Desktop/simulight/images/barramento"),item);
MainWindow.h
public slots: void addtreeitem( QIcon, DiagramItem *item);
MainWindow.cpp
MainWindow::MainWindow() { scene = new DiagramScene(itemMenu, this); connect(scene, SIGNAL(addtreeitem(QIcon, DiagramItem *item);), this, SLOT(addtreeitem(QIcon, DiagramItem *item))); } ... void MainWindow::addtreeitem( QIcon icon, DiagramItem *item) { QString name=item->data(0).toString(); addTreeEquip(tree->selectedItems()[0],name,icon); QMessageBox msg; msg.setText("ok");//to test if the signal is being emitted msg.exec(); }
i don't know why this is wrong :(
connect(scene, SIGNAL(addtreeitem(QIcon, DiagramItem *item);), this, SLOT(addtreeitem(QIcon, DiagramItem *item)));
- this code cannot compile
- you don't check the result of connect
- you should prefer the new functor connect syntax
- Inside
SIGNAL
andSLOT
only types should be given, no variable names
Regards
-
and I don't if it makes any difference, but the signal is emitted from a mousePressEvent function
DiagramScene.h
void addtreeitem( QIcon, DiagramItem *item);
Please show more context about this header file, i.e. where/how the signal: keyword is used before this definition
As @aha_1980 suggested please make the effort to try using the new signal & slots syntax. It helps showing any errors during compile-time so it's easier to spot what could be wrong with the signal and slots connections.
In addition, I'd suggest that you don't use the same name for the slot as the signal. Usually something like handleSignalName is easier to follow, i.e. handleAddTreeItem() in your case.
-
Hi,
DiagramScene.h
class DiagramScene : public QGraphicsScene { Q_OBJECT ... signals: void itemInserted(DiagramItem *item); void textInserted(QGraphicsTextItem *item); void clicked(); void addtreeitem(DiagramItem *item);//here is the signal .... };
that's it
How can i write it with the new sintax?
When i execute the program it says "QObject::connect: No such signal DiagramScene::addtreeitem(DiagramItem*);"PS: i changed the input of the signal and the slot to only DiagramItem*
-
@frnklu20 said in signal is not working:
SIGNAL(addtreeitem(QIcon, DiagramItem*);
I don't see such a signal in your header (apart from the fact that the semicolon is absolutely wrong there). As already wrote above: Use the new signal/slot syntax or properly check the return value of QObject::connect() / take a look at the console output during runtime and you will notice that there is no such a signal at all.
/edit: did not saw you last comment about the change of the signal, but the rest of my comments is still valid.
-
@frnklu20 said in signal is not working:
but how can i write it in the new sintax?
By looking into the documentation
-
You are missing the & for the function parameters.
-
And what error exactly ?
-
in the no matching member it says:
mainwindow.cpp:25:5: error: no matching member function for call to 'connect'
qobject.h:228:43: note: candidate function template not viable: requires at least 4 arguments, but 2 were provided
qobject.h:260:13: note: candidate function template not viable: requires 3 arguments, but 2 were provided
qobject.h:269:13: note: candidate function template not viable: requires at least 4 arguments, but 2 were provided
qobject.h:300:13: note: candidate function template not viable: requires 3 arguments, but 2 were provided
qobject.h:308:13: note: candidate function template not viable: requires at least 4 arguments, but 2 were provided
qobject.h:463:41: note: candidate function not viable: requires at least 3 arguments, but 2 were provided
qobject.h:208:36: note: candidate function not viable: requires at least 4 arguments, but 2 were provided
qobject.h:211:36: note: candidate function not viable: requires at least 4 arguments, but 2 were provideddon't know what that means
-
Since you're doing the connect in a class which is not derived from QObject or in main() you have forgot 'QObject::' before connect since it's a static function of QObject
And you've a syntax error in your code - '&DiagramScene::addtreeitem),'
-
yeah i fixed it! but it gives me an error in the connect
if i put the ¶meter in the objects it says cannot take the adress of an rvalue of type MainWindow and no matching member function for call to 'connect'
-
yeah i fixed it! but it gives me an error in the connect
if i put the ¶meter in the objects it says cannot take the adress of an rvalue of type MainWindow and no matching member function for call to 'connect'