PDA

View Full Version : templated signal or slot



QPlace
4th August 2007, 12:37
Moc returns error when slot is declared as a templated function.
I have following situation - my slots are different only in return type. So, following examples of MDI in QT docs I am creating two functions per each action. Example:

void newView(); // slot that is triggered by a menu action
View* createView(); // actual creation of a View.

If I have, say 10 of such different view types, the code becomes too cluttered. I don't want to move "creatView" code in "newView", since "createVeiw" calls will be reused elsewhere.

Question: is there a possibility to somehow employ templates here and do not revert to doing something like:
void newView(int viewtype)
{
switch (viewtype)
{
BaseView* view;
case 1: view = (BaseView*) createView1();
case 2: view = (BaseView*) createView2();
...
}
}

marcel
4th August 2007, 12:50
No, you cannot.
See this link: http://doc.trolltech.com/4.3/templates.html.

The current method seems pretty good to me.
How would you have done it with template functions?

Would you have had a single createView function parameterized by what?
If you can create 10 views with the same template function, then doesn't that mean that all functions createView1..10 are similar? Therefore you could pass viewType to createView.

Regards