Results 1 to 2 of 2

Thread: qobject_cast "good coding practice"

  1. #1
    Join Date
    Mar 2008
    Posts
    141
    Thanks
    10
    Thanked 9 Times in 9 Posts

    Default qobject_cast "good coding practice"

    Hi,

    i wonder what is a better practice of passing pointers to childwidgets (if their is anything like a common good practice ) of this two possibilities: Passing the Baseclass or MyClass?

    I have a MainWindow and need to pass a pointer to subclass of QSqlTableModel to a childwidget:

    mainwindow.cpp
    Qt Code:
    1. MyQSqlTableModel *tm = new MyQSqlTableModel;
    2. ChildWidget *cw = new ChildWidget( this );
    3. cw->setTableModel( tm );
    To copy to clipboard, switch view to plain text mode 

    1. childwidget.cpp
    Qt Code:
    1. void ChildWidget::setTableModel( QAbstractItemModel *model )
    2. {
    3. m_model = qobject_cast<MyQSqlTableModel *>(model);
    4. m_model->doSomething();
    To copy to clipboard, switch view to plain text mode 


    2. childwidget.cpp
    Qt Code:
    1. void ChildWidget::setTableModel( MyQSqlTableModel *model )
    2. {
    3. m_model = model;
    4. m_model->doSomething();
    To copy to clipboard, switch view to plain text mode 

    I have seen both and wonder what the difference is and what the better approach might be.

  2. #2
    Join Date
    Dec 2006
    Posts
    849
    Thanks
    6
    Thanked 163 Times in 151 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: qobject_cast "good coding practice"

    I prefer the 2nd. It states what really must be passed (whereas the 1st lets the compiler tolerate errors that will manifest themselves at runtime).
    (Exceptions are when you inherit a class and have no choice in the signature.)

  3. The following user says thank you to caduel for this useful post:

    janus (17th October 2008)

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.