Is it possible to connect a signal to a static slot without a receiver instance?
Update for QT5: Yes you can
static void someFunction() {
qDebug() << "pressed";
}
// ... somewhere else
QObject::connect(button, &QPushButton::clicked, someFunction);
In QT4 you can't:
No it is not allowed. Rather, it is allowed to use a slot which is a static function, but to be able to connect it you need an instance.
In their example,
connect(exitAct, SIGNAL(triggered()), qApp, SLOT(closeAllWindows()));
means than they previously called
QApplication* qApp = QApplication::instance();
Edit:
The only interface for connecting object is the function
bool QObject::connect ( const QObject * sender, const QMetaMethod & signal, const QObject * receiver, const QMetaMethod & method, Qt::ConnectionType type = Qt::AutoConnection )
How are you going to get rid of const QObject * receiver
?
Check the moc
files in your project, it speaks by itself.
Qt: How to connect static signal from different class to slot?
As indicated in this thread it is not possible to emit static signals since it is always associated with a QObject
, as an alternative they create a singleton that would be equivalent to what you want.
#include <QtCore>
class Receiver: public QObject
{
Q_OBJECT
public:
using QObject::QObject;
Q_SLOT void mySlot(){
qDebug()<< __PRETTY_FUNCTION__;
QCoreApplication::quit();
}
};
class Sender: public QObject
{
Q_OBJECT
using QObject::QObject;
public:
static Sender& instance(){
static Sender m_instance;
return m_instance;
}
static void myFunction1(){
emit instance().mySignal();
}
Q_SIGNAL void mySignal();
};
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
Receiver r;
QObject::connect(&Sender::instance(), &Sender::mySignal, &r, &Receiver::mySlot);
QTimer::singleShot(1000, &Sender::myFunction1);
return a.exec();
}
#include "main.moc"
Qt Designer - How to connect a signal to a static function?
The second argument is incorrect. You should specify the class name, not object name. So it should be:
QObject::connect(actionOpen, &QAction::triggered, fileOpen);
Complete working example (tested):
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);
menu.show();
return a.exec();
}
How to call a static function as a SLOT() in Qt?
You try this,
QObject::connect(selectButton, SIGNAL(clicked()), listenerObj, SLOT(setLabel()));
listenerObj is the object pointer of class that you declared your slot. If you are unable to use "this" in listener, you declare an active object which contains a public slot of your function setLabel and connect the slot.
declare setLabel() as public slot in header file of your new class
class SomeClass
{
public slots:
void setLabel();
}
then using parent pointer you could show the label in interface
I think some of this will help you.
Static Signals in QT
In theory, Qt Could implement this feature, by basically adding code generation for the helper object code you showed to the moc compiler.
Because any signal is a function declaration, of which the implementation is generated by the moc compiler. This implementation is basically just a:
for( auto& slot: connected_slots):
slot.object->slot.function()
(very simplified)
this connected_slots
needs to live somewhere. ie. it is a variable, which needs to be stored in memory.
So in any case you are going to need an object of some kind to store this in. This is normally in the QObject
base class.
My guess for the reason that they didn't add this, is because the Qt framework is very object oriented, and the added complexity outweighs the benefits.
QT: Connect a signal with a fundamental type in its signature to a slot with QVariant in its signature
You can as long as you change
void setValue(QVariant & value)
into
void setValue(QVariant value)
or
void setValue(const QVariant & value)
otherwise compilation will fail with a static assertion failed (Signal and slot arguments are not compatible).
Related Topics
Program Can't Find Libgcc_S_Dw2-1.Dll
Is There C/C++ Equivalent of Eval("Function(Arg1, Arg2)")
Uses for Negative Zero Floating Point Value
Std::Vector of Std::Vectors Contiguity
Counting the Number of Occurrences of a String Within a String
How to Download a File with Winhttp in C/C++
Base Pointer to Array of Derived Objects
Initializer List for Dynamic Arrays
How to Make Cout Behave as in Binary Mode
Consistent Pseudo-Random Numbers Across Platforms
Portable Zip Library for C/C++ (Not an Application)
Is There Any Use for Local Function Declarations
Cast Vector<T> to Vector<Const T>
Member Fields, Order of Construction
How to Write to Middle of a File in C++