qstring

How to highlight a string of text within a QTextEdit

柔情痞子 提交于 2019-12-01 06:03:13
I'm a student programmer currently developing an application for work using Qt4. I am building an equation editor and I'm having issues attempting to highlight a string within my QTextEdit field. I have a function that parses through the QTextEdit string and returns an a start and end integer of where an error is located. My original strategy was to use HTML tags at these two points to highlight the error. Unfortunately there appears to be an issue with html tagging and the equation syntax. What I think I need is a strategy that relies on Qt's library to set a background color between these

QString与TCHAR/wchar_t/LPWSTR之间的类型转换

不羁的心 提交于 2019-12-01 02:40:30
TCHAR/wchar_t/LPWSTR这三种类型在Unicode字符集中是一样的。 在Qt框架中,经常会使用到windows的函数,而自VC6.0以后,windows默认使用Unicode字符集,windows也相应的推出了TCHAR作为char的宽字符集和多字符集的通用类型来表示char类型。Unicode字符集中,TCHAR代表的是wchar_t,而Qt中,大多数情况下使用QString,这时就需要wchar_t*和QString之间的相互转换。代码如下: 1.TCHAR *类型转为QString类型: 1 QString MainWindow::WcharToChar(const TCHAR *wp, size_t codePage) 2 { 3 QString str; 4 int len = WideCharToMultiByte(codePage, 0, wp, wcslen(wp), NULL, 0, NULL, NULL); 5 char *p = new char[len + 1]; 6 memset(p, 0, len + 1); 7 WideCharToMultiByte(codePage, 0, wp, wcslen(wp), p, len, NULL, NULL); 8 p[len] = '\0'; 9 str = QString(p); 10

Qt中文乱码问题(包含 urlEncode/urlDecode)

一曲冷凌霜 提交于 2019-11-30 18:45:18
1)在主文件中使用本地编码,进行语言国际化的处理,<QTextCodec> QTextCodec::setCodecForLocale(QTextCodec::codecForLocale()); QTextCodec::setCodecForCStrings(QTextCodec::codecForLocale()); QTextCodec::setCodecForTr(QTextCodec::codecForLocale()); 2)利用翻译文件.qm进行语言国际化的处理,首先在pro文件中添加:TRANSLATIONS = 文件名.ts,使用lupdate进行编译,生成ts文件,利用Linguist进行翻译,最后使用lrelease生成.qm文件,再在源文件中添加如下代码,进行引入.qm文件: #include<QTranslator> QTranslator *translator = new QTranslator; translator->load("HUAMOXIAN.qm"); a.installTranslator(translator); 参考文档: Qt之国际化--多语言动态切换 3)QString 转 char* 示例: Qstring str; char* toch; QByteArray byteArray = str.toLatin1(); toch

Should non-QObject derived classes “always” be put on the stack?

回眸只為那壹抹淺笑 提交于 2019-11-30 15:46:23
问题 Coming from the Symbian world, I'm used to using the heap as much as possible to avoid running out of stack space, especially when handling descriptors. CBase derived classes were always dynamically allocated on the heap, since if they were not, their member variables would stay uninitialized. Does the same convention apply to QObject-derived classes? In Qt it seems to be common to put, for example QString, on the stack. Are the string contents put on the heap while QString acts as a

Should non-QObject derived classes “always” be put on the stack?

江枫思渺然 提交于 2019-11-30 14:52:40
Coming from the Symbian world, I'm used to using the heap as much as possible to avoid running out of stack space, especially when handling descriptors. CBase derived classes were always dynamically allocated on the heap, since if they were not, their member variables would stay uninitialized. Does the same convention apply to QObject-derived classes? In Qt it seems to be common to put, for example QString, on the stack. Are the string contents put on the heap while QString acts as a container on the stack, or is everything put on the stack? Frank Osterfeld As sje397 said: It's idiomatic to

Qt 笔记2--Qt 操作 Json

