mirror of
https://github.com/crystalidea/qt6windows7.git
synced 2025-07-02 23:35:28 +08:00
qt 6.5.1 original
This commit is contained in:
54
examples/dbus/chat/CMakeLists.txt
Normal file
54
examples/dbus/chat/CMakeLists.txt
Normal file
@ -0,0 +1,54 @@
|
||||
# Copyright (C) 2022 The Qt Company Ltd.
|
||||
# SPDX-License-Identifier: BSD-3-Clause
|
||||
|
||||
cmake_minimum_required(VERSION 3.16)
|
||||
project(chat LANGUAGES CXX)
|
||||
|
||||
set(CMAKE_INCLUDE_CURRENT_DIR ON)
|
||||
|
||||
if(NOT DEFINED INSTALL_EXAMPLESDIR)
|
||||
set(INSTALL_EXAMPLESDIR "examples")
|
||||
endif()
|
||||
|
||||
set(INSTALL_EXAMPLEDIR "${INSTALL_EXAMPLESDIR}/dbus/chat")
|
||||
|
||||
find_package(Qt6 REQUIRED COMPONENTS Core DBus Gui Widgets)
|
||||
|
||||
qt_standard_project_setup()
|
||||
|
||||
set(chat_SRCS)
|
||||
qt_add_dbus_interface(chat_SRCS
|
||||
org.example.chat.xml
|
||||
chat_interface
|
||||
)
|
||||
|
||||
qt_add_dbus_adaptor(chat_SRCS
|
||||
org.example.chat.xml
|
||||
qobject.h
|
||||
QObject
|
||||
chat_adaptor
|
||||
)
|
||||
|
||||
qt_add_executable(chat
|
||||
chat.cpp chat.h
|
||||
chatmainwindow.ui
|
||||
${chat_SRCS}
|
||||
)
|
||||
|
||||
set_target_properties(chat PROPERTIES
|
||||
WIN32_EXECUTABLE TRUE
|
||||
MACOSX_BUNDLE TRUE
|
||||
)
|
||||
|
||||
target_link_libraries(chat PRIVATE
|
||||
Qt6::Core
|
||||
Qt6::DBus
|
||||
Qt6::Gui
|
||||
Qt6::Widgets
|
||||
)
|
||||
|
||||
install(TARGETS chat
|
||||
RUNTIME DESTINATION "${INSTALL_EXAMPLEDIR}"
|
||||
BUNDLE DESTINATION "${INSTALL_EXAMPLEDIR}"
|
||||
LIBRARY DESTINATION "${INSTALL_EXAMPLEDIR}"
|
||||
)
|
91
examples/dbus/chat/chat.cpp
Normal file
91
examples/dbus/chat/chat.cpp
Normal file
@ -0,0 +1,91 @@
|
||||
// Copyright (C) 2016 The Qt Company Ltd.
|
||||
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
|
||||
|
||||
#include <QApplication>
|
||||
#include <QInputDialog>
|
||||
#include <QMessageBox>
|
||||
|
||||
#include "chat.h"
|
||||
#include "chat_adaptor.h"
|
||||
#include "chat_interface.h"
|
||||
|
||||
ChatMainWindow::ChatMainWindow()
|
||||
{
|
||||
setupUi(this);
|
||||
|
||||
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, [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);
|
||||
|
||||
auto connection = QDBusConnection::sessionBus();
|
||||
connection.registerObject("/", this);
|
||||
|
||||
using org::example::chat;
|
||||
|
||||
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 (!changeNickname(true))
|
||||
QMetaObject::invokeMethod(qApp, &QApplication::quit, Qt::QueuedConnection);
|
||||
}
|
||||
|
||||
void ChatMainWindow::displayMessage(const QString &message)
|
||||
{
|
||||
m_messages.append(message);
|
||||
|
||||
if (m_messages.count() > 100)
|
||||
m_messages.removeFirst();
|
||||
|
||||
auto history = m_messages.join(QLatin1String("\n"));
|
||||
chatHistory->setPlainText(history);
|
||||
}
|
||||
|
||||
bool ChatMainWindow::changeNickname(bool initial)
|
||||
{
|
||||
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;
|
||||
}
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
QApplication app(argc, argv);
|
||||
|
||||
if (!QDBusConnection::sessionBus().isConnected()) {
|
||||
qWarning("Cannot connect to the D-Bus session bus.\n"
|
||||
"Please check your system settings and try again.\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
ChatMainWindow chat;
|
||||
chat.show();
|
||||
return app.exec();
|
||||
}
|
30
examples/dbus/chat/chat.h
Normal file
30
examples/dbus/chat/chat.h
Normal file
@ -0,0 +1,30 @@
|
||||
// Copyright (C) 2016 The Qt Company Ltd.
|
||||
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
|
||||
|
||||
#ifndef CHAT_H
|
||||
#define CHAT_H
|
||||
|
||||
#include <QStringList>
|
||||
|
||||
#include "ui_chatmainwindow.h"
|
||||
|
||||
class ChatMainWindow: public QMainWindow, Ui::ChatMainWindow
|
||||
{
|
||||
Q_OBJECT
|
||||
QString m_nickname;
|
||||
QStringList m_messages;
|
||||
public:
|
||||
ChatMainWindow();
|
||||
|
||||
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:
|
||||
bool changeNickname(bool initial = false);
|
||||
};
|
||||
|
||||
#endif // CHAT_H
|
14
examples/dbus/chat/chat.pro
Normal file
14
examples/dbus/chat/chat.pro
Normal file
@ -0,0 +1,14 @@
|
||||
QT += dbus widgets
|
||||
|
||||
HEADERS += chat.h
|
||||
SOURCES += chat.cpp
|
||||
FORMS += chatmainwindow.ui
|
||||
|
||||
DBUS_ADAPTORS += org.example.chat.xml
|
||||
DBUS_INTERFACES += org.example.chat.xml
|
||||
|
||||
CONFIG += no_batch # work around QTBUG-96513
|
||||
|
||||
# install
|
||||
target.path = $$[QT_INSTALL_EXAMPLES]/dbus/chat
|
||||
INSTALLS += target
|
185
examples/dbus/chat/chatmainwindow.ui
Normal file
185
examples/dbus/chat/chatmainwindow.ui
Normal file
@ -0,0 +1,185 @@
|
||||
<ui version="4.0" >
|
||||
<author></author>
|
||||
<comment></comment>
|
||||
<exportmacro></exportmacro>
|
||||
<class>ChatMainWindow</class>
|
||||
<widget class="QMainWindow" name="ChatMainWindow" >
|
||||
<property name="geometry" >
|
||||
<rect>
|
||||
<x>0</x>
|
||||
<y>0</y>
|
||||
<width>800</width>
|
||||
<height>600</height>
|
||||
</rect>
|
||||
</property>
|
||||
<property name="windowTitle" >
|
||||
<string>Qt D-Bus Chat</string>
|
||||
</property>
|
||||
<widget class="QWidget" name="centralwidget" >
|
||||
<layout class="QHBoxLayout" >
|
||||
<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="QTextBrowser" name="chatHistory" >
|
||||
<property name="acceptDrops" >
|
||||
<bool>false</bool>
|
||||
</property>
|
||||
<property name="toolTip" >
|
||||
<string>Messages sent and received from other users</string>
|
||||
</property>
|
||||
<property name="acceptRichText" >
|
||||
<bool>true</bool>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item>
|
||||
<layout class="QHBoxLayout" >
|
||||
<property name="margin" >
|
||||
<number>0</number>
|
||||
</property>
|
||||
<property name="spacing" >
|
||||
<number>6</number>
|
||||
</property>
|
||||
<item>
|
||||
<widget class="QLabel" name="label" >
|
||||
<property name="text" >
|
||||
<string>Message:</string>
|
||||
</property>
|
||||
<property name="buddy" >
|
||||
<cstring>messageLineEdit</cstring>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item>
|
||||
<widget class="QLineEdit" name="messageLineEdit" />
|
||||
</item>
|
||||
<item>
|
||||
<widget class="QPushButton" name="sendButton" >
|
||||
<property name="sizePolicy" >
|
||||
<sizepolicy>
|
||||
<hsizetype>1</hsizetype>
|
||||
<vsizetype>0</vsizetype>
|
||||
<horstretch>0</horstretch>
|
||||
<verstretch>0</verstretch>
|
||||
</sizepolicy>
|
||||
</property>
|
||||
<property name="toolTip" >
|
||||
<string>Sends a message to other people</string>
|
||||
</property>
|
||||
<property name="whatsThis" >
|
||||
<string/>
|
||||
</property>
|
||||
<property name="text" >
|
||||
<string>Send</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
</layout>
|
||||
</item>
|
||||
</layout>
|
||||
</item>
|
||||
</layout>
|
||||
</widget>
|
||||
<widget class="QMenuBar" name="menubar" >
|
||||
<property name="geometry" >
|
||||
<rect>
|
||||
<x>0</x>
|
||||
<y>0</y>
|
||||
<width>800</width>
|
||||
<height>31</height>
|
||||
</rect>
|
||||
</property>
|
||||
<widget class="QMenu" name="menuQuit" >
|
||||
<property name="title" >
|
||||
<string>Help</string>
|
||||
</property>
|
||||
<addaction name="actionAboutQt" />
|
||||
</widget>
|
||||
<widget class="QMenu" name="menuFile" >
|
||||
<property name="title" >
|
||||
<string>File</string>
|
||||
</property>
|
||||
<addaction name="actionChangeNickname" />
|
||||
<addaction name="separator" />
|
||||
<addaction name="actionQuit" />
|
||||
</widget>
|
||||
<addaction name="menuFile" />
|
||||
<addaction name="menuQuit" />
|
||||
</widget>
|
||||
<widget class="QStatusBar" name="statusbar" />
|
||||
<action name="actionQuit" >
|
||||
<property name="text" >
|
||||
<string>Quit</string>
|
||||
</property>
|
||||
<property name="shortcut" >
|
||||
<string>Ctrl+Q</string>
|
||||
</property>
|
||||
</action>
|
||||
<action name="actionAboutQt" >
|
||||
<property name="text" >
|
||||
<string>About Qt...</string>
|
||||
</property>
|
||||
</action>
|
||||
<action name="actionChangeNickname" >
|
||||
<property name="text" >
|
||||
<string>Change nickname...</string>
|
||||
</property>
|
||||
<property name="shortcut" >
|
||||
<string>Ctrl+N</string>
|
||||
</property>
|
||||
</action>
|
||||
</widget>
|
||||
<pixmapfunction></pixmapfunction>
|
||||
<tabstops>
|
||||
<tabstop>chatHistory</tabstop>
|
||||
<tabstop>messageLineEdit</tabstop>
|
||||
<tabstop>sendButton</tabstop>
|
||||
</tabstops>
|
||||
<resources/>
|
||||
<connections>
|
||||
<connection>
|
||||
<sender>messageLineEdit</sender>
|
||||
<signal>returnPressed()</signal>
|
||||
<receiver>sendButton</receiver>
|
||||
<slot>animateClick()</slot>
|
||||
<hints>
|
||||
<hint type="sourcelabel" >
|
||||
<x>299</x>
|
||||
<y>554</y>
|
||||
</hint>
|
||||
<hint type="destinationlabel" >
|
||||
<x>744</x>
|
||||
<y>551</y>
|
||||
</hint>
|
||||
</hints>
|
||||
</connection>
|
||||
<connection>
|
||||
<sender>actionQuit</sender>
|
||||
<signal>triggered(bool)</signal>
|
||||
<receiver>ChatMainWindow</receiver>
|
||||
<slot>close()</slot>
|
||||
<hints>
|
||||
<hint type="sourcelabel" >
|
||||
<x>-1</x>
|
||||
<y>-1</y>
|
||||
</hint>
|
||||
<hint type="destinationlabel" >
|
||||
<x>399</x>
|
||||
<y>299</y>
|
||||
</hint>
|
||||
</hints>
|
||||
</connection>
|
||||
</connections>
|
||||
</ui>
|
15
examples/dbus/chat/org.example.chat.xml
Normal file
15
examples/dbus/chat/org.example.chat.xml
Normal file
@ -0,0 +1,15 @@
|
||||
<!DOCTYPE node PUBLIC "-//freedesktop//DTD D-BUS Object Introspection 1.0//EN"
|
||||
"http://www.freedesktop.org/standards/dbus/1.0/introspect.dtd">
|
||||
<node>
|
||||
<interface name="org.example.chat">
|
||||
<signal name="message">
|
||||
<arg name="nickname" type="s" direction="out"/>
|
||||
<arg name="text" type="s" direction="out"/>
|
||||
</signal>
|
||||
<signal name="action">
|
||||
<arg name="nickname" type="s" direction="out"/>
|
||||
<arg name="text" type="s" direction="out"/>
|
||||
</signal>
|
||||
</interface>
|
||||
</node>
|
||||
|
Reference in New Issue
Block a user