jerous86 / nimqt

Qt bindings for nim
GNU General Public License v2.0
93 stars 6 forks source link

Connecting a signal to non-QObject slot proc? #11

Closed matkuki closed 1 year ago

matkuki commented 1 year ago

Could this be added, connecting a free standing proc to a signal?

#include <QApplication>
#include <QMenu>
#include <QAction>

void fileOpen() {
  qDebug() << "ok";
}

int main(int argc, char *argv[]) {
  QApplication a(argc, argv);
  QMenu menu;
  QAction* actionOpen = menu.addAction("test");
  QObject::connect(actionOpen, &QAction::triggered, fileOpen); // <-- This part
  menu.show();
  return a.exec();
}
jerous86 commented 1 year ago

I see what you mean. But not sure if it can be added. Is that possible in Qt with c++?

If it is not possible, then the proc fileOpen should somehow know that it should be a slot in a QObject (maybe a pragma?). But I'm not too inclined to implement it in this case then :)

matkuki commented 1 year ago

Just tested it to be sure and yes, the above code works in C++ (added the needed includes into the above code). I think it's this signature for the QObject: https://doc.qt.io/qt-5/qobject.html#connect-4

jerous86 commented 1 year ago

I finally figured out how to do it, and should work just like the regular connect :) An example can be found in examples/hello.nim

matkuki commented 1 year ago

Excellent, works like a charm 👍👍👍👍 Thank you!