对着背影说爱祢 提交于 2019-11-30 12:39:25
Qt 笔记2--Qt 操作 Json JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式。它基于 ECMAScript(欧洲计算机协会制定的js规范)一个子集,采用完全独立于编程语言的文本格式来存储和表示数据,其简洁和清晰的层次结构使得 JSON 成为理想的数据交换语言。 Qt提供了一系列的Json处理类,包括:QJsonDocument、QJsonObject、QJsonArray、QJsonValue、QJsonParseError,以下为笔者最近学习Qt Json解析功能时写的一些用例,相应学习笔记分享在此处,以便于后续参考。若后续有新的功能或者案例,也会在此处加以补充! 1、功能说明 1) Json转字符串--F1Json2String、F2JsonArray2String 2) 创建Json--M1CreateJson、M1CreateJsonV2 3) 创建Json数组--M2CreateJsonArray 4) 查找Json字段--M3FindFromJson 5) 修改Json字段--M4ModifyJson 6) 删除Json字段--M5DeleteItem 7) 添加Json字段--M6AddItem 8) 保存Json到文档--M7SaveJsonToDoc 9) 加载文档中的Json-

Why is QString printed with quotation marks?

99封情书 提交于 2019-11-30 11:50:12
So when you use qDebug() to print a QString , quotation marks appears suddenly in the output. int main() { QString str = "hello world"; //Classic qDebug() << str; //Output: "hello world" //Expected Ouput: hello world } I know we can solve this with qPrintable(const QString) , but I was just wondering why does QString work like that?, and Is there a method inside QString to change the way it's printed? Why? It's because of the implementation of qDebug() . From the source code : inline QDebug &operator<<(QChar t) { stream->ts << '\'' << t << '\''; return maybeSpace(); } inline QDebug &operator<<

Qt程序开机自动运行

女生的网名这么多〃 提交于 2019-11-30 09:41:47
一、写入注册表需要管理员权限   1.开发中生成并运行程序需要写入注册表时,应该以管理员权限打开项目;   2.点击程序运行需要写入注册表,则应该以管理员权限打开此程序。 二、实现 void MoreSetWindow::sltCheckBoxStartStateChanged(bool checked) // 启动 {    QSettings reg("HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", QSettings::NativeFormat);    if (checked)    {      QString appName = QCoreApplication::applicationName(); // 程序名称      QString appPath = QCoreApplication::applicationFilePath(); // 程序路径      appPath = appPath.replace("/", "\\");      reg.setValue(appName, appPath);    }    else    {      reg.setValue("DeviceClient", "");    } } 来源: https://www

QML与C++数据绑定

我是研究僧i 提交于 2019-11-30 07:31:14
参考资料: Exposing Attributes of C++ Types to QML Overview - QML and C++ Integration Embedding C++ Objects into QML with Context Properties 参照 View-Model 模型,QML作为 View,C++中的对象作为 Model,实现业务逻辑和界面的分离。 暴露单个C++类的属性 通过这种方法,QML中可以直接访问注册到上下文中的C++类实例,并且是注册到QML的全局(具体是注册到一个 QQuickView 或者 engine)。以自定义一个 Name 类,类包括一个 data 属性为例。 类定义 需要暴露给QML访问的类需要有特殊的定义: /*name.h*/ #include <QObject> class Name : public QObject //继承自QObject { Q_OBJECT//QObject宏 Q_PROPERTY(QString data READ data WRITE setData NOTIFY dataChanged) public: Name(QObject *parent = nullptr);//默认构造函数 Name(QString _name);//构造函数 QString data() const;/

qt 中文乱码问题

邮差的信 提交于 2019-11-30 07:16:04
QTextCodec *codec = QTextCodec::codecForName("UTF-8"); QTextCodec::setCodecForTr(codec); QTextCodec::setCodecForLocale(QTextCodec::codecForLocale()); QTextCodec::setCodecForCStrings(QTextCodec::codecForLocale()); 在main函数中加入以上代码。   转自:http://f.dataguru.cn/thread-866992-1-1.html 解决Qt中文乱码以及汉字编码的问题(UTF-8/GBK) 编码知识科普Qt常见的两种编码是:UTF-8和GBK ★UTF-8:Unicode TransformationFormat-8bit,允许含BOM,但通常不含BOM。是用以解决国际上字符的一种多字节编码,它对英文使用8位(即一个字节),中文使用24为(三个字节)来编码。UTF-8包含全世界所有国家需要用到的字符,是国际编码,通用性强。UTF-8编码的文字可以在各国支持UTF8字符集的浏览器上显示。如,如果是UTF8编码,则在外国人的英文IE上也能显示中文,他们无需下载IE的中文语言支持包。 ★GBK是国家标准GB2312基础上扩容后兼容GB2312的标准