Qt QString 类型判断为空错误解决方案
void Procession::displayMessage(const QString& information) { qDebug() << information;
// 在TextEdit中显示消息
if (!information.isEmpty()) {
ui->message->append(information);
}
}
G:\Qt\Client\Smarthome_Client\procession.cpp:63: error: could not convert 'information' from 'const QString' to 'bool' if (information) { ^
这个错误是因为在条件语句中,需要一个布尔值作为条件,而你传递了一个QString类型的变量。要解决这个问题,你可以使用QString的isEmpty()方法来检查字符串是否为空。修改代码如下:
void Procession::displayMessage(const QString& information)
{
qDebug() << information;
// 在TextEdit中显示消息
if (!information.isEmpty()) {
ui->message->append(information);
}
}
这样修改后,如果字符串不为空,就会将其追加到TextEdit中显示。
原文地址: http://www.cveoy.top/t/topic/fB9z 著作权归作者所有。请勿转载和采集!