Returns str as a const char *. This is equivalent to str.toLocal8Bit().constData().
The char pointer will be invalid after the statement in which qPrintable() is used. This is because the array returned by QString::toLocal8Bit() will fall out of scope.
Note: qDebug(), qInfo(), qWarning(), qCritical(), qFatal() expect %s arguments to be UTF-8 encoded, while qPrintable() converts to local 8-bit encoding. Therefore qUtf8Printable() should be used for logging strings instead of qPrintable().
注意:qDebug()、qInfo()、qWarning()、qCritical() 和 qFatal() 要求 %s 参数采用 UTF-8 编码,而 qPrintable() 则转换为本地 8 位编码。因此,应该使用 qUtf8Printable() 来记录字符串,而不是 qPrintable()。
const char *qUtf8Printable(const QString &str)
Returns str as a const char *. This is equivalent to str.toUtf8().constData().
The char pointer will be invalid after the statement in which qUtf8Printable() is used. This is because the array returned by QString::toUtf8() will fall out of scope.
voidMulticaster::readyRead() { while (socket->hasPendingDatagrams()) { QByteArray datagram; datagram.resize(socket->pendingDatagramSize());
QHostAddress senderAddr; quint16 senderPort;
// Here it is assumed that if a datagram is fragmented, it can // be dismissed and treated as a UDP unreliability qint64 r = socket->readDatagram(datagram.data(), datagram.size(), &senderAddr, &senderPort); if (r != datagram.size()) { // Ignore errors and datagrams of incorrect size. qDebug() << "Multicaster::readyRead()" << "readDatagram() returned" << r << ", but" << datagram.size() << "expected. Ignored."; return; }
if (senderPort != settings.port) { // Ignore datagrams sent from unknown ports. qDebug() << "Multicaster::readyRead()" << "Received datagram from port" << senderPort << ", but expected port is" << settings.port << ". Ignored."; return; }
if (senderAddr == ownIp) { // Ignore datagrams sent by this host to itself. return; }
/////////////////////////////////////// PRIVATE FUNCTIONS//////////////////////////////// // // Function to get information about the server and write results to data members voidNotifyClient::getServerInformation() { // return if we don't have valid connection if (! b_validconnection) return; // get the server information QDBusMessage reply = notifyclient->call(QLatin1String("GetServerInformation")); if (reply.type() == QDBusMessage::ReplyMessage) { QList<QVariant> outargs = reply.arguments(); s_name = outargs.at(0).toString(); s_vendor = outargs.at(1).toString(); s_version = outargs.at(2).toString(); s_spec_version = outargs.at(3).toString(); } else { if (reply.type() == QDBusMessage::InvalidMessage) qCritical("CMST - Invalid reply received to GetServerInformation method."); elseif (reply.type() == QDBusMessage::ErrorMessage) #if QT_VERSION >= 0x050400 qCritical("CMST - Error reply received to GetServerInforation method: %s", qUtf8Printable(reply.errorMessage()) ); #else qCritical("CMST - Error reply received to GetServerInforation method: %s", qPrintable(reply.errorMessage()) ); #endif } // else some error occured
boolDbFunc::execQuery(QSqlQuery& query, const QString& sql, const ArgList& args) { // Executes an existing query (in place) with the supplied SQL/args. // THIS IS THE MAIN POINT THROUGH WHICH ALL QUERIES SHOULD BE EXECUTED. query.prepare(sql); addArgs(query, args);
/*! \since 5.9 Returns the path name of a file in the temporary directory. Does \e not check if the file actually exists in the directory. Redundant multiple separators or "." and ".." directories in \a fileName are not removed (see QDir::cleanPath()). Absolute paths are not allowed. */ QString QTemporaryDir::filePath(const QString &fileName)const { if (QDir::isAbsolutePath(fileName)) { qWarning("QTemporaryDir::filePath: Absolute paths are not allowed: %s", qUtf8Printable(fileName)); returnQString(); }
if (!d_ptr->success) returnQString();
QString ret = d_ptr->pathOrError; if (!fileName.isEmpty()) { ret += QLatin1Char('/'); ret += fileName; } return ret; }
// Function to get the capabilities of the server and write to a qstringlist data member voidNotifyClient::getCapabilities() { // return if we don't have valid connection if (! b_validconnection) return; // get the server capabilities QDBusReply<QStringList> reply = notifyclient->call(QLatin1String("GetCapabilities") );
if (reply.isValid()) sl_capabilities = reply.value(); else #if QT_VERSION >= 0x050400 qCritical("CMST - Error reply received to GetCapabilities method: %s", qUtf8Printable(reply.error().message()) ); #else qCritical("CMST - Error reply received to GetCapabilities method: %s", qPrintable(reply.error().message()) ); #endif return; }
// Function to force a close of a notification voidNotifyClient::closeNotification(quint32 id) { // return if we don't have valid connection if (! b_validconnection) return; QDBusMessage reply = notifyclient->call(QLatin1String("CloseNotification"), id); if (reply.type() == QDBusMessage::InvalidMessage) qCritical("CMST - Invalid reply received to CloseNotification method."); elseif (reply.type() == QDBusMessage::ErrorMessage) #if QT_VERSION >= 0x050400 qCritical("CMST - Error reply received to CloseNotification method: %s", qUtf8Printable(reply.errorMessage()) ); #else qCritical("CMST - Error reply received to CloseNotification method: %s", qPrintable(reply.errorMessage()) ); #endif return; }
/*! * \brief InitializeMeters -- Once per program execution actions. * * Opens a connection to the database. * * For each meter in the args list, check to see if there is a table in the * database for the response from the meter. If not, create the table. * * Each meter has its time set to the local standard time. * * \param serialPort Pointer to serial port for communication with meter. * \param args List of meter ids from command line. * \return true if successful, false otherwise. */ boolInitializeMeters(QSerialPort *serialPort, const QStringList &args) { qDebug("Begin"); QSqlDatabase dbConn = QSqlDatabase::database(ConnectionName);
if (!dbConn.isOpen()) { qCritical() << "Unable to open database for meter data."; qInfo() << "Return false"; returnfalse; } QSqlQuery query(dbConn);
/*! Do meter initialization tasks for each meter. */ foreach (QString meterId, args) { //! Expand meter id to all 12 characters. QString fullMeterId = meterId.rightJustified(sizeof(RequestMsgV4.meterId), '0', true);
//! Check for the existence of database tables in which to store meter data.
if (fullMeterId.toLongLong() >= 300000000) { // Is a v.4 meter. //! Set the time in the meter to the computer's idea of the local standard time. if (!SetMeterTime(serialPort, fullMeterId)) { qWarning("Unable to set meter time for meter %s.", qUtf8Printable(fullMeterId)); }
// Create the tables if they don't exist. VerifyDatabaseTable(query, fullMeterId, "_A"); VerifyDatabaseTable(query, fullMeterId, "_B"); } else { // Is a v.3 meter. // Don't try to set meter time for v.3 meter since I don't know how to do it. // Create the table if it doesn't exist. VerifyDatabaseTable(query, fullMeterId, ""); } } }