首页 > 其他分享 >QDialogButtonBox的用法

QDialogButtonBox的用法

时间:2022-12-31 15:31:22浏览次数:37  
标签:QDialogButtonBox tcpClient mainLayout tr 用法 connect Dialog tcpServerConnection


#include "dialog.h"

#include <QtNetwork>
#include <QtWidgets>

static const int TotalBytes = 50 * 1024 * 1024;
static const int PayloadSize = 64 * 1024; // 64 KB

Dialog::Dialog(QWidget *parent)
: QDialog(parent)
{
clientProgressBar = new QProgressBar;
clientStatusLabel = new QLabel(tr("Client ready"));
serverProgressBar = new QProgressBar;
serverStatusLabel = new QLabel(tr("Server ready"));

startButton = new QPushButton(tr("&Start"));
quitButton = new QPushButton(tr("&Quit"));

buttonBox = new QDialogButtonBox;
buttonBox->addButton(startButton, QDialogButtonBox::ActionRole);
buttonBox->addButton(quitButton, QDialogButtonBox::RejectRole);

connect(startButton, &QAbstractButton::clicked, this, &Dialog::start);
connect(quitButton, &QAbstractButton::clicked, this, &QWidget::close);
connect(&tcpServer, &QTcpServer::newConnection,
this, &Dialog::acceptConnection);
connect(&tcpClient, &QAbstractSocket::connected, this, &Dialog::startTransfer);
connect(&tcpClient, &QIODevice::bytesWritten,
this, &Dialog::updateClientProgress);
connect(&tcpClient, QOverload<QAbstractSocket::SocketError>::of(&QAbstractSocket::error),
this, &Dialog::displayError);

QVBoxLayout *mainLayout = new QVBoxLayout;
mainLayout->addWidget(clientProgressBar);
mainLayout->addWidget(clientStatusLabel);
mainLayout->addWidget(serverProgressBar);
mainLayout->addWidget(serverStatusLabel);
mainLayout->addStretch(1);
mainLayout->addSpacing(10);
mainLayout->addWidget(buttonBox);
setLayout(mainLayout);

setWindowTitle(tr("Loopback"));
}

void Dialog::start()
{
startButton->setEnabled(false);

#ifndef QT_NO_CURSOR
QApplication::setOverrideCursor(Qt::WaitCursor);
#endif

bytesWritten = 0;
bytesReceived = 0;

while (!tcpServer.isListening() && !tcpServer.listen()) {
QMessageBox::StandardButton ret = QMessageBox::critical(this,
tr("Loopback"),
tr("Unable to start the test: %1.")
.arg(tcpServer.errorString()),
QMessageBox::Retry
| QMessageBox::Cancel);
if (ret == QMessageBox::Cancel)
return;
}

serverStatusLabel->setText(tr("Listening"));
clientStatusLabel->setText(tr("Connecting"));
tcpClient.connectToHost(QHostAddress::LocalHost, tcpServer.serverPort());
}

void Dialog::acceptConnection()
{
tcpServerConnection = tcpServer.nextPendingConnection();
if (!tcpServerConnection) {
serverStatusLabel->setText(tr("Error: got invalid pending connection!"));
return;
}

connect(tcpServerConnection, &QIODevice::readyRead,
this, &Dialog::updateServerProgress);
connect(tcpServerConnection,
QOverload<QAbstractSocket::SocketError>::of(&QAbstractSocket::error),
this, &Dialog::displayError);
connect(tcpServerConnection, &QTcpSocket::disconnected,
tcpServerConnection, &QTcpSocket::deleteLater);

serverStatusLabel->setText(tr("Accepted connection"));
tcpServer.close();
}

void Dialog::startTransfer()
{
// called when the TCP client connected to the loopback server
bytesToWrite = TotalBytes - int(tcpClient.write(QByteArray(PayloadSize, '@')));
clientStatusLabel->setText(tr("Connected"));
}

void Dialog::updateServerProgress()
{
bytesReceived += int(tcpServerConnection->bytesAvailable());
tcpServerConnection->readAll();

serverProgressBar->setMaximum(TotalBytes);
serverProgressBar->setValue(bytesReceived);
serverStatusLabel->setText(tr("Received %1MB")
.arg(bytesReceived / (1024 * 1024)));

if (bytesReceived == TotalBytes) {
tcpServerConnection->close();
startButton->setEnabled(true);
#ifndef QT_NO_CURSOR
QApplication::restoreOverrideCursor();
#endif
}
}

