int QString::toInt(bool *ok = Q_NULLPTR, int base = 10) const

本文详细介绍了 Qt 中 QString 类的 toInt 方法。该方法能够将字符串转换为整数,并允许指定不同的进制,默认为十进制。文章还提供了示例代码,展示了如何使用此方法及其返回值和错误处理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

以下为官方文档:

int QString::toInt(bool *ok = Q_NULLPTR, int base = 10) const

Returns the string converted to an int using base base, which is 10 by default and must be between 2 and 36, or 0. Returns 0 if the conversion fails.

If a conversion error occurs, *ok is set to false; otherwise *ok is set to true.

If base is 0, the C language convention is used: If the string begins with "0x", base 16 is used; if the string begins with "0", base 8 is used; otherwise, base 10 is used.

The string conversion will always happen in the 'C' locale. For locale dependent conversion use QLocale::toInt()

Example:

 
  QString str = "FF";
  bool ok;
  int hex = str.toInt(&ok, 16);       // hex == 255, ok == true
  int dec = str.toInt(&ok, 10);       // dec == 0, ok == false
 我认为转换之前会进行类型检测,如果无法转换,OK变为0,返回值为0,同时ok一直为0,我们也可以在后续去判断 

反之为1,转换成功

#include <QWidget> #include "ui_SerialUI.h" #include "Serial.h" /** * @brief 串口通信的界面管理类 * 负责管理串口配置界面、用户交互及与底层串口操作的桥接 */ class SerialUI : public QWidget { Q_OBJECT public: /* * @brief 构造函数 * @param parent 父窗口指针 */ SerialUI(QWidget *parent = nullptr); /* * @brief 初始化串口控件参数 */ void init_port_control(); /* * @brief 获取当前选中的串口名称 * @return 当前选择的端口名 */ QString get_sel_port() const; private slots: /* * @brief 处理打开/关闭串口按钮点击事件 根据当前状态切换串口连接,更新界面显示 */ void on_open_clicked(); /* * @brief 处理发送按钮点击事件,将输入框内容发送至已打开的串口 */ void on_send_clicked(); /* * @brief 接收并显示来自串口的数据 * @param data 接收到的字符串数据 */ void on_data_rec(const QString& data); private: Ui::SerialUI ui;//界面组件对象 Serial *serial_handler =nullptr; // 串口操作处理器 };#include <QObject> #include <QSerialPort> struct SerialParamInfo { QString port_name;//串口名 int baud_rate = 115200;//端口号 QSerialPort::DataBits data_bits = QSerialPort::Data8; // 数据位 QSerialPort::Parity parity_bits = QSerialPort::NoParity; // 校验位 QSerialPort::StopBits stop_bits = QSerialPort::OneStop; // 停止位 QSerialPort::FlowControl flow_control = QSerialPort::NoFlowControl; // 流控制 }; // 串口通信核心类 class Serial : public QObject { Q_OBJECT public: explicit Serial(QObject* parent = nullptr);// 构造函数 ~Serial(); // 析构函数(自动关闭串口并清理资源) public: /* * @brief 打开串口 * @parm 配置参数 * @return 返回布尔值避免重复打开已连接的串口 */ bool open(const SerialParamInfo& config); /* * @brief 主动关闭串口连接 */ void close_port(); /* * @brief 发送字符串数据(UTF-8编码) * @parm 要发送的数据 */ void write_data(const QString& data); /* * @brief 调用QSerialPort的isOpen */ bool is_open() const; signals: /*sigRecvData * @brief 数据接收信号 当串口收到数据时,通过此信号通知上层 * @param data 接收到的字符串数据 */ void sig_rec_data(const QString& data); private slots: /* * @brief 串口数据到达处理槽 *当QSerialPort触发readyRead信号时自动调用 *负责读取缓冲区数据并转发给上层 */ void handle_ready_read(); private: QSerialPort ser_port;// Qt串口对象实例 };针对以上实现的.h文件,让串口每次保存上次加载的数据
03-29
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

胖头鱼爱算法

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值