diff options
author | Ievgenii Meshcheriakov <ievgenii.meshcheriakov@qt.io> | 2023-02-21 15:19:47 +0100 |
---|---|---|
committer | Ievgenii Meshcheriakov <ievgenii.meshcheriakov@qt.io> | 2023-02-27 15:55:07 +0100 |
commit | 8b4865759029250d0f75c84ca0b1312429d24086 (patch) | |
tree | ab9541cab03867b35072fae2b2048baaf0f73d4b /examples | |
parent | acc6dc9c09ae778bced33c01f0a767f291cd279f (diff) |
chat example: Use QInputDialog to get a user nickname
This gets rid of a custom form. Also quit the application if user
closes the input dialog without entering a nickname.
Task-number: QTBUG-111366
Pick-to: 6.5
Change-Id: I56ed99d69d031e7ef06711fc7ec112311c974ca6
Reviewed-by: MÃ¥rten Nordheim <marten.nordheim@qt.io>
Diffstat (limited to 'examples')
-rw-r--r-- | examples/dbus/chat/CMakeLists.txt | 1 | ||||
-rw-r--r-- | examples/dbus/chat/chat.cpp | 36 | ||||
-rw-r--r-- | examples/dbus/chat/chat.h | 10 | ||||
-rw-r--r-- | examples/dbus/chat/chat.pro | 2 | ||||
-rw-r--r-- | examples/dbus/chat/chatsetnickname.ui | 149 |
5 files changed, 20 insertions, 178 deletions
diff --git a/examples/dbus/chat/CMakeLists.txt b/examples/dbus/chat/CMakeLists.txt index 561b223460..b6b17e294d 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 6e7c670834..31b9af4ae7 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 <QApplication> +#include <QInputDialog> #include <QMessageBox> #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 aabafcdc95..8a6551aaae 100644 --- a/examples/dbus/chat/chat.h +++ b/examples/dbus/chat/chat.h @@ -7,7 +7,6 @@ #include <QStringList> #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 93d3178177..2b4630d5f1 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 fb9894e09f..0000000000 --- a/examples/dbus/chat/chatsetnickname.ui +++ /dev/null @@ -1,149 +0,0 @@ -<ui version="4.0" > - <author></author> - <comment></comment> - <exportmacro></exportmacro> - <class>NicknameDialog</class> - <widget class="QDialog" name="NicknameDialog" > - <property name="geometry" > - <rect> - <x>0</x> - <y>0</y> - <width>396</width> - <height>105</height> - </rect> - </property> - <property name="sizePolicy" > - <sizepolicy> - <hsizetype>1</hsizetype> - <vsizetype>1</vsizetype> - <horstretch>0</horstretch> - <verstretch>0</verstretch> - </sizepolicy> - </property> - <property name="windowTitle" > - <string>Set nickname</string> - </property> - <layout class="QVBoxLayout" > - <property name="margin" > - <number>9</number> - </property> - <property name="spacing" > - <number>6</number> - </property> - <item> - <layout class="QVBoxLayout" > - <property name="margin" > - <number>0</number> - </property> - <property name="spacing" > - <number>6</number> - </property> - <item> - <widget class="QLabel" name="label" > - <property name="sizePolicy" > - <sizepolicy> - <hsizetype>1</hsizetype> - <vsizetype>1</vsizetype> - <horstretch>0</horstretch> - <verstretch>0</verstretch> - </sizepolicy> - </property> - <property name="text" > - <string>New nickname:</string> - </property> - </widget> - </item> - <item> - <widget class="QLineEdit" name="nickname" /> - </item> - </layout> - </item> - <item> - <layout class="QHBoxLayout" > - <property name="margin" > - <number>0</number> - </property> - <property name="spacing" > - <number>6</number> - </property> - <item> - <spacer> - <property name="orientation" > - <enum>Qt::Horizontal</enum> - </property> - <property name="sizeHint" > - <size> - <width>131</width> - <height>31</height> - </size> - </property> - </spacer> - </item> - <item> - <widget class="QPushButton" name="okButton" > - <property name="text" > - <string>OK</string> - </property> - </widget> - </item> - <item> - <widget class="QPushButton" name="cancelButton" > - <property name="text" > - <string>Cancel</string> - </property> - </widget> - </item> - <item> - <spacer> - <property name="orientation" > - <enum>Qt::Horizontal</enum> - </property> - <property name="sizeHint" > - <size> - <width>40</width> - <height>20</height> - </size> - </property> - </spacer> - </item> - </layout> - </item> - </layout> - </widget> - <pixmapfunction></pixmapfunction> - <resources/> - <connections> - <connection> - <sender>okButton</sender> - <signal>clicked()</signal> - <receiver>NicknameDialog</receiver> - <slot>accept()</slot> - <hints> - <hint type="sourcelabel" > - <x>278</x> - <y>253</y> - </hint> - <hint type="destinationlabel" > - <x>96</x> - <y>254</y> - </hint> - </hints> - </connection> - <connection> - <sender>cancelButton</sender> - <signal>clicked()</signal> - <receiver>NicknameDialog</receiver> - <slot>reject()</slot> - <hints> - <hint type="sourcelabel" > - <x>369</x> - <y>253</y> - </hint> - <hint type="destinationlabel" > - <x>179</x> - <y>282</y> - </hint> - </hints> - </connection> - </connections> -</ui> |