QStringLiteral 的作用和使用场景:

QStringLiteral是Qt中的宏,用于创建高效、安全的字符串字面量。它在编译时处理,避免运行时拷贝,提升性能,且利于编译期检查,增强安全性。常见用途包括构造QString、常量表达式和信号槽机制。使用QStringLiteral能优化程序并减小内存消耗。

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

通过 chatgpt 了解如下:

QStringLiteral 使用场景

`QStringLiteral 是 Qt 框架中的一个宏,用于创建 QString 类型的字符串字面量。它在编译时执行,并提供了一种更高效的方式来创建字符串。在使用QString字面量时,需要遵循以下规则:

  1. 在字符串前加上QLatin1StringQStringLiteral前缀(两者等价)。

  2. 字符串内容不需要双引号包围,例如:QLatin1String("Hello")QStringLiteral("World")

  3. 字符串内容中不能包含\0字符,否则其行为是未定义的。

以下是QString字面量的一些使用场景:

  1. 用于构造QString,以便在代码中使用和处理字符串。例如:
QString text = QStringLiteral("Hello World!");
  1. 用于构造QString类型的常量表达式。在程序中使用常量表达式可以带来更快的启动时间和更好的优化。例如:
static constexpr QLatin1String kCompanyName("MyCompany");
const QLatin1String kVersion("1.0");
  1. 用于在Qt信号槽机制中传递字符串参数,可以提高运行效率并减少内存消耗。例如:
connect(button, &QPushButton::clicked, this, &MyClass::handleButtonClicked);
...
void MyClass::handleButtonClicked()
{
    QString text = QStringLiteral("Button clicked!");
    qDebug() << text;
}

总之,使用QStringLiteral可以在编译时进行字符串处理,优化程序性能和安全性,并且方便在Qt代码中使用和处理字符串

QStringLiteral 的作用和意义:

`QStringLiteral 是 Qt 框架中的一个宏,用于创建 QString 类型的字符串字面量。其主要作用是为了提高字符串在编译期的安全性和性能。

在C++中,字符串字面量是以字符数组的方式存在,当程序构造QString类型对象时,需要对这个字面量进行拷贝操作,这会使得在运行时会出现一定的性能损失和一些安全隐患。

而如果使用QStringLiteral来创建字符串字面量,编译器在编译期生成的代码可以将这个字符串直接存储在一个只读区域,并不需要额外的拷贝操作,从而提高了字符串的性能。此外,因为这个字符串是只读的,所以它在编译期即可被编码器检查,避免了一些潜在的安全问题。同时,在某些情况下,它还可以降低二进制文件的大小。

例如,以下是一个使用了QStringLiteral的例子:

QString str = QStringLiteral("Hello World");

使用QStringLiteral可以让编译器优化代码,应用程序运行时就不需要再对字符串进行拷贝和处理了。

总之,QStringLiteral提高了字符串字面量的效率和安全性,并且方便在Qt代码中使用和处理字符串。

