diff --git a/examples/dbus/chat/CMakeLists.txt b/examples/dbus/chat/CMakeLists.txt index 561b2234608..b6b17e294d5 100644 --- a/examples/dbus/chat/CMakeLists.txt +++ b/examples/dbus/chat/CMakeLists.txt @@ -32,7 +32,6 @@ qt_add_dbus_adaptor(chat_SRCS qt_add_executable(chat chat.cpp chat.h chatmainwindow.ui - chatsetnickname.ui ${chat_SRCS} ) diff --git a/examples/dbus/chat/chat.cpp b/examples/dbus/chat/chat.cpp index 6e7c670834e..31b9af4ae72 100644 --- a/examples/dbus/chat/chat.cpp +++ b/examples/dbus/chat/chat.cpp @@ -2,6 +2,7 @@ // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause #include +#include #include #include "chat.h" @@ -9,7 +10,6 @@ #include "chat_interface.h" ChatMainWindow::ChatMainWindow() - : m_nickname(QLatin1String("nickname")) { setupUi(this); sendButton->setEnabled(false); @@ -36,11 +36,8 @@ ChatMainWindow::ChatMainWindow() connect(iface, &org::example::chat::action, this, &ChatMainWindow::actionSlot); - NicknameDialog dialog; - dialog.cancelButton->setVisible(false); - dialog.exec(); - m_nickname = dialog.nickname->text().trimmed(); - emit action(m_nickname, QLatin1String("joins the chat")); + if (!changeNickname(true)) + QMetaObject::invokeMethod(qApp, &QApplication::quit, Qt::QueuedConnection); } ChatMainWindow::~ChatMainWindow() @@ -89,14 +86,23 @@ void ChatMainWindow::sendClickedSlot() messageLineEdit->setText(QString()); } -void ChatMainWindow::changeNickname() +bool ChatMainWindow::changeNickname(bool initial) { - NicknameDialog dialog(this); - if (dialog.exec() == QDialog::Accepted) { - QString old = m_nickname; - m_nickname = dialog.nickname->text().trimmed(); - emit action(old, QString("is now known as %1").arg(m_nickname)); + auto newNickname = QInputDialog::getText(this, tr("Set nickname"), tr("New nickname:")); + newNickname = newNickname.trimmed(); + + if (!newNickname.isEmpty()) { + auto old = m_nickname; + m_nickname = newNickname; + + if (initial) + emit action(m_nickname, tr("joins the chat")); + else + emit action(old, tr("is now known as %1").arg(m_nickname)); + return true; } + + return false; } void ChatMainWindow::aboutQt() @@ -109,12 +115,6 @@ void ChatMainWindow::exiting() emit action(m_nickname, QLatin1String("leaves the chat")); } -NicknameDialog::NicknameDialog(QWidget *parent) - : QDialog(parent) -{ - setupUi(this); -} - 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 aabafcdc951..8a6551aaaee 100644 --- a/examples/dbus/chat/chat.h +++ b/examples/dbus/chat/chat.h @@ -7,7 +7,6 @@ #include #include "ui_chatmainwindow.h" -#include "ui_chatsetnickname.h" class ChatMainWindow: public QMainWindow, Ui::ChatMainWindow { @@ -29,16 +28,9 @@ private slots: void actionSlot(const QString &nickname, const QString &text); void textChangedSlot(const QString &newText); void sendClickedSlot(); - void changeNickname(); + bool changeNickname(bool initial = false); void aboutQt(); void exiting(); }; -class NicknameDialog: public QDialog, public Ui::NicknameDialog -{ - Q_OBJECT -public: - NicknameDialog(QWidget *parent = nullptr); -}; - #endif // CHAT_H diff --git a/examples/dbus/chat/chat.pro b/examples/dbus/chat/chat.pro index 93d3178177d..2b4630d5f11 100644 --- a/examples/dbus/chat/chat.pro +++ b/examples/dbus/chat/chat.pro @@ -2,7 +2,7 @@ QT += dbus widgets HEADERS += chat.h SOURCES += chat.cpp -FORMS += chatmainwindow.ui chatsetnickname.ui +FORMS += chatmainwindow.ui DBUS_ADAPTORS += org.example.chat.xml DBUS_INTERFACES += org.example.chat.xml diff --git a/examples/dbus/chat/chatsetnickname.ui b/examples/dbus/chat/chatsetnickname.ui deleted file mode 100644 index fb9894e09fc..00000000000 --- a/examples/dbus/chat/chatsetnickname.ui +++ /dev/null @@ -1,149 +0,0 @@ - - - - - NicknameDialog - - - - 0 - 0 - 396 - 105 - - - - - 1 - 1 - 0 - 0 - - - - Set nickname - - - - 9 - - - 6 - - - - - 0 - - - 6 - - - - - - 1 - 1 - 0 - 0 - - - - New nickname: - - - - - - - - - - - - 0 - - - 6 - - - - - Qt::Horizontal - - - - 131 - 31 - - - - - - - - OK - - - - - - - Cancel - - - - - - - Qt::Horizontal - - - - 40 - 20 - - - - - - - - - - - - - okButton - clicked() - NicknameDialog - accept() - - - 278 - 253 - - - 96 - 254 - - - - - cancelButton - clicked() - NicknameDialog - reject() - - - 369 - 253 - - - 179 - 282 - - - - -