我需要获取实际的字符数(而不是字节数),类似于在 V8 中获取字符串长度时的输出。
\n\n这对于 Twitter 的使用是必要的,无论使用哪种语言,Twitter 都会按字符数计算,即使使用 UTF-8(它不按字节长度计算)。
\n\n前任:
\n\n在 chrome/chromium js 控制台,或在 Nodejs 中:
\n\n> "Sch\xc3\xb6ne Gr\xc3\xbc\xc3\x9fe".length\n< 12\nRun Code Online (Sandbox Code Playgroud)\n\n在 Qt 4.8.2 中,尝试QString someStr = "Sch\xc3\xb6ne Gr\xc3\xbc\xc3\x9fe"; cout << someStr.length()将输出 15,这不是我的目标。
我在Qt编程,但我更习惯于PHP.
因此,考虑到这一点,我如何"回显"或"打印"QStringList或QString的内容以确保内容符合预期?
我正在构建一个GUI应用程序.无论如何打印内容?
显然在PHP中,你可以在一个数组上使用print_r,QStringList有什么类似的东西吗?并且回显一个变量,类似于QString的任何东西?
如果需要,我可以提供代码.
谢谢.
我想检查 aQString是否仅由不可打印或不可见的字符组成。该QString可包含Unicode ...
我想正则表达式可能有用,但我不知道如何创建这样的正则表达式。
如何检查 a 是否QString仅包含“不可见”字符?(空格, \n, \r, \t...)
我的“蛮力”尝试
bool checkIfEmpty(const QString &contents) const
{
for(QString::const_iterator itr(contents.begin()); itr != contents.end(); ++itr)
{
if(*itr != '\n' && *itr != '\r' && *itr != ' ' && *itr != '\t')
return false;
}
return true;
}
Run Code Online (Sandbox Code Playgroud) 我有这个带有PyQt5的Python 3.5.1程序,以及一个从QtCreator ui文件创建的GUI,其中pyqtSlot装饰器导致“ TypeError:在textChanged(QString)和edited()之间发生connect()失败”。
在重现该问题的示例代码中,我有2个自定义类:MainApp和LineEditHandler。MainApp实例化主GUI(来自文件“ mainwindow.ui”),并且LineEditHandler处理QLineEdit对象。LineEditHandler存在的原因是将与QLineEdit对象主要相关的自定义方法集中在一个类中。它的构造函数需要QLineEdit对象和MainApp实例(在需要时访问其他对象/属性)。
在MainApp中,我将QLineEdit的textChanged信号连接到LineEditHandler.edited()。如果我不使用pyqtSlot()装饰LineEditHandler.edited(),则一切正常。如果我确实对方法使用@pyqtSlot(),则代码运行将失败,并显示“ TypeError:textChanged(QString)和edited()之间的connect()失败”。我在这里做错什么了吗?
您可以在以下位置获取mainwindow.ui文件:https ://drive.google.com/file/d/0B70NMOBg3HZtUktqYVduVEJBN2M/view
这是产生问题的示例代码:
import sys
from PyQt5 import uic
from PyQt5.QtWidgets import *
from PyQt5.QtCore import pyqtSlot
Ui_MainWindow, QtBaseClass = uic.loadUiType("mainwindow.ui")
class MainApp(QMainWindow, Ui_MainWindow):
def __init__(self):
# noinspection PyArgumentList
QMainWindow.__init__(self)
Ui_MainWindow.__init__(self)
self.setupUi(self)
# Instantiate the QLineEdit handler.
self._line_edit_handler = LineEditHandler(self, self.lineEdit)
# Let the QLineEdit handler deal with the QLineEdit textChanged signal.
self.lineEdit.textChanged.connect(self._line_edit_handler.edited)
class LineEditHandler:
def __init__(self, main_window, line_edit_obj):
self._line_edit = line_edit_obj
self._main_window = main_window
# FIXME The pyqtSlot decorator causes …Run Code Online (Sandbox Code Playgroud) 我需要找到一个干净,快捷的方式写QString在.csv文件中。我试过:
QString path= QCoreApplication::applicationDirPath() + QString("/myfile.csv");
QFile file(path);
QString mystring = "Hello, world!";
if(!file.open(QIODevice::WriteOnly)){
file.close();
} else {
QTextStream out(&file); out << mystring;
file.close();
}
Run Code Online (Sandbox Code Playgroud)
但它经常为我"???????"在myfile.csv
我有一个字符串。例如:
QString myString = "Today is Tuesday";
Run Code Online (Sandbox Code Playgroud)
要求是:当用户键入一个字符串时,如果该字符串包含在 中myString,则该部分myString应为粗体,不区分大小写(Qt::CaseInsensitive),但myString应保留的格式(大写字符应为大写和小写字符)应该是小写)。
例如:
tu- >今天是屠esdayES- >今天是屠ES天aY- >托德AY就是Tuesd AY这是我的功能:
void myClass::setBoldForMatching( const QString &p_text )
{
QRegExp regExp( p_text, Qt::CaseInsensitive, QRegExp::RegExp );
if ( !p_text.isEmpty() )
{
if ( myString.contains( regExp ) )
{
myString = myString.replace( p_text, QString( "<b>" + p_text + "</b>" ), Qt::CaseInsensitive );
}
}
}
Run Code Online (Sandbox Code Playgroud)
这个函数是错误的,因为 …
我正在尝试使用该QString::toDouble()功能验证用户输入.该文件说,函数应该这样使用:
double QString::toDouble ( bool * ok = 0 ) const;
/*
Returns the string converted to a double value.
Returns 0.0 if the conversion fails.
If a conversion error occurs, *ok is set to false; otherwise *ok is set to true.
*/
Run Code Online (Sandbox Code Playgroud)
所以我试图使用它*ok来抛出错误消息,如果它的错误,目的是只允许用户输入有效的整数或小数.问题是即使输入单词,消息也始终返回有效.到目前为止,这是我的代码:
void MainWindow::checkData()
{
bool validate;
QString tempStr;
tempStr = ui->lineEditValidate->text();
double converted = tempStr.toDouble(&validate);
if (validate = false)
{
QErrorMessage validateError;
validateError.showMessage("Input is Invalid");
validateError.exec();
}
else
{
QErrorMessage worksFine; …Run Code Online (Sandbox Code Playgroud) 我正在和一个现有项目的Qt合作.我正在尝试通过串行电缆将字符串发送到恒温器以向其发送命令.我需要确保字符串只包含0-9,af,并且长度不超过或少于6个字符.我试图使用QString.contains,但我现在卡住了.任何帮助,将不胜感激.
我知道有很多的信息有关转换QString到char*,但我还是需要一些澄清这个问题.
Qt提供QTextCodecs转换QString(内部存储unicode中的字符)QByteArray,允许我检索char*哪些代表一些非unicode编码中的字符串.但是当我想要一个unicode时我该怎么办QByteArray?
QTextCodec* codec = QTextCodec::codecForName("UTF-8");
QString qstr = codec->toUnicode("??????");
std::string stdstr(reinterpret_cast<const char*>(qstr.constData()), qstr.size() * 2 ); // * 2 since unicode character is twice longer than char
qDebug() << QString(reinterpret_cast<const QChar*>(stdstr.c_str()), stdstr.size() / 2); // same
Run Code Online (Sandbox Code Playgroud)
上面的代码按照我的预期打印"Юникод".但是,我想知道这是否是去unicode的正道char*的QString.特别是,reinterpret_cast这种技术中的s和大小算术看起来非常难看.
我正在尝试在文本文件中搜索字符串; 我的目的是只有在我的文本文件中没有写入它时才写它.
这是我的函数(我不知道如何放入while循环):
QFile MyFile("text.txt");
MyFile.open(QIODevice::ReadWrite);
QTextStream in (&MyFile);
while(!MyFile.atEnd())
{ //do something to search string inside }
MyFile.close();
Run Code Online (Sandbox Code Playgroud)
我怎样才能做到这一点?从Qt的帮助,方法"包含"仅适用于const变量; 我可以用它来寻找我的字符串吗?