PDA

View Full Version : QListView force redraw.



Dave Stewart
19th December 2007, 10:33
Hi there, first post to the forums. Hope someone can help me.

I have a list view which is populated by any of a number of background threads (mutex locked of course ;-). The problem I have is that the list view only redraws with the new rows when I resize it (it's in a docking window).

I guess I need to emit a signal to get it to redraw but I've tried every signal I can find and still no joy. Code sample follows. What am I doing wrong. I guess I need to get the main event loop to fire but how do I send an appropriate event from another thread?


class MessageList : public QListWidget
{
Q_OBJECT
public:
MessageList(QWidget* pW) : QListWidget(pW) {}

void logMessage(const std::string& sMessage) {
boost::mutex::scoped_lock scoped_lock(m_oMessageListMutex);
QListWidgetItem *item = new QListWidgetItem(sMessage.c_str(), this);
setCurrentItem (item);
update();
emit itemChanged(item);
emit itemActivated (item);
emit itemClicked (item);
emit itemDoubleClicked (item);
emit itemEntered (item);
emit itemPressed (item);
}

protected:
boost::mutex m_oMessageListMutex;
};

Many thanks
Dave

wysota
19th December 2007, 10:40
I have a list view which is populated by any of a number of background threads (mutex locked of course ;-).
That won't work. Mutex or no mutex... You can't update the list from within external threads. Instead emit a signal from other threads that will be connected to a custom slot in the view that will add the items. You won't need any special treatment then.

Dave Stewart
19th December 2007, 12:26
That won't work. Mutex or no mutex... You can't update the list from within external threads. Instead emit a signal from other threads that will be connected to a custom slot in the view that will add the items. You won't need any special treatment then.

Hi wysota, thanks for the reply. Your suggestion seems straight forward enough but I can't get it to work.

I've defined the following in MainWindow.h in class MainWindow



void logMessage(const std::string& sMessage) {
boost::mutex::scoped_lock scoped_lock(m_oMessageListMutex);
emit logMessageSignal(QString(sMessage.c_str()));
}

signals:
void logMessageSignal(QString& sMessage);

public slots:
void messageReceived(QString& sMessage) {
QListWidgetItem *item = new QListWidgetItem(sMessage, messagesListWidget);
}

logMessage() is called from the worker threads.

In MainWindow.cpp I have connected as follows (called from the main thread):


connect(this, SIGNAL(logMessageSignal(QString& sMessage)), this, SLOT(messageReceived(QString& sMessage)));


I get a runtime error:

Object::connect: No such signal MainWindow::logMessageSignal(QString&sMessage)
Object::connect: (sender name: 'MainWindow')
Object::connect: (receiver name: 'MainWindow')

cheers
Dave

wysota
19th December 2007, 13:42
Don't pass parameter names into signal and slot signatures.

http://www.qtcentre.org/forum/faq.php?faq=qt_signalslot#faq_qt_signalslot_with_n ames

Dave Stewart
19th December 2007, 14:41
Don't pass parameter names into signal and slot signatures.

http://www.qtcentre.org/forum/faq.php?faq=qt_signalslot#faq_qt_signalslot_with_n ames

Thanks for that. I'll check the FAQS first in future.

Cheers
Dave