diff options
Diffstat (limited to 'examples/corelib/ipc/localfortuneserver/server.cpp')
-rw-r--r-- | examples/corelib/ipc/localfortuneserver/server.cpp | 36 |
1 files changed, 18 insertions, 18 deletions
diff --git a/examples/corelib/ipc/localfortuneserver/server.cpp b/examples/corelib/ipc/localfortuneserver/server.cpp index 2eee4760be..9b8b08bd17 100644 --- a/examples/corelib/ipc/localfortuneserver/server.cpp +++ b/examples/corelib/ipc/localfortuneserver/server.cpp @@ -60,22 +60,21 @@ Server::Server(QWidget *parent) : QDialog(parent) { - statusLabel = new QLabel; - statusLabel->setWordWrap(true); - quitButton = new QPushButton(tr("Quit")); - quitButton->setAutoDefault(false); + setWindowFlags(windowFlags() & ~Qt::WindowContextHelpButtonHint); server = new QLocalServer(this); if (!server->listen("fortune")) { - QMessageBox::critical(this, tr("Fortune Server"), + QMessageBox::critical(this, tr("Local Fortune Server"), tr("Unable to start the server: %1.") .arg(server->errorString())); close(); return; } + QLabel *statusLabel = new QLabel; + statusLabel->setWordWrap(true); statusLabel->setText(tr("The server is running.\n" - "Run the Fortune Client example now.")); + "Run the Local Fortune Client example now.")); fortunes << tr("You've been leading a dog's life. Stay off the furniture.") << tr("You've got to think about tomorrow.") @@ -85,35 +84,36 @@ Server::Server(QWidget *parent) << tr("You cannot kill time without injuring eternity.") << tr("Computers are not intelligent. They only think they are."); - connect(quitButton, SIGNAL(clicked()), this, SLOT(close())); - connect(server, SIGNAL(newConnection()), this, SLOT(sendFortune())); + QPushButton *quitButton = new QPushButton(tr("Quit")); + quitButton->setAutoDefault(false); + connect(quitButton, &QPushButton::clicked, this, &Server::close); + connect(server, &QLocalServer::newConnection, this, &Server::sendFortune); QHBoxLayout *buttonLayout = new QHBoxLayout; buttonLayout->addStretch(1); buttonLayout->addWidget(quitButton); buttonLayout->addStretch(1); - QVBoxLayout *mainLayout = new QVBoxLayout; + QVBoxLayout *mainLayout = new QVBoxLayout(this); mainLayout->addWidget(statusLabel); mainLayout->addLayout(buttonLayout); - setLayout(mainLayout); - setWindowTitle(tr("Fortune Server")); + setWindowTitle(QGuiApplication::applicationDisplayName()); } void Server::sendFortune() { QByteArray block; QDataStream out(&block, QIODevice::WriteOnly); - out.setVersion(QDataStream::Qt_4_0); - out << (quint32)0; - out << fortunes.at(qrand() % fortunes.size()); - out.device()->seek(0); - out << (quint32)(block.size() - sizeof(quint32)); + out.setVersion(QDataStream::Qt_5_10); + const int fortuneIndex = QRandomGenerator::bounded(0, fortunes.size()); + const QString &message = fortunes.at(fortuneIndex); + out << quint32(message.size()); + out << message; QLocalSocket *clientConnection = server->nextPendingConnection(); - connect(clientConnection, SIGNAL(disconnected()), - clientConnection, SLOT(deleteLater())); + connect(clientConnection, &QLocalSocket::disconnected, + clientConnection, &QLocalSocket::deleteLater); clientConnection->write(block); clientConnection->flush(); |