Results 1 to 8 of 8

Thread: model conception

  1. #1
    Join Date
    Apr 2006
    Posts
    6
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default model conception

    Hi all,

    I have a question regarding the general conception of a Qt program.

    I have an application that uses several types of items, say cars, drivers, mechanics, etc... (which all have a few specific values: brand of a car, age of a driver, etc...). The application has a list of every of these items, and performs an optimisation based on the informations they contain.

    The user interface I'm building uses the model / view framework (mainly QTableModel and QTableView), to allow the edition of items. I built a model for every type of item (specializations of QTableModel), so I have: CarModel, DriverModel, etc...
    All these classes actually share a lot of code: addItem, removeItem,... All I could find is to copy/paste the code in the different classes.

    Is there not something simpler to do? I was thinking of using templates, and doing something like:
    template<class T> class MyModel (which would include a few pure virtual functions)
    which I would then specialize for cars, drivers, mechanics,...
    But I see in the documentation of moc that signals and slots aren't supported in template classes.

    So how could I keep my code simple, and avoid having the same piece of code copied in several classes?

    Thanks,

    Xavier

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: model conception

    Implement the common parts of all models in a direct subclass of the table model and then subclass it and implement the parts that differ in the subclass.

    QAbstractTableModel <-- MyCommonModel <-- { CarModel, DriverModel, MechanicModel }

  3. #3
    Join Date
    Apr 2006
    Posts
    6
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: model conception

    Thanks for that quick reply.

    Actually, I have a problem of types within the code that should be shared.
    For instance, I have, for each class, a vector of pointers to objects.
    i.e vector<Car*> or vector<Driver*>

    now I have functions like removeElement(int num), which says:
    theVector->erase(...)
    I would like such functions to "templatized". It is every time the same function, but applying to different data types, i.e. vector<Car*> or vector<Driver*>.
    That's why I was thinking of templates.

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: model conception

    You can could an abstract base class for those objects:
    Qt Code:
    1. . +--- Car
    2. |
    3. Object <---+--- Driver
    4. |
    5. +--- Mechanic
    To copy to clipboard, switch view to plain text mode 
    and then create a class that would operate on a list of Object* and its subclasses (specific to each of these objects).

  5. #5
    Join Date
    Apr 2006
    Posts
    6
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: model conception

    Yes, I could do that but there is a problem of type casts.

    Indeed, the compiler can easily cast say between Car and Object, but vector<Car*> and vector<Object*> won't work.

    So my library returns a vector<Car*>&, and the GUI cannot simply treat it as a vector<Object*>&. Except if I provide an explicit cast function, but I don't know how to do that. (I can not simply cast every item in a new vector, since I need to operate on the vector itself).

    Anyway, the Qt documentation is not very clear about templates. It only says:
    moc does not handle all of C++. The main problem is that class templates cannot have signals or slots. Here is an example:

    class SomeTemplate<int> : public QFrame
    {
    Q_OBJECT
    ...

    signals:
    void mySignal(int);
    };
    So the derived class can't have signals. How about the base class? Is this valid:

    Qt Code:
    1. template<class T> class SomeTemplate : public QTableModel
    2. {
    3. Q_OBJECT
    4. ...
    5.  
    6. signals:
    7. void mySignal(int);
    8. };
    To copy to clipboard, switch view to plain text mode 

    ??

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: model conception

    If classes share a common functionality, they can operate on the common base class of objects. In places where classes need a specialised functionality (so you implement them in a subclass), they can cast the object to a specialised class.

    You really don't need templates here... And if you really really really want to have them, make sure that the template class won't introduce signals or slots -- implement them in a superclass, so that you don't have to moc the subclass.

  7. #7
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: model conception

    Quote Originally Posted by xavier
    How about the base class?
    Probably it will work, as long as you won't have to run moc on the template (that is: you can't use Q_OBJECT or add new signals or slots).

    Quote Originally Posted by xavier
    Is this valid: [...]?
    No, it's not. It's exactly the same situation as the one from the docs --- a template class with a signal.

  8. #8
    Join Date
    Apr 2006
    Posts
    6
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: model conception

    Quote Originally Posted by wysota
    If classes share a common functionality, they can operate on the common base class of objects. In places where classes need a specialised functionality (so you implement them in a subclass), they can cast the object to a specialised class.

    You really don't need templates here... And if you really really really want to have them, make sure that the template class won't introduce signals or slots -- implement them in a superclass, so that you don't have to moc the subclass.
    Well, I don't really really want to use templates... But, as I said earlier, I have a library whith functions which return vector<Car*>, vector<Driver*>, etc...
    The function "removeElement", has exactly the same code every time but it operates on different types (vector<Car*> and vector<Driver*>).
    I could create a base class "object", as Jacek suggested earlier. But how can I cast from vector<Car*> and vector<Driver*> to vector<Object*>, considering that my function operates on the vector itself (erase elements), so I can't just copy and cast every single element in a new vector!
    and I don't want to modify the base library so that it returns a vector<object*>.

    See what I mean?
    Now this is more a C++ problem than a Qt one, so I guess I should search elsewhere. Anyway, thanks for your help.

    Xavier

Similar Threads

  1. hierarchical model in a flat view
    By gniking in forum Qt Programming
    Replies: 4
    Last Post: 10th November 2009, 20:17
  2. Coin3d + Qt: SIGLNALs and SLOTs
    By vonCZ in forum Newbie
    Replies: 26
    Last Post: 15th May 2009, 07:34
  3. Model / Threading Advice
    By tntcoda in forum Qt Programming
    Replies: 6
    Last Post: 19th November 2008, 14:02
  4. Custom Model Advice Requested
    By mclark in forum Qt Programming
    Replies: 3
    Last Post: 18th September 2008, 16:26
  5. Model Choices review/questions
    By ucntcme in forum Qt Programming
    Replies: 1
    Last Post: 16th September 2007, 21:57

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.