Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Qt and variadic functions

OK, before lecturing me on the use of C-style variadic functions in C++...everything else has turned out to require nothing short of rewriting the Qt MOC.

What I'd like to know is whether or not you can have a "slot" in a Qt object that takes an arbitrary amount/type of arguments. The thing is that I really want to be able to generate Qt objects that have slots of an arbitrary signature. Since the MOC is incompatible with standard preprocessing and with templates, it's not possible to do so with either direct approach. I just came up with another idea:

struct funky_base : QObject
{
  Q_OBJECT
  funky_base(QObject * o = 0);

public slots:
  virtual void the_slot(...) = 0;
};

If this is possible then, because you can make a template that is a subclass of a QObject derived object so long as you don't declare new Qt stuff in it, I should be able to implement a derived templated type that takes the ... stuff and turns it into the appropriate, expected types.

If it is, how would I connect to it? Would this work?

connect(x, SIGNAL(someSignal(int)), y, SLOT(the_slot(...)));

If nobody's tried anything this insane and doesn't know off hand, yes I'll eventually try it myself...but I am hoping someone already has existing knowledge I can tap before possibly wasting my time on it.


This question was an attempt to find a way to design a 'catch-all' base class for a templated object that could translate Qt signals into static signals like boost::signals2 or just basic functions. I thought if I could construct a slot that took variadic templates I could use TMP to reconstruct the parameters out of the va_args. The answer to the problem was pretty much exactly that but cuts in BEFORE the slot gets called by the qt mechanism. The first installment of an article series on how to make the whole thing showed how I solved this part of the problem:

http://crazyeddiecpp.blogspot.com/2011/01/quest-for-sane-signals-in-qt-step-1.html

That's my old blog location. New one's in my profile if you want to see other weird sh1t.

like image 636
Edward Strange Avatar asked Dec 27 '10 21:12

Edward Strange


People also ask

What is the purpose of Variadic functions?

Variadic functions are functions that can take a variable number of arguments. In C programming, a variadic function adds flexibility to the program. It takes one fixed argument and then any number of arguments can be passed.

How do you declare a variadic function in C++?

Variadic functions are functions (e.g. std::printf) which take a variable number of arguments. To declare a variadic function, an ellipsis appears after the list of parameters, e.g. int printf(const char* format...);, which may be preceded by an optional comma.

Is printf variadic function?

Variadic functions are functions (e.g. printf) which take a variable number of arguments. The declaration of a variadic function uses an ellipsis as the last parameter, e.g. int printf(const char* format, ...);. See variadic arguments for additional detail on the syntax and automatic argument conversions.

What is variadic Python?

Variadic parameters (Variable Length argument) are Python's solution to that problem. A Variadic Parameter accepts arbitrary arguments and collects them into a data structure without raising an error for unmatched parameters numbers.


1 Answers

Apparently people keep thinking this question is still in need of answer because several weeks after I asked it and then answered it myself, people are still posting their responses. So I guess I have to make it explicitly an answer instead of both in the question itself and the first comment for it:

You can't use a variadic function as a signal or slot in Qt.

I spent a lot of time and effort not only solving the problem and sharing it, but explaining HOW the problem was solved. Please consider reading, you might even learn something new.

like image 52
Edward Strange Avatar answered Oct 07 '22 19:10

Edward Strange