diff options
author | Ievgenii Meshcheriakov <ievgenii.meshcheriakov@qt.io> | 2023-02-21 16:23:57 +0100 |
---|---|---|
committer | Ievgenii Meshcheriakov <ievgenii.meshcheriakov@qt.io> | 2023-02-27 15:55:07 +0100 |
commit | 742e79312fc98711f68749937d0db433d961f546 (patch) | |
tree | 8e80bb6c9b6b9d0e2ccd2d1270e1237c223eac7d /examples | |
parent | 57d25b3a535736aea7edcec1f8466984e36822de (diff) |
chat example: Update code style
Use lambdas instead of slots where practical.
Consistently use signals to transmit D-Bus messages.
Extract a local variable for the used D-Bus connection.
Task-number: QTBUG-111366
Pick-to: 6.5
Change-Id: Icc6667e1392ada1b7d3b33c4e4b32917dd648390
Reviewed-by: MÃ¥rten Nordheim <marten.nordheim@qt.io>
Diffstat (limited to 'examples')
-rw-r--r-- | examples/dbus/chat/chat.cpp | 84 | ||||
-rw-r--r-- | examples/dbus/chat/chat.h | 9 |
2 files changed, 27 insertions, 66 deletions
diff --git a/examples/dbus/chat/chat.cpp b/examples/dbus/chat/chat.cpp index 2eea46b014..cced911955 100644 --- a/examples/dbus/chat/chat.cpp +++ b/examples/dbus/chat/chat.cpp @@ -12,72 +12,48 @@ ChatMainWindow::ChatMainWindow() { setupUi(this); - sendButton->setEnabled(false); - connect(messageLineEdit, &QLineEdit::textChanged, - this, &ChatMainWindow::textChangedSlot); - connect(sendButton, &QPushButton::clicked, - this, &ChatMainWindow::sendClickedSlot); + connect(messageLineEdit, &QLineEdit::textChanged, this, + [this](const QString &newText) { sendButton->setEnabled(!newText.isEmpty()); }); + connect(sendButton, &QPushButton::clicked, this, [this]() { + emit message(m_nickname, messageLineEdit->text()); + messageLineEdit->clear(); + }); connect(actionChangeNickname, &QAction::triggered, this, &ChatMainWindow::changeNickname); - connect(actionAboutQt, &QAction::triggered, - this, &ChatMainWindow::aboutQt); - connect(qApp, &QApplication::lastWindowClosed, - this, &ChatMainWindow::exiting); + connect(actionAboutQt, &QAction::triggered, this, [this]() { QMessageBox::aboutQt(this); }); + connect(qApp, &QApplication::lastWindowClosed, this, + [this]() { emit action(m_nickname, tr("leaves the chat")); }); // add our D-Bus interface and connect to D-Bus new ChatAdaptor(this); - QDBusConnection::sessionBus().registerObject("/", this); - org::example::chat *iface; - iface = new org::example::chat(QString(), QString(), QDBusConnection::sessionBus(), this); - QDBusConnection::sessionBus().connect(QString(), QString(), "org.example.chat", "message", this, SLOT(messageSlot(QString,QString))); - connect(iface, &org::example::chat::action, - this, &ChatMainWindow::actionSlot); + auto connection = QDBusConnection::sessionBus(); + connection.registerObject("/", this); - if (!changeNickname(true)) - QMetaObject::invokeMethod(qApp, &QApplication::quit, Qt::QueuedConnection); -} - -void ChatMainWindow::rebuildHistory() -{ - QString history = m_messages.join( QLatin1String("\n" ) ); - chatHistory->setPlainText(history); -} + using org::example::chat; -void ChatMainWindow::messageSlot(const QString &nickname, const QString &text) -{ - QString msg( QLatin1String("<%1> %2") ); - msg = msg.arg(nickname, text); - m_messages.append(msg); + auto *iface = new chat({}, {}, connection, this); + connect(iface, &chat::message, this, [this](const QString &nickname, const QString &text) { + displayMessage(tr("<%1> %2").arg(nickname, text)); + }); + connect(iface, &chat::action, this, [this](const QString &nickname, const QString &text) { + displayMessage(tr("* %1 %2").arg(nickname, text)); + }); - if (m_messages.count() > 100) - m_messages.removeFirst(); - rebuildHistory(); + if (!changeNickname(true)) + QMetaObject::invokeMethod(qApp, &QApplication::quit, Qt::QueuedConnection); } -void ChatMainWindow::actionSlot(const QString &nickname, const QString &text) +void ChatMainWindow::displayMessage(const QString &message) { - QString msg( QLatin1String("* %1 %2") ); - msg = msg.arg(nickname, text); - m_messages.append(msg); + m_messages.append(message); if (m_messages.count() > 100) m_messages.removeFirst(); - rebuildHistory(); -} - -void ChatMainWindow::textChangedSlot(const QString &newText) -{ - sendButton->setEnabled(!newText.isEmpty()); -} -void ChatMainWindow::sendClickedSlot() -{ - QDBusMessage msg = QDBusMessage::createSignal("/", "org.example.chat", "message"); - msg << m_nickname << messageLineEdit->text(); - QDBusConnection::sessionBus().send(msg); - messageLineEdit->setText(QString()); + auto history = m_messages.join(QLatin1String("\n")); + chatHistory->setPlainText(history); } bool ChatMainWindow::changeNickname(bool initial) @@ -99,16 +75,6 @@ bool ChatMainWindow::changeNickname(bool initial) return false; } -void ChatMainWindow::aboutQt() -{ - QMessageBox::aboutQt(this); -} - -void ChatMainWindow::exiting() -{ - emit action(m_nickname, QLatin1String("leaves the chat")); -} - int main(int argc, char **argv) { QApplication app(argc, argv); diff --git a/examples/dbus/chat/chat.h b/examples/dbus/chat/chat.h index 0cbca29590..f7706eeeba 100644 --- a/examples/dbus/chat/chat.h +++ b/examples/dbus/chat/chat.h @@ -16,20 +16,15 @@ class ChatMainWindow: public QMainWindow, Ui::ChatMainWindow public: ChatMainWindow(); - void rebuildHistory(); +private: + void displayMessage(const QString &message); signals: void message(const QString &nickname, const QString &text); void action(const QString &nickname, const QString &text); private slots: - void messageSlot(const QString &nickname, const QString &text); - void actionSlot(const QString &nickname, const QString &text); - void textChangedSlot(const QString &newText); - void sendClickedSlot(); bool changeNickname(bool initial = false); - void aboutQt(); - void exiting(); }; #endif // CHAT_H |