PDA

View Full Version : Looking for list-widget like string stack class



pospiech
23rd November 2008, 11:58
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

wysota
23rd November 2008, 12:46
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.

pospiech
23rd November 2008, 14:28
I see that QStringList is suitable for storing the messages, but how do I display them?

I could use


listwidget->clear();
listwidget->addItems(currentStringList );


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

wysota
23rd November 2008, 18:45
Take a look at QStringListModel.

pospiech
23rd November 2008, 18:58
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.

caduel
23rd November 2008, 19:36
QStringList aList;
// add contents
QStringListModel *slm=new QStringListModel(aList);
QListView *lv = new QListView;
lv->setModel(slm);

// 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:

slm->setStringList(newContents);
(better use insertRows/removeRows/setData, though, otherwise performance will suffer, there might be flickering and the user will lose his selection etc...)

HTH

pospiech
23rd November 2008, 20:01
(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?

wysota
24th November 2008, 09:04
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.

aamer4yu
24th November 2008, 16:25
Or may be you can look at QListWidget :)

pospiech
24th November 2008, 20:31
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.

pospiech
24th November 2008, 21:27
My solution using the ideas posted here looks now like this:


#include "ui/ui_WidgetListStringStack.h"
#include <QtCore/QStringList>
#include <QtGui/QStringListModel>
#include <QtCore/QPointer>

class WidgetListStringStack : public QWidget, protected Ui_WidgetListStringStack
{
Q_OBJECT
public:
WidgetListStringStack(QWidget* parent = 0, Qt::WFlags flags = 0);
virtual ~WidgetListStringStack();

private:
QPointer<QStringListModel> StringListModel;
QStringList StackList;
int MaxStackSize;

public:
void addMessage(QString & message);
void setMaxStackSize(int value);

};




WidgetListStringStack::WidgetListStringStack( QWidget* parent /*= 0*/, Qt::WFlags flags /*= 0*/ ) : QWidget(parent, flags)
{
setupUi(this);
setMaxStackSize(100);
StringListModel=new QStringListModel();
}


WidgetListStringStack::~WidgetListStringStack()
{
}

void WidgetListStringStack::setMaxStackSize(int value)
{
MaxStackSize = value;
}

void WidgetListStringStack::addMessage(QString & message)
{
StackList.append(message);
if (StackList.size() > MaxStackSize)
StackList.removeFirst();

StringListModel->setStringList(StackList);

listView->setModel(StringListModel);
}


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 ?

wysota
25th November 2008, 08:54
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.