void PagesWidget::addPage(const QString& userId, const QString& username) { ChatPageWidget* chatPage = new ChatPageWidget(userId, this); chatPage->setUsername(username); connect(chatPage, &ChatPageWidget::messageSent, this, &PagesWidget::onMessageSent); addWidget(chatPage); qDebug() << "page" << userId << "added" << count(); }
void PagesWidget::addPage(int friendId, const QString& username) { ChatPageWidget* chatPage = new ChatPageWidget(friendId, this); chatPage->setUsername(username); connect(chatPage, &ChatPageWidget::sendMessage, this, &PagesWidget::onMessageSent); connect(chatPage, &ChatPageWidget::sendAction, this, &PagesWidget::onActionToSend); addWidget(chatPage); qDebug() << "page" << friendId << "added" << count(); }
// if preformance would be critical, replace with QHash<QString userId, ChatPageWidget*> ChatPageWidget* PagesWidget::widget(const QString& userId) const { for (int i = 0; i < count(); i ++) { ChatPageWidget* chatPage = dynamic_cast<ChatPageWidget*>(QStackedWidget::widget(i)); if (chatPage != nullptr && chatPage->getUserId() == userId) { return chatPage; } } return nullptr; }
void PagesWidget::onMessageSent(const QString &message) { ChatPageWidget* chatPage = static_cast<ChatPageWidget*>(sender()); emit messageSent(chatPage->getUserId(), message); }
void PagesWidget::onActionToSend(const QString &action) { ChatPageWidget* chatPage = static_cast<ChatPageWidget*>(sender()); emit sendAction(chatPage->getFriendId(), action); }