void Dialog::updateClientProgress(qint64 numBytes)
{
// called when the TCP client has written some bytes
bytesWritten += int(numBytes);

// only write more if not finished and when the Qt write buffer is below a certain size.
if (bytesToWrite > 0 && tcpClient.bytesToWrite() <= 4 * PayloadSize)
bytesToWrite -= tcpClient.write(QByteArray(qMin(bytesToWrite, PayloadSize), '@'));

clientProgressBar->setMaximum(TotalBytes);
clientProgressBar->setValue(bytesWritten);
clientStatusLabel->setText(tr("Sent %1MB").arg(bytesWritten / (1024 * 1024)));
}

void Dialog::displayError(QAbstractSocket::SocketError socketError)
{
if (socketError == QTcpSocket::RemoteHostClosedError)
return;

QMessageBox::information(this, tr("Network error"),
tr("The following error occurred: %1.")
.arg(tcpClient.errorString()));

tcpClient.close();
tcpServer.close();
clientProgressBar->reset();
serverProgressBar->reset();
clientStatusLabel->setText(tr("Client ready"));
serverStatusLabel->setText(tr("Server ready"));
startButton->setEnabled(true);
#ifndef QT_NO_CURSOR
QApplication::restoreOverrideCursor();
#endif
}

QDialogButtonBox的用法_QT

标签:QDialogButtonBox,tcpClient,mainLayout,tr,用法,connect,Dialog,tcpServerConnection
From: https://blog.51cto.com/u_15515702/5982135

相关文章

  • sqlite 多列多行转置到单行的用法
    1、sqlite:group_concat()多列 参考https://qa.1r1g.com/sf/ask/685814741/SELECTstep,group_concat(concat)ASformatedFROM(SELECTstep,......
  • dataset 用法
       ADO.NetDataAdapter类-ADO.Net教程(yiibai.com) ......
  • curl 命令用法
    curl命令用法目录curl命令用法简介常用命令不带参数:发出GET请求-A参数:用来指定客户端的用户代理标头,即User-Agent。curl的默认用户代理字符串是curl/[version]。-b......
  • window.open用法详解
    window.open用法详解夕山雨于2020-08-0917:44:52发布49906收藏111分类专栏:js基础文章标签:window.open版权华为云开发者联盟该内容已被华为云开发者联......
  • 爬虫之 xPath 用法总结整理
    在网络爬虫中,对于HTML的解析,XPath是一种常用的方法。XPath最初是用于XML中,其主要使用路径表达式在XML文档中进行导航。用于HTML中主要是因为HTML和XML非常的相似:它们具有......
  • SAP实战中Submit的常见用法(方法二挺好用的,标准,自定义报表都可以用)
    场景:系统MB52/MB51/MB5B等类似的报表,虽然数据很全面,执行效率也够快,但是经常会不满足用户需求(增添字段、添加查询条件等),很多ABAP会选择去COPY出标准程序,然后去做修改,强烈不......
  • MySQL的group_concat()函数简单用法
    将groupby产生的同一个分组中的值连接起来,返回一个字符串结果。group_concat函数首先根据groupby指定的列进行分组,将同一组的列显示出来,并且用分隔符分隔。由函数参数(字......
  • python的list的用法
    #ReadMe#本工具是根据用户选择的条目来打印该列表下的内容#例如选择“北京”就会打印北京下面的“海淀”“昌平”“朝阳”,选择“海淀”然后会打印海淀下面的“清华大学”和......
  • lombok之@Accessors用法
    lombok之@Accessors用法lombok的@Accessors注解,打在pojo类上,实现setter方法的链式调用,特别是在初始化对象实例时,可以实现@Builder注解的部分功能,非常方便。注意:有些框架,......
  • Java千问11:instanceof运算符用法详解
    ​Java语言当中提供了一个叫做instanceof的运算符。很多教科书上对这个运算符的介绍并不详细,只是简单的说这个运算符是用来判断某个对象是不是属于某种类型。我们现在就用一......