Results 1 to 5 of 5

Thread: Problem with virtual functions

  1. #1
    Join Date
    Jun 2007
    Location
    Netherlands
    Posts
    54
    Thanks
    8
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Problem with virtual functions

    Hey all

    I am trying to create a treeview in which the parent holds different information than its children, so i created two seperate classes, postheaderitem and postdataitem, for this and one abstract class, abstractpostitem, which also serves as a rootitem.

    From a dialog I create an entire new tree item with its child items and return that. That item I want to append to my rootitem. Before I append it I want to set the parent of the new item so it points to rootItem.

    Qt Code:
    1. void PostModel::addPost(AbstractPostItem newPost)
    2. {
    3. newPost.setParent(rootPost);
    4. this->insertRows(rootPost->childCount(),1,newPost,QModelIndex());
    5. }
    To copy to clipboard, switch view to plain text mode 

    The type created in my dialog is of postheaderitem and that is passed to this function as newPost. But the setParent function is called in the abstractpostitem and not in the postheaderitem.

    So I am wondering if I should cast newPost to a postheaderitem or if I am doing something else wrong.

    Regards
    eekhoorn12

  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: Problem with virtual functions

    Hmm... what exactly is the problem again? What is postheaderitem? What is AbstractPostItem? Are you sure you don't need to use pointers anywhere?

  3. #3
    Join Date
    Jun 2007
    Location
    Netherlands
    Posts
    54
    Thanks
    8
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Problem with virtual functions

    I have changed my code some to make use of pointers, the previous problem is solved but I still have a problem.

    I will try to explain the situation some more.

    I have a treeview which I would like to populate. It is going to be a treeview with a headerbar en below that one level that describes a certain group with its own information and it has child items to have some other information.

    To create this I tried to make one abstract class named AbstractPostItem, this class als functions as the root item of the treeview.

    For the description items in the treeview I created PostHeaderItem which inherits AbstractPostItem and for the child items of PostHeaderItem I created PostDataItem which also inherits AbstractPostItem.

    Now from a dialog I try to create a new PostHeaderItem with its children, for that I use the following code:

    Qt Code:
    1. PostHeaderItem::PostHeaderItem(QStringList files, QString subject, QString group)
    2. {
    3. foreach(QString i, files)
    4. {
    5. PostDataItem *newItem = new PostDataItem(i);
    6. newItem->setSubject(subject);
    7. newItem->setGroup(group);
    8. newItem->setParent(this);
    9. addItem(newItem);
    10. }
    11. setSubject(subject);
    12. setGroup(group);
    13. }
    To copy to clipboard, switch view to plain text mode 

    Which is called from:

    Qt Code:
    1. AbstractPostItem *NewPost::getPost()
    2. {
    3. PostHeaderItem *newItem = new PostHeaderItem(files, ui->inputSubject->text(), ui->inputUsenetGroup->text());
    4. return newItem;
    5. }
    To copy to clipboard, switch view to plain text mode 

    Which is called from:

    Qt Code:
    1. void PostView::newPost()
    2. {
    3. AbstractPostItem *tmpAbstractPostItem;
    4. postScreen = new NewPost(this);
    5. postScreen->setWindowFlags(Qt::Window);
    6. postScreen->setAttribute(Qt::WA_ShowModal);
    7. if(postScreen->exec() == QDialog::Accepted)
    8. {
    9. tmpAbstractPostItem = postScreen->getPost();
    10. postsList->addPost(tmpAbstractPostItem);
    11. postScreen->close();
    12. }
    13. }
    To copy to clipboard, switch view to plain text mode 

    Now my problem is that in the first and second code bit everything works just as expected, but when I try to call functions from tmpAbstractPostItem in the third code bit it uses the functions in the AbstractPostItem class instead of the PostHeaderItem class which is what I want.

    So my question is how can I accomplish this?

    Update:
    Added the files as attachment.
    Attached Files Attached Files
    Last edited by eekhoorn12; 7th April 2010 at 23:24.

  4. #4
    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: Problem with virtual functions

    Quote Originally Posted by eekhoorn12 View Post
    Server isn't always running, usually during european day time and evening it is up.
    I have deleted links to the code from your post. Please do it the proper way - attach files to your post instead of linking to external sites, especially ones that might not work. If you manage to squeeze into 6 hours from posting the original post, you can (and should) edit it and add the attachment there.

  5. #5
    Join Date
    Jun 2007
    Location
    Netherlands
    Posts
    54
    Thanks
    8
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Problem with virtual functions

    Found out what my problem was. The problem was that the function declaration in the AbstractPostItem and PostHeaderItem were not the same. The one in PostHeaderItem was a const and the one in AbstractPostItem was not. When I changed that it worked.

Similar Threads

  1. QSharedDataPointer and Data with virtual functions
    By niko in forum Qt Programming
    Replies: 0
    Last Post: 2nd February 2010, 08:23
  2. Replies: 3
    Last Post: 17th February 2009, 04:23
  3. Virtual FB problem
    By nrabara in forum Newbie
    Replies: 0
    Last Post: 2nd October 2008, 06:05
  4. problem with virtual function in a .h
    By mickey in forum General Programming
    Replies: 5
    Last Post: 19th April 2008, 18:57
  5. virtual overloaded functions and base class function call...
    By nouknouk in forum General Programming
    Replies: 7
    Last Post: 11th March 2006, 21:26

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.