qstring

QSettings::IniFormat values with “,” returned as QStringList

最后都变了- 提交于 2019-11-28 06:11:03
问题 I am using QSettings to parse an ini file: QSettings cfg(path, QSettings::IniFormat); When I obtain a value QVariant qv = cfg.value("title"); containing a comma the variant contains a QStringList instead of a QString title=foo => QString title=foo,bar => QStringList How can I always get strings, or at least obtain the original line ( title=foo,bar ) ? 回答1: You have at least two ways to address this issue, all of them presented below: test.ini title="foo,bar" title_unquoted=foo,bar main.cpp

QT字符串QString

纵然是瞬间 提交于 2019-11-28 02:43:28
append(),追加,等于+= sprintf() QString str; str.sprintf("%s, %d", "hello ", 3);//str=hello 3 insert(), 插入字符串 prepend(), 在开头插入字符串 replace(), 替换字符串 trimmed(), 移除字符串首尾空白字符,包括换行符,制表符 simplified(), 移除首尾空白字符,将中间的连续空白字符合并成一个空格 startsWith(), 判断一个字符串是否以某个字符串开头 来源: https://www.cnblogs.com/xieqianli/p/11386766.html

五。设计模式(原型模式)

强颜欢笑 提交于 2019-11-27 23:33:25
概念: 用原型实例指定创建对象的种类,并且通过拷贝这些原型创建新的对象。 1.浅复制,引用对象不进行复制。 使用环境: 1.当要实例化的类是在运行时刻指定时,例如:通过动态装载, 2.为了避免创建与产品类层次平行的工厂类层次时。 3.当一个类的实例只能有几个不同状态组合中的一种时。建立相应数目的原型并克隆它们可能比每次用合适的状态手工 实例化该类更方便一些。 原型模式的构成: 1.抽象的原型类(prototype):定义具有克隆自己方法的接口。 2.具体原型类(ConcretePrototype):实现具体的克隆方法。 3.客户类(Client):通过克隆生成一个新的对象。 具体代码如下: 客户端类代码: ****************************************************************** #include <QCoreApplication> #include "concreteprototype.h" #include <QDebug> int main( int argc, char *argv[]) { QCoreApplication a(argc, argv); concreteprototype *type1 = new concreteprototype ( "1" ); concreteprototype

read QProcess output to string

ⅰ亾dé卋堺 提交于 2019-11-27 20:35:48
I have a code that uses QProcess like this. int main(int argc, char *argv[]) { int status=0; QProcess pingProcess; QString ba; QString exec = "snmpget"; QStringList params; params << "-v" << "2c" << "-c" << "public" << "10.18.32.52" << ".1.3.6.1.4.1.30966.1.2.1.1.1.5.10"; status=pingProcess.execute(exec, params); pingProcess.close(); } This outputs the following. SNMPv2-SMI::enterprises.30966.1.2.1.1.1.5.10 = STRING: "0.1" I want to take(read) this output as string. I searched for this and I cant find the solution. Thanks in advance. Shf Did you try QByteArray QProcess::readAllStandardOutput()

How to get QString from QListView selected item in Qt?

偶尔善良 提交于 2019-11-27 14:40:58
问题 I need to get the selected item name in QListView as a QString . I have tried to google, but I haven't found anything useful. 回答1: It depends on selectionMode lets say you have ExtendedSelection which means you can select any number of items (including 0). ui->listView->setSelectionMode(QAbstractItemView::ExtendedSelection); you should iterate through ui->listView->selectionModel()->selectedIndexes() to find indexes of selected items, and then call text() method to get item texts: QStringList

Convert std::string to QString

早过忘川 提交于 2019-11-27 13:50:45
I've got an std::string content that I know contains UTF-8 data. I want to convert it to a QString . How do I do that, avoiding the from-ASCII conversion in Qt? Michael Mrozek There's a QString function called fromUtf8 that takes a const char* : QString str = QString::fromUtf8(content.c_str()); Jackpap QString::fromStdString(content) is better since it is more robust. Also note, that if std::string is encoded in UTF-8, then it should give exactly the same result as QString::fromUtf8(content.data(), int(content.size())) . Usually, the best way of doing the conversion is using the method

How to deal with “%1” in the argument of QString::arg()?

眉间皱痕 提交于 2019-11-27 13:37:56
问题 Everybody loves QString("Put something here %1 and here %2") .arg(replacement1) .arg(replacement2); but things get itchy as soon as you have the faintest chance that replacement1 actually contains %1 or even %2 anywhere. Then, the second QString::arg() will replace only the re-introduced %1 or both %2 occurrences. Anyway, you won't get the literal "%1" that you probably intended. Is there any standard trick to overcome this? If you need an example to play with, take this #include

QT 字体计算

梦想与她 提交于 2019-11-27 13:23:56
CSize fontMetricsCSize(QString source, QPainter *pDC); CSize fontMetricsCSize(QString source, QPainter *pDC) { CSize sz; QFontMetrics fontMetrics(pDC->font()); int nFontWidth = fontMetrics.width(source); int nFontHeight = fontMetrics.height(); sz = CSize(nFontWidth, nFontHeight); return sz; } 来源: https://www.cnblogs.com/ShiShouTHS/p/11365548.html

Qt 操作SQLite数据库

北城以北 提交于 2019-11-27 12:57:10
项目中通常需要采用各种数据库(如 Qracle、SQL Server、MySQL等)来实现对数据的存储、查询等功能。下面讲解如何在 Qt 中操作 SQlite 数据库。 一、SQLite 介绍 Sqlite 数据库作为 Qt 项目开发中经常使用的一个轻量级的数据库,可以说是兼容性相对比较好的数据库之一(Sqlite就像Qt的亲儿子,如同微软兼容Access数据库一样)。Qt5 以上版本可以直接使用(Qt自带驱动),是一个轻量级的数据库,概况起来具有以下优点: SQLite 的设计目的是嵌入式 SQL 数据库引擎,它基于纯C语言代码,已经应用于非常广泛的领域内。 SQLite 在需要长时间存储时可以直接读取硬盘上的数据文件(.db),在无须长时间存储时也可以将整个数据库置于内存中,两者均不需要额外的服务器端进程,即 SQLite 是无须独立运行的数据库引擎。 源代码开源,你可以用于任何用途,包括出售它。 零配置 – 无需安装和管理配置。 不需要配置,不需要安装,也不需要管理员。 同一个数据文件可以在不同机器上使用,可以在不同字节序的机器间自由共享。 支持多种开发语言,C, C++, PHP, Perl, Java, C#,Python, Ruby等。 二、用法 2.1 准备 1、引入SQL模块 在Qt项目文件(.pro文件)中,加入SQL模块: QT += sql 2、引用头文件

How to change string into QString?

耗尽温柔 提交于 2019-11-27 10:34:59
What is the most basic way to do it? If by string you mean std::string you can do it with this method: QString QString::fromStdString(const std::string & str) std::string str = "Hello world"; QString qstr = QString::fromStdString(str); If by string you mean Ascii encoded const char * then you can use this method: QString QString::fromAscii(const char * str, int size = -1) const char* str = "Hello world"; QString qstr = QString::fromAscii(str); If you have const char * encoded with system encoding that can be read with QTextCodec::codecForLocale() then you should use this method: QString