class TaskQueueManager : public QObject//负责管理队列的出队进队,管理插入、管理米数、管理换卷的时候需要做的额外的判断 { public: TaskQueueManager(QObject* parent = nullptr); ~TaskQueueManager(); void InitTaskQueueManager(); void EnqueueInsert(const DefectInfoItem& data); void EnqueueUpdateReel(const QSharedPointer<TaskStruct::UpDataByReelTask> task); void EnqueueUpdateMeter(const QSharedPointer<TaskStruct::UpDataByMeterTask> task); QSharedPointer<TaskStruct::DatabaseTaskBase> DequeueTask(); void StartTimers(); void StopTimers(); private slots: void OnInsertTimerTimeout(); void OnMeterTimerTimeout(); private: void FlushInsertQueue(); QString GeneratorTableName(DefectInfoItem& data); private: QQueue<QSharedPointer<TaskStruct::DatabaseTaskBase>> reelQueue; QMutex reelQueueMutex; QQueue<QSharedPointer<TaskStruct::DatabaseTaskBase>> otherQueue; QMutex otherQueueMutex; QQueue<DefectInfoItem>insertQueue;//插入队列 QMutex insertQueueMutex;//插入队列锁 //米数更新用的成员变量 CurrentRunData currentMeterData; QMutex meterDataMutex; bool meterDataDirty = false; QTimer* insertTimer;//插入定时器 QTimer* meterTimer;//米数更新定时器(米数不用接收到就马上执行,而是要等上50毫秒再执行) protected: Q_OBJECT }; TaskQueueManager::TaskQueueManager(QObject* parent) : QObject(parent) { } TaskQueueManager::~TaskQueueManager() { this->StopTimers(); { QMutexLocker locker(&this->insertQueueMutex); if (!this->insertQueue.isEmpty()) { FlushInsertQueue(); } } delete this->insertTimer; delete this->meterTimer; } void TaskQueueManager::InitTaskQueueManager() { qDebug() << QStringLiteral("TaskQueueManager线程:") << QThread::currentThreadId(); this->insertTimer = new QTimer(); this->insertTimer->setInterval(500); this->meterTimer = new QTimer(); this->meterTimer->setInterval(50); connect(this->insertTimer, &QTimer::timeout, this, &TaskQueueManager::OnInsertTimerTimeout); connect(this->meterTimer, &QTimer::timeout, this, &TaskQueueManager::OnMeterTimerTimeout); } void TaskQueueManager::EnqueueInsert(const DefectInfoItem& data) { qDebug() << QStringLiteral("EnqueueInsert线程:") << QThread::currentThreadId(); QMutexLocker locker(&this->insertQueueMutex); this->insertQueue.enqueue(data); qDebug() << this->insertQueue.size(); } void TaskQueueManager::EnqueueUpdateReel(const QSharedPointer<TaskStruct::UpDataByReelTask> task) { QMutexLocker locker(&this->reelQueueMutex); this->reelQueue.enqueue(task); } void TaskQueueManager::EnqueueUpdateMeter(const QSharedPointer<TaskStruct::UpDataByMeterTask> task) { QMutexLocker locker(&this->meterDataMutex); this->currentMeterData = task->currentRunData; this->meterDataDirty = true; } QSharedPointer<TaskStruct::DatabaseTaskBase> TaskQueueManager::DequeueTask() { qDebug() << "reelQueue" << this->reelQueue.size(); qDebug() << "otherQueue" << this->otherQueue.size(); qDebug() << QStringLiteral("DequeueTask线程:") << QThread::currentThreadId(); { QMutexLocker locker(&this->reelQueueMutex); if (!this->reelQueue.isEmpty()) { return this->reelQueue.dequeue(); } } { QMutexLocker locker(&this->otherQueueMutex); if (!this->otherQueue.isEmpty()) { return this->otherQueue.dequeue(); } } return nullptr; } void TaskQueueManager::StartTimers() { qDebug() << QStringLiteral("StartTimers线程:") << QThread::currentThreadId(); this->meterTimer->start(); this->insertTimer->start(); } void TaskQueueManager::StopTimers() { qDebug() << QStringLiteral("StopTimers线程:") << QThread::currentThreadId(); if (this->meterTimer && this->meterTimer->isActive()) { this->meterTimer->stop(); } if (this->insertTimer && this->insertTimer->isActive()) // 修复重复判断 { this->insertTimer->stop(); } } void TaskQueueManager::OnInsertTimerTimeout() { qDebug() << QStringLiteral("OnInsertTimerTimeout线程:") << QThread::currentThreadId(); QMutexLocker locker(&this->insertQueueMutex); if (!insertQueue.isEmpty()) { FlushInsertQueue(); } } void TaskQueueManager::FlushInsertQueue() { qDebug() << QStringLiteral("FlushInsertQueue线程:") << QThread::currentThreadId(); if (this->insertQueue.isEmpty()) { return; } QHash<QString, QQueue<DefectInfoItem>> l_groupedTasks; while (!this->insertQueue.isEmpty()) { auto l_defectInfoItem = this->insertQueue.dequeue(); QString l_tableName = this->GeneratorTableName(l_defectInfoItem); l_groupedTasks[l_tableName].enqueue(l_defectInfoItem); } for (auto it = l_groupedTasks.begin(); it != l_groupedTasks.end(); ++it) { auto l_batchTask = QSharedPointer<TaskStruct::BatchInsertTask>::create(); l_batchTask->defectItems = it.value(); l_batchTask->tableName = it.key(); { QMutexLocker locker(&this->otherQueueMutex); this->otherQueue.enqueue(l_batchTask); } } } QString TaskQueueManager::GeneratorTableName(DefectInfoItem& data) { QString l_timePart = data.startTime; QString l_tableName = "test_defect_table";//QString("reel_%1").arg(l_timePart.replace("-", "").replace(":", "").replace(" ", "").replace(".", "")); return l_tableName; } void TaskQueueManager::OnMeterTimerTimeout() { QSharedPointer<TaskStruct::UpDataByMeterTask> task; { QMutexLocker locker(&meterDataMutex); if (!this->meterDataDirty) { return; } task = QSharedPointer<TaskStruct::UpDataByMeterTask>::create(); task->currentRunData = currentMeterData; this->meterDataDirty = false; } QMutexLocker locker(&otherQueueMutex); this->otherQueue.enqueue(task); } class TaskWorker : public QObject { public: explicit TaskWorker(QObject* parent = nullptr); ~TaskWorker(); void InitTaskWorker(); bool DoInsertNgDataList(const QQueue<DefectInfoItem>& dataList, const QString& tableName); bool DoUpdateReel(CurrentRunData& data); bool DoUpdateMeter(CurrentRunData& data); bool IsDatabaseOpen(); bool OpenDataBaseConnect(); bool CloseConnection(); private: bool ExecuteCommand(QSqlQuery& query); QSqlDatabase taskDB; protected: Q_OBJECT }; TaskWorker::TaskWorker(QObject* parent) : QObject(parent) { } TaskWorker::~TaskWorker() { this->CloseConnection(); } void TaskWorker::InitTaskWorker() { qDebug() << QStringLiteral("InitTaskWorker线程:") << QThread::currentThreadId(); if (!OpenDataBaseConnect()) { qDebug() << QStringLiteral("数据库连接失败!"); } } bool TaskWorker::OpenDataBaseConnect() { ConfigManager* l_configInstance = ConfigManager::GetConfigInstance(); ConfigManager::DAOConfig* l_daoConfig = l_configInstance->GetConfig(); QString l_driverName; switch (l_daoConfig->type) { case ConfigManager::DataBaseType::SQLite: l_driverName = l_driverName = "QSQLITE"; break; case ConfigManager::DataBaseType::MySQL: l_driverName = l_driverName = "QMYSQL"; break; case ConfigManager::DataBaseType::PostgreSQL: l_driverName = l_driverName = "QPSQL"; break; case ConfigManager::DataBaseType::Oracle: l_driverName = l_driverName = "QOCI"; break; default: l_driverName = "QMYSQL"; } if (QSqlDatabase::contains("TaskConnection")) { this->taskDB = QSqlDatabase::database("TaskConnection"); if (this->taskDB.isOpen()) { this->taskDB.close(); } } else { this->taskDB = QSqlDatabase::addDatabase(l_driverName, "TaskConnection"); } if (l_daoConfig->type != ConfigManager::DataBaseType::SQLite) { this->taskDB.setHostName(l_daoConfig->host); this->taskDB.setPort(l_daoConfig->port); } taskDB.setDatabaseName(l_daoConfig->dbName); taskDB.setUserName(l_daoConfig->account); taskDB.setPassword(l_daoConfig->password); if (!taskDB.open()) { QSqlError l_error = taskDB.lastError(); //qDebug() << QStringLiteral("驱动错误:") << l_error.driverText(); //qDebug() << QStringLiteral("错误描述:") << l_error.text(); return false; } else { //qDebug() << QStringLiteral("数据库连接创建成功,线程ID:") << QThread::currentThreadId(); //qDebug() << QStringLiteral("子线程 数据库连接成功"); return true; } qDebug() << QStringLiteral("OpenDataBaseConnect线程:") << QThread::currentThreadId(); } class XZJDATABASE_EXPORT TaskProcessor : public QObject { public: TaskProcessor(QObject* parent = nullptr); ~TaskProcessor(); void InitTaskProcessor(); QSharedPointer<TaskQueueManager> GetTaskQueueManager(); public slots: void StartProcessing(); void StopProcessing(); public slots: void ProcessTask(); private: QSharedPointer<TaskQueueManager> queueManager; // 队列管理器 QSharedPointer<TaskWorker> worker; // 数据库操作实例 QMutex waitMutex; // 等待锁 QWaitCondition waitCondition; // 线程等待条件 std::atomic<bool> isRunning{ false }; // 运行状态 protected: Q_OBJECT }; TaskProcessor::TaskProcessor(QObject* parent) : QObject(parent) { } TaskProcessor::~TaskProcessor() { this->StopProcessing(); } void TaskProcessor::InitTaskProcessor() { qDebug() << QStringLiteral("InitTaskProcessor线程:") << QThread::currentThreadId(); this->queueManager = QSharedPointer<TaskQueueManager>::create(this); this->worker = QSharedPointer<TaskWorker>::create(this); this->queueManager->InitTaskQueueManager(); this->worker->InitTaskWorker(); } QSharedPointer<TaskQueueManager> TaskProcessor::GetTaskQueueManager() { return this->queueManager; } void TaskProcessor::StartProcessing() { this->isRunning = true; this->queueManager->StartTimers(); this->ProcessTask(); } void TaskProcessor::StopProcessing() { this->isRunning = false; this->waitCondition.wakeAll(); this->queueManager->StopTimers(); } void TaskProcessor::ProcessTask() { qDebug() << QStringLiteral("ProcessTask线程:") << QThread::currentThreadId(); while (this->isRunning) { qDebug() << QStringLiteral("ProcessTask 循环执行"); auto task = queueManager->DequeueTask(); if (!task) { qDebug() << "null data"; QMutexLocker locker(&waitMutex); waitCondition.wait(&waitMutex, 50); } else { switch (task->type) { case TaskStruct::TaskType::BatchInsert: { qDebug() << QStringLiteral("TaskStruct::TaskType::BatchInsert 批量插入"); auto bacthInsertTask = task.staticCast<TaskStruct::BatchInsertTask>(); bool success = this->worker->DoInsertNgDataList(bacthInsertTask->defectItems, bacthInsertTask->tableName); if (success) { LOG_INFO("Batch insert completed successfully."); qDebug() << "Batch insert completed successfully."; } else { LOG_ERROR("Batch insert failed."); qDebug() << "Batch insert failed."; } break; } case TaskStruct::TaskType::UpdateReel: { auto reelTask = task.staticCast<TaskStruct::UpDataByReelTask>(); bool success = this->worker->DoUpdateReel(reelTask->currentRunData); if (success) { LOG_INFO("Reel update completed."); } else { LOG_ERROR("Reel update failed."); } break; } case TaskStruct::TaskType::UpdateMeter: { auto meterTask = task.staticCast<TaskStruct::UpDataByMeterTask>(); bool success = this->worker->DoUpdateMeter(meterTask->currentRunData); if (success) { LOG_INFO("Meter update completed."); } else { LOG_ERROR("Meter update failed."); } break; } } } } } //可以选择缓存然后等初始化完成后通过OnThread中进行数据的再次添加 class XZJDATABASE_EXPORT TaskAccessService : public QObject { public: explicit TaskAccessService(QObject* parent = nullptr); ~TaskAccessService() override; void InitTaskAccessService(); void InsertNgData(const DefectInfoItem& data); void UpDateByChangeReel(const CurrentRunData& data); void UpdateByChangeMeter(const CurrentRunData& data); private: QSharedPointer<TaskProcessor> processor; QThread* workerThread; int testcount = 0; protected: Q_OBJECT }; TaskAccessService::TaskAccessService(QObject* parent) : QObject(parent) { } TaskAccessService::~TaskAccessService() { if (this->workerThread && this->workerThread->isRunning()) { this->processor->StopProcessing(); this->workerThread->quit(); this->workerThread->wait(1000); this->workerThread->terminate(); } delete this->workerThread; } void TaskAccessService::InitTaskAccessService() { qDebug() << QStringLiteral("InitTaskAccessService线程:") << QThread::currentThreadId(); this->workerThread = new QThread(); this->processor = QSharedPointer<TaskProcessor>::create(); this->processor->moveToThread(this->workerThread); this->processor->InitTaskProcessor(); connect(workerThread, &QThread::started, processor.data(), &TaskProcessor::StartProcessing); this->workerThread->start(); } void TaskAccessService::InsertNgData(const DefectInfoItem& data) { this->testcount++; this->processor->GetTaskQueueManager()->EnqueueInsert(data); qDebug() << this->testcount; } void TaskAccessService::UpDateByChangeReel(const CurrentRunData& data) { auto task = QSharedPointer<TaskStruct::UpDataByReelTask>::create(); task->currentRunData = data; this->processor->GetTaskQueueManager()->EnqueueUpdateReel(task); } void TaskAccessService::UpdateByChangeMeter(const CurrentRunData& data) { auto task = QSharedPointer<TaskStruct::UpDataByMeterTask>::create(); task->currentRunData = data; this->processor->GetTaskQueueManager()->EnqueueUpdateMeter(task); } void XZJDataBaseModel::Init() { QString configPath = PathManager::GetPathInstance()->GetConfigFilePath(); ConfigManager* l_configInstance = ConfigManager::GetConfigInstance(); l_configInstance->LoadJsonConfig(configPath); ConfigManager::DAOConfig* l_daoConfig = l_configInstance->GetConfig(); PrintRollProductionDAO* l_daoInstance = PrintRollProductionDAO::GetDAOInstance(); l_daoInstance->OpenDataBaseConnect(l_daoConfig->type, l_daoConfig->host, l_daoConfig->port, l_daoConfig->dbName, l_daoConfig->account, l_daoConfig->password); this->taskAccessService = QSharedPointer<TaskAccessService>::create(); this->taskAccessService->InitTaskAccessService(); //this->TestQuery(); this->TestInsert(); } 这些是我优化后的代码,请帮我分析下是否还有问题,以及insert流程能否走通,以及关于Processor中的ProcessTask函数是否有其他方法实现在不用定时器的情况下,以及Service中需要考虑子线程主线程之间的顺序关系,比如子线程还未初始化,主线程就通过service进行调用接口,并且你不要通过一些比如在未初始化完全前就禁止调用接口的方式来解决这个问题。我的主线程目前就一个创建service对象、初始化service、然后开始调用对象中的函数,其他的你看下分别有哪些问题以及有哪些方法解决?
最新发布
07-24
QtTreePropertyBrowser *propertyBrowser = new QtTreePropertyBrowser(); propDocker->setWidget(propertyBrowser); addDockWidget(Qt::RightDockWidgetArea, propDocker); // 创建属性管理器 QtVariantPropertyManager *variantManager = new QtVariantPropertyManager(this); QtVariantEditorFactory *variantFactory = new QtVariantEditorFactory(this); propertyBrowser->setFactoryForManager(variantManager, variantFactory); // 添加示例属性 QtProperty *groupItem = variantManager->addProperty(QtVariantPropertyManager::groupTypeId(),QStringLiteral("Group1")); QtVariantProperty *item = variantManager->addProperty(QVariant::Int, QStringLiteral("组1整型变量: ")); item->setValue(100); groupItem->addSubProperty(item); item = variantManager->addProperty(QVariant::Bool, QStringLiteral("组1布尔类型变量 ")); item->setValue(true); groupItem->addSubProperty(item); item = variantManager->addProperty(QVariant::Double, QStringLiteral("组1双精度浮点型类型变量: ")); item->setValue(3.1415); groupItem->addSubProperty(item); item = variantManager->addProperty(QVariant::String, QStringLiteral("组1字符串型类型变量: ")); item->setValue(QStringLiteral("hello world")); groupItem->addSubProperty(item); QtProperty *groupItem1 = variantManager->addProperty(QtVariantPropertyManager::groupTypeId(),QStringLiteral("Group2")); QtVariantProperty *item1 = variantManager->addProperty(QVariant::Int, QStringLiteral("组2整型变量: ")); item1->setValue(100); groupItem1->addSubProperty(item); item1 = variantManager->addProperty(QVariant::String, QStringLiteral("组2字符串型类型变量: ")); item1->setValue(QStringLiteral("hello world")); groupItem1->addSubProperty(item1); item1 = variantManager->addProperty(QVariant::Bool, QStringLiteral("组2颜色类型变量 ")); item1->setValue(true); groupItem1->addSubProperty(item1); item1 = variantManager->addProperty(QVariant::Double, QStringLiteral("Enum Property: ")); item->setValue(Eum1); groupItem->addSubProperty(item); propertyBrowser->addProperty(groupItem); propertyBrowser->addProperty(groupItem1); 在上述代码的颜色部分作出一个颜色选择并在其下有一个子树,其中有rgb三个选项也可选择,无论谁选择都会影响对方
07-11
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值