PDA

View Full Version : QAbstractListModel searching.



ComaWhite
12th June 2009, 06:48
I'm currently having trouble with writing my IRC client. When changing a user's channel status, and doing it my own self it updates fine. But when testing it with another client in a channel. Removing and adding statuses, the QListView model will not update the User's icons unless I move my mouse over the QListView. Currently I'm thinking the way I have it right now is just slow as hell and what not. Is there a better way of optimising the searching of a user?



void
ChannelTab::slotOnMode(const QString &from, const QString &channel, const QString &mode, const QString &nick)
{
if (name() != channel) {
return;
}

enum {
None,
Give,
Take
};
int state = None;

QModelIndex index;
Aki::Irc::User *user = 0;

//Aki::Irc::User *user = d->userList->model()->data(d->userList->currentIndex(), NickListModel::IrcUserRole)
// .value<Aki::Irc::User*>();

for (int i = 0; i < d->userList->count(); ++i) {
index = d->userList->model()->index(i, 0);
user = d->userList->model()->data(index, NickListModel::IrcUserRole).value<Aki::Irc::User*>();
if (user->nick() == nick) {
kDebug() << "found";
break;
}
}

if (!user) {
return;
}

QString modes = user->modes();
bool fromYou = from == d->socket->currentNick();
bool toYou = nick == d->socket->currentNick();

foreach (QChar c, mode) {
if (c == QChar('+')) {
state = Give;
kDebug() << "Give";
} else if (c == QChar('-')) {
state = Take;
kDebug() << "Take";
} else if (c == QChar('o')) {
if (state == Give) {
user->setModes(modes + c);
d->userList->model()->setData(d->userList->currentIndex(),
QVariant::fromValue<Aki::Irc::User*>(user),
NickListModel::IrcUserRole);
d->channelOutput->addMode(from, nick, c, toYou, fromYou);
} else if (state == Take) {
user->removeModes(c);
d->userList->model()->setData(d->userList->currentIndex(),
QVariant::fromValue<Aki::Irc::User*>(user),
NickListModel::IrcUserRole);
d->channelOutput->addRemoveMode(from, nick, c, toYou, fromYou);
}
} else if (c == QChar('v')) {
if (state == Give) {
user->setModes(modes + c);
d->userList->model()->setData(d->userList->currentIndex(),
QVariant::fromValue<Aki::Irc::User*>(user),
NickListModel::IrcUserRole);
d->channelOutput->addMode(from, nick, c, toYou, fromYou);
} else if (state == Take) {
user->removeModes(c);
d->userList->model()->setData(d->userList->currentIndex(),
QVariant::fromValue<Aki::Irc::User*>(user),
NickListModel::IrcUserRole);
d->channelOutput->addRemoveMode(from, nick, c, toYou, fromYou);
}
} else if (c == QChar('h')) {
if (state == Give) {
user->setModes(modes + c);
d->userList->model()->setData(d->userList->currentIndex(),
QVariant::fromValue<Aki::Irc::User*>(user),
NickListModel::IrcUserRole);
d->channelOutput->addMode(from, nick, c, toYou, fromYou);
} else if (state == Take) {
user->removeModes(c);
d->userList->model()->setData(d->userList->currentIndex(),
QVariant::fromValue<Aki::Irc::User*>(user),
NickListModel::IrcUserRole);
d->channelOutput->addRemoveMode(from, nick, c, toYou, fromYou);
}
}
}
}

wysota
15th June 2009, 18:41
I can't make much from the code above but you are probably not emitting proper signals from your model (like dataChanged() from setData()).