Results 1 to 18 of 18

Thread: Updating widgets

  1. #1

    Default Updating widgets

    I have a pointer to a QGroupBox. I want to destroy the QGroupBox, create a new one and have the pointer look for the new one.

    If I try to use this code, the QGroupBox simply disappears. I need to update it, and deleting->recreating it is the simplest way I can think of.
    Qt Code:
    1. QGroupBox *oldReceivedPMsBox = receivedPMsBox;
    2.  
    3. receivedPMsBox = new QGroupBox("Received PMs", this);
    4. myMessageScrollList = new MessageScrollList(account->getUser()->getReceivedPMs(), this);
    5. QVBoxLayout *receivedPMsLayout = new QVBoxLayout;
    6. receivedPMsLayout->addWidget(myMessageScrollList);
    7. receivedPMsBox->setLayout(receivedPMsLayout);
    8.  
    9. oldReceivedPMsBox->deleteLater();
    To copy to clipboard, switch view to plain text mode 

    Please help me.

  2. #2
    Join Date
    Feb 2008
    Posts
    491
    Thanks
    12
    Thanked 142 Times in 135 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: Updating widgets

    Think about what "oldReceivedPMsBox" is pointing at and where you're calling deleteLater().

    Quote Originally Posted by The 11th plague of Egypt View Post
    I need to update it, and deleting->recreating it is the simplest way I can think of.
    Kind of a brute force way of updating, don't you think? Can you not clear "myMessageScrollList"?

  3. #3

    Default Re: Updating widgets

    Yes, I solved using this code. I hope it destroys all the widgets in myMessageScrollList, will check.
    Tell me if it's correct on the GUI side, please.
    Qt Code:
    1. QGroupBox *oldReceivedPMsBox = receivedPMsBox;
    2.  
    3. receivedPMsBox = new QGroupBox("Received PMs", this);
    4. myMessageScrollList = new MessageScrollList(account->getUser()->getReceivedPMs(), this);
    5. QVBoxLayout *receivedPMsLayout = new QVBoxLayout;
    6. receivedPMsLayout->addWidget(myMessageScrollList);
    7. receivedPMsBox->setLayout(receivedPMsLayout);
    8.  
    9. myLayout->removeWidget(oldReceivedPMsBox);
    10. oldReceivedPMsBox->deleteLater();
    11. myLayout->addWidget(receivedPMsBox, 0, 0);
    To copy to clipboard, switch view to plain text mode 
    myMessageScrollList is a scrollarea full of MessageWidgets, so I need to destroy a bunch of objects anyway.
    Is there something inherently wrong in destroying widgets at runtime?

    BTW I have a question about QGroupBox
    Qt Code:
    1. messagesBox = new QGroupBox("title", this);
    2. QWidget *widgetInABox = new QWidget;
    3. QVBoxLayout *boxLayout = new QVBOXLayout;
    4. boxLayout->addWidget(widgetInABox);
    5. messagesBox->setLayout(boxLayout);
    To copy to clipboard, switch view to plain text mode 
    Does widgetInABox become a child of messagesBox?
    Last edited by The 11th plague of Egypt; 21st September 2011 at 15:20.

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

    Default Re: Updating widgets

    Why are you creating a new groupbox instead of reusing the old one?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5

    Default Re: Updating widgets

    Because it's quicker?

    To be honest, this is a little more complex. That's not the whole code.

    I have a QGroupBox, inside the layout of which is a QScrollArea.

    Inside the QScrollArea is a QWidget.

    Inside the QWidget is layout which holds a whole bunch of QWidgets, some of which I need to remove.

    The new code destroys the QWidget inside the QScrollArea, and leaves the QGroupBox intact.

    I don't know if using deletelater() on the QWidget destroys all the QWidgets inside its layout.

    Here's the whole thing
    Qt Code:
    1. MessageScrollBox::MessageScrollBox(const QString &_title, const QVector<Message*> *messages, QWidget *parent) :
    2. QWidget(parent), title(_title), myMessages(messages)
    3. {
    4. QVBoxLayout *containerLayout = new QVBoxLayout;
    5.  
    6. for (QVector<Message*>::const_iterator it = myMessages->constBegin(); it != myMessages->constEnd(); ++it)
    7. {
    8. containerLayout->addWidget(new MessageReadWidget(*it));
    9. }
    10. QWidget *containerWidget = new QWidget;
    11. containerWidget->setLayout(containerLayout);
    12.  
    13. myMessagesScroll = new QScrollArea(this);
    14. myMessagesScroll->setWidget(containerWidget); //containerWidget becomes a child of myMessagesScroll
    15. myMessagesScroll->setWidgetResizable(true);
    16.  
    17. messagesBox = new QGroupBox(title, this);
    18. QVBoxLayout *messagesBoxLayout = new QVBoxLayout;
    19. messagesBoxLayout->addWidget(myMessagesScroll);
    20. messagesBox->setLayout(messagesBoxLayout);
    21.  
    22. QVBoxLayout *myLayout = new QVBoxLayout;
    23. myLayout->addWidget(messagesBox);
    24. setLayout(myLayout);
    25. }
    26.  
    27. MessageScrollBox::~MessageScrollBox()
    28. {
    29. qDebug() << "Destroying MessageScrollBox";
    30. }
    31.  
    32. void MessageScrollBox::refreshMessages()
    33. {
    34. QVBoxLayout *newContainerLayout = new QVBoxLayout;
    35.  
    36. for (QVector<Message*>::const_iterator it = myMessages->constBegin(); it != myMessages->constEnd(); ++it)
    37. {
    38. newContainerLayout->addWidget(new MessageReadWidget(*it));
    39. }
    40. QWidget *newContainerWidget = new QWidget;
    41. newContainerWidget->setLayout(newContainerLayout);
    42.  
    43. QWidget *oldContainerWidget = myMessagesScroll->widget();
    44. myMessagesScroll->setWidget(newContainerWidget);
    45. oldContainerWidget->deleteLater();
    46. }
    To copy to clipboard, switch view to plain text mode 

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

    Default Re: Updating widgets

    Quote Originally Posted by The 11th plague of Egypt View Post
    Because it's quicker?
    In terms of lines of code you have to write or in terms of execution time? Certainly not the latter, hard to say about the former.

    Inside the QWidget is layout which holds a whole bunch of QWidgets, some of which I need to remove.
    Why don't you just remove the widgets you don't want and leave the rest unchanged?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  7. #7
    Join Date
    Sep 2011
    Posts
    1,245
    Thanks
    3
    Thanked 127 Times in 126 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Updating widgets

    honestly, it looks poorly designed. You should have some class/functionality that handles the messages and knows how to handle new ones, and what to do when old ones get too old.

    Does widgetInABox become a child of messagesBox?
    qwidgets in layouts will become children of the widget which owns the layout. (so yes you your question)

  8. The following user says thank you to amleto for this useful post:

    The 11th plague of Egypt (21st September 2011)

  9. #8

    Default Re: Updating widgets

    Thank you, it may be poorly designed, but I just need it to work.
    I'll make it more complex and faster when needed.

    BTW the very last line causes a segfault, I don't know why
    oldContainerWidget->deleteLater();

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

    Default Re: Updating widgets

    Quote Originally Posted by The 11th plague of Egypt View Post
    Thank you, it may be poorly designed, but I just need it to work.
    I'll make it more complex and faster when needed.
    I read your code again. Are you sure you shouldn't be using QListWidget instead of all those widgets of yours?

    BTW the very last line causes a segfault, I don't know why
    oldContainerWidget->deleteLater();
    You are probably dereferencing a null pointer.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  11. The following user says thank you to wysota for this useful post:

    The 11th plague of Egypt (21st September 2011)

  12. #10

    Default Re: Updating widgets

    I read your code again. Are you sure you shouldn't be using QListWidget instead of all those widgets of yours?
    I don't really know, here's how the thing looks (in its horrible appearence).


    I read your code again. Are you sure you shouldn't be using QListWidget instead of all those widgets of yours?
    You are probably dereferencing a null pointer.
    Right! I just reread the QScrollArea::setWidget documentation, and noticed this...
    The widget becomes a child of the scroll area, and will be destroyed when the scroll area is deleted or when a new widget is set.

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

    Default Re: Updating widgets

    Quote Originally Posted by The 11th plague of Egypt View Post
    I don't really know, here's how the thing looks (in its horrible appearence).
    Yes, I'd say you should use QListWidget with a custom delegate
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  14. #12

    Default Re: Updating widgets

    Nice idea, makes me curious. The content I display is static (the QLineEdits are set to read only).

    I'm thinking about creating empty QListWidgetItems and then using this function to display my custom widgets. Is that ok?
    void QListWidget::setItemWidget ( QListWidgetItem * item, QWidget * widget )

    Or can I directly create a QListWidgetItem that integrates my widget?
    EDIT: In this case, I need to get my widgets destroyed when the list is cleared.

    QItemDelegate seems a little more advanced, I'm not sure I can handle it.
    Last edited by The 11th plague of Egypt; 21st September 2011 at 21:20.

  15. #13

    Default Re: Updating widgets

    Bump

    I'm still wondering if and how I can add a QWidget to a QListWidget. Would this work?

    Qt Code:
    1. QWidget *myWidget = new MessageReadWidget(...);
    2. QListWidget *myQListWidget = new QListWidget(this);
    3. myQListWidget->setItemWidget (new QListWidgetItem(myQListWidget), myWidget);
    To copy to clipboard, switch view to plain text mode 

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

    Default Re: Updating widgets

    It would, but it would get terribly slow and your widget would not be related to the item's contents in any way.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  17. #15

    Default Re: Updating widgets

    Better or worse than adding them all to a QScrollArea?

    Is there a cleaner way to do what I need without getting too complex?

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

    Default Re: Updating widgets

    I think you should just implement your own delegate. If you don't want to, then stick with QScrollArea, there is no point in using QListWidget if you don't want to have any items anyway.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  19. #17

    Default Re: Updating widgets

    Thank you. I can't really understand what a delegate is, got any quick tutorial/code snippet?

    BTW is there a way to know how many tabs a tabwidget has? I need to destroy them all, and clear() is not sufficient.

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

    Default Re: Updating widgets

    Quote Originally Posted by The 11th plague of Egypt View Post
    Thank you. I can't really understand what a delegate is, got any quick tutorial/code snippet?
    You can start with QAbstractItemDelegate docs and then head to Star Delegate example.

    BTW is there a way to know how many tabs a tabwidget has? I need to destroy them all, and clear() is not sufficient.
    QTabWidget::count()
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  21. The following user says thank you to wysota for this useful post:

    The 11th plague of Egypt (28th September 2011)

Similar Threads

  1. Replies: 2
    Last Post: 30th March 2011, 20:20
  2. Replies: 2
    Last Post: 16th December 2010, 11:52
  3. Replies: 2
    Last Post: 20th August 2010, 13:20
  4. Replies: 5
    Last Post: 18th April 2010, 23:31
  5. Replies: 0
    Last Post: 15th May 2009, 15:38

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.