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
Change-Id: I56ed99d69d031e7ef06711fc7ec112311c974ca6
Reviewed-by: Mårten Nordheim <marten.nordheim@qt.io>
(cherry picked from commit 8b4865759029250d0f75c84ca0b1312429d24086)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
This commit is contained in:
Ievgenii Meshcheriakov 2023-02-21 15:19:47 +01:00 committed by Qt Cherry-pick Bot
parent da062aeda4
commit 2f15ece9a7
5 changed files with 20 additions and 178 deletions

View File

@ -32,7 +32,6 @@ qt_add_dbus_adaptor(chat_SRCS
qt_add_executable(chat
chat.cpp chat.h
chatmainwindow.ui
chatsetnickname.ui
${chat_SRCS}
)

View File

@ -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);

View File

@ -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

View File

@ -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

View File

@ -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>