Results 1 to 12 of 12

Thread: Looking for list-widget like string stack class

  1. #1
    Join Date
    Feb 2008
    Posts
    157
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Looking for list-widget like string stack class

    I have messages (Qstrings), which shall be displayed in a list. but this list shall not be filled infinitly, mabe only 1000 items. Then for more items the first ones should be skipped.

    So it is in principle exactly what happens in every console.

    I was wondering if such a class already exists, so I do not need to reinvent it.

    Matthias

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

    Default Re: Looking for list-widget like string stack class

    There is QStringList which should suit your needs or QCache, but the first one is better - if you go over the limit, simply remove the first item.

  3. #3
    Join Date
    Feb 2008
    Posts
    157
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Looking for list-widget like string stack class

    I see that QStringList is suitable for storing the messages, but how do I display them?

    I could use
    Qt Code:
    1. listwidget->clear();
    2. listwidget->addItems(currentStringList );
    To copy to clipboard, switch view to plain text mode 

    but that looks to be horrible slow, especially if it is called more than once a second.

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

    Default Re: Looking for list-widget like string stack class

    Take a look at QStringListModel.

  5. #5
    Join Date
    Feb 2008
    Posts
    157
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Looking for list-widget like string stack class

    Quote Originally Posted by wysota View Post
    Take a look at QStringListModel.
    Looks interesting, but how do I combine that with a Gui. The class and the included example have no object that derives from QWidget.

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

    Default Re: Looking for list-widget like string stack class

    Qt Code:
    1. // add contents
    2. QListView *lv = new QListView;
    3. lv->setModel(slm);
    To copy to clipboard, switch view to plain text mode 

    // Note that the QStringListModel is not aware of (later) modifications to the list you originally passed! You have to update the model to keep the two in sync.

    and when you want to change the model:
    Qt Code:
    1. slm->setStringList(newContents);
    To copy to clipboard, switch view to plain text mode 
    (better use insertRows/removeRows/setData, though, otherwise performance will suffer, there might be flickering and the user will lose his selection etc...)

    HTH

  7. #7
    Join Date
    Feb 2008
    Posts
    157
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Looking for list-widget like string stack class

    Quote Originally Posted by caduel View Post
    (better use insertRows/removeRows/setData, though, otherwise performance will suffer, there might be flickering and the user will lose his selection etc...)
    I have never worked with a model class, but seem to understand everyhing you posted, except your last statement. Does it mean the whole idea does not work?

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

    Default Re: Looking for list-widget like string stack class

    It means you shouldn't change the string list after you add it to the model because the model won't see the changes. You have to change contents of the model instead. I suggest you take a look at the docs.

  9. #9
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    8
    Thanked 334 Times in 317 Posts

    Default Re: Looking for list-widget like string stack class

    Or may be you can look at QListWidget

  10. #10
    Join Date
    Feb 2008
    Posts
    157
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Looking for list-widget like string stack class

    Quote Originally Posted by aamer4yu View Post
    Or may be you can look at QListWidget
    As I wrote im my second post in this thread I know listwidget very well but expect not really a very good speed if I constantly remove the first item and add a new last one and that every second.

  11. #11
    Join Date
    Feb 2008
    Posts
    157
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Looking for list-widget like string stack class

    My solution using the ideas posted here looks now like this:
    Qt Code:
    1. #include "ui/ui_WidgetListStringStack.h"
    2. #include <QtCore/QStringList>
    3. #include <QtGui/QStringListModel>
    4. #include <QtCore/QPointer>
    5.  
    6. class WidgetListStringStack : public QWidget, protected Ui_WidgetListStringStack
    7. {
    8. Q_OBJECT
    9. public:
    10. WidgetListStringStack(QWidget* parent = 0, Qt::WFlags flags = 0);
    11. virtual ~WidgetListStringStack();
    12.  
    13. private:
    14. QPointer<QStringListModel> StringListModel;
    15. QStringList StackList;
    16. int MaxStackSize;
    17.  
    18. public:
    19. void addMessage(QString & message);
    20. void setMaxStackSize(int value);
    21.  
    22. };
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. WidgetListStringStack::WidgetListStringStack( QWidget* parent /*= 0*/, Qt::WFlags flags /*= 0*/ ) : QWidget(parent, flags)
    2. {
    3. setupUi(this);
    4. setMaxStackSize(100);
    5. StringListModel=new QStringListModel();
    6. }
    7.  
    8.  
    9. WidgetListStringStack::~WidgetListStringStack()
    10. {
    11. }
    12.  
    13. void WidgetListStringStack::setMaxStackSize(int value)
    14. {
    15. MaxStackSize = value;
    16. }
    17.  
    18. void WidgetListStringStack::addMessage(QString & message)
    19. {
    20. StackList.append(message);
    21. if (StackList.size() > MaxStackSize)
    22. StackList.removeFirst();
    23.  
    24. StringListModel->setStringList(StackList);
    25.  
    26. listView->setModel(StringListModel);
    27. }
    To copy to clipboard, switch view to plain text mode 

    I filled it using a timer with 100ms and it does not flicker and looks very smooth.

    However I need to look at the last item by default. How do I reset the view at every addMessage() call ?

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

    Default Re: Looking for list-widget like string stack class

    What do you mean by reset? You wish to scroll the view to the end? Just take a look at QAbstractItemView docs and you'll see.

    Anyway, I'd rather use a simple model with a circular buffer with a regular QListView.

Similar Threads

  1. MVC - Abstract Widget Base Class - setupUI
    By SenSej in forum Newbie
    Replies: 0
    Last Post: 13th October 2008, 11:44
  2. Replies: 1
    Last Post: 7th August 2007, 09:27
  3. QWidget display on 2 stack widget page
    By spawnwj in forum Qt Programming
    Replies: 3
    Last Post: 4th September 2006, 13:07

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
  •  
Qt is a trademark of The Qt Company.