[software development] 求教:first()和last()两个函数的问题 Resolved
Tofloor
poster avatar
嵩云闲人
deepin
2023-10-08 22:58
Author

deepin20.9、QT5.15,使用first()、last()报错:

/home/hrhh-zengfc/qttest/sampl04_02_01/widget.cpp👎 In member function ‘void Widget::on_pushButton_23_clicked()’:

/home/hrhh-zengfc/qttest/sampl04_02_01/widget.cpp:62: error: ‘class QString’ has no member named ‘first’
QString str3 = str1.first(N + 1);
^~~~~

/home/hrhh-zengfc/qttest/sampl04_02_01/widget.cpp:64: error: ‘class QString’ has no member named ‘last’; did you mean ‘at’?
str3 = str1.last(str1.size() - N - 1);
^~~~

问题出在哪儿?

烦请大神指教。

Reply Favorite View the author
All Replies
hotime
deepin
2023-10-08 23:42
#1

就是字面意思呀: ‘class QString’ has no member named ‘first’

QString类没有名为first的成员函数,所以你不能这么使用。

QVector、QList和QMap才有。

你如果是要从QString类型的字符串中获得某一位字符,应该使用at

如:

QString str = "Hello, world!";
// 获取第一个字符
QChar firstChar = str.at(0);

(信息来源参考自百度AI)

Reply View the author
嵩云闲人
deepin
2023-10-09 01:02
#2
hotime

就是字面意思呀: ‘class QString’ has no member named ‘first’

QString类没有名为first的成员函数,所以你不能这么使用。

QVector、QList和QMap才有。

你如果是要从QString类型的字符串中获得某一位字符,应该使用at

如:

QString str = "Hello, world!";
// 获取第一个字符
QChar firstChar = str.at(0);

(信息来源参考自百度AI)

这是看到的一个例程,运行环境windows10、QT6.24,是可以运行的。想在linux环境测试下,结果出错了。所有希望弄清楚是什么原因。

Reply View the author
hotime
deepin
2023-10-09 01:20
#3
嵩云闲人

这是看到的一个例程,运行环境windows10、QT6.24,是可以运行的。想在linux环境测试下,结果出错了。所有希望弄清楚是什么原因。

看来百度AI回答的不准确啊

也许是Qt版本的差异?毕竟它报错就是提示QString没有first这个成员函数。

Reply View the author
忘记、过去
deepin
2023-10-09 01:30
#4
嵩云闲人

这是看到的一个例程,运行环境windows10、QT6.24,是可以运行的。想在linux环境测试下,结果出错了。所有希望弄清楚是什么原因。

说明 first() 和 last() 是 Qt 6 提供的新接口,Qt 5 不支持......

截图_选择区域_20231008173231.png

Reply View the author
嵩云闲人
deepin
2023-10-09 17:01
#5
忘记、过去

说明 first() 和 last() 是 Qt 6 提供的新接口,Qt 5 不支持......

截图_选择区域_20231008173231.png

谢谢!!!

Reply View the author