Qt 如何使用QlistIterator在Qlist中迭代类
我想显示包含Airports类实例的Qlist的完整内容。该类本身包含另一个集成跑道类实例的Qlist 以下是Qlist m_apList的结构示例:Qt 如何使用QlistIterator在Qlist中迭代类,qt,qlist,Qt,Qlist,我想显示包含Airports类实例的Qlist的完整内容。该类本身包含另一个集成跑道类实例的Qlist 以下是Qlist m_apList的结构示例: m_apList <2 éléments> QList<Airport> [0] @0x29265df0 Airport m_code "LFPO" QString m_name "PARIS/ORLY" QString
m_apList <2 éléments> QList<Airport>
[0] @0x29265df0 Airport
m_code "LFPO" QString
m_name "PARIS/ORLY" QString
m_position @0x29265df0 QGeoCoordinate
m_runway <6 éléments> QList<Runway>
[0] @0x292660d0 Runway
m_freqIls 110.3 double
m_ils 1 int
m_ilsRdh_ft 50 int
m_ilsSlope_deg 3.0 double
m_length 7874 int
m_loc 19 int
m_name "02" QString
m_position @0x292660d0 QGeoCoordinate
m_qfu 19 int
m_width 197 int
[1] @0x292661b8 Runway
m_freqIls 108.5 double
m_ils 1 int
m_ilsRdh_ft 54 int
m_ilsSlope_deg 3.0 double
m_length 11975 int
m_loc 63 int
m_name "06" QString
m_position @0x292661b8 QGeoCoordinate
m_qfu 63 int
m_width 148 int
[2] @0x29266270 Runway
[3] @0x29266358 Runway
[4] @0x29266468 Runway
[5] @0x292664f0 Runway
m_runwayLen 11900 int
m_unknown1 5000 int
m_unknown2 0 int
[1] @0x29265f50 Airport
m_code "LFPP" QString
m_name "LE PLESSIS/BELLEVILL" QString
m_position @0x29265f50 QGeoCoordinate
m_runway <4 éléments> QList<Runway>
[0] @0x292666e0 Runway
m_freqIls 0.0 double
m_ils 0 int
m_ilsRdh_ft 50 int
m_ilsSlope_deg 0.0 double
m_length 2297 int
m_loc 0 int
m_name "07" QString
m_position @0x292666e0 QGeoCoordinate
m_qfu 66 int
m_width 66 int
[1] @0x29266800 Runway
[2] @0x29266888 Runway
[3] @0x29265548 Runway
m_runwayLen 2700 int
m_unknown1 0 int
m_unknown2 0 int
m_apList QList,需要创建一个迭代器来展开QList。
但是我得到了这个错误消息“no match for”operator你不能像那样用qDebug()打印出你的Airport
对象。你能做的就是自己序列化Airport对象,例如:
void listAirports()
{
foreach (const Airport &airport, m_apList)
{
// Print out the airport's properties
qDebug() << "Code:" << airport.m_code; // Might be a member functions
qDebug() << "Name:" << airport.m_name;
// etc.
// Print out ranaways
const auto &ranways = airport.ranways();
foreach (const Ranway &ranway, ranways)
{
qDebug() << "m_freqIls" << ranway.m_freqIls;
qDebug() << "m_ilsSlope_deg " << ranway.m_ilsSlope_deg;
// etc.
}
}
}
void list airports()
{
foreach(康斯特机场和机场,m_apList)
{
//打印出机场的属性
qDebug()你不能像那样用qDebug()打印出你的Airport
对象。你能做的就是自己序列化Airport对象,例如:
void listAirports()
{
foreach (const Airport &airport, m_apList)
{
// Print out the airport's properties
qDebug() << "Code:" << airport.m_code; // Might be a member functions
qDebug() << "Name:" << airport.m_name;
// etc.
// Print out ranaways
const auto &ranways = airport.ranways();
foreach (const Ranway &ranway, ranways)
{
qDebug() << "m_freqIls" << ranway.m_freqIls;
qDebug() << "m_ilsSlope_deg " << ranway.m_ilsSlope_deg;
// etc.
}
}
}
void list airports()
{
foreach(康斯特机场和机场,m_apList)
{
//打印出机场的属性
qDebug()您可以演示如何将AirPort实例添加到Qlist中吗?AirPort的可能副本是通过PS中的addAirport()函数添加的。感谢您关注此作业。您可以演示如何将AirPort实例添加到Qlist中吗?AirPort的可能副本是通过addAirport()添加的函数,以PS表示。感谢您的关注。感谢vahancho。它适用于第一部分“机场属性”。但我无法访问此处的跑道:const auto&ranways=airport.ranways();我将访问器设置为:QList ranways(){return mu trunway;}获取此错误:从类型为'QList'auto&trunk=airport.ranways()的右值初始化类型为'QList&'的非常量引用无效;此补码:将'const airport'作为'this'参数传递将丢弃限定符[-fppermissive]auto&trunk=airport.ranways();您必须返回对m_ranway对象的引用,而不是副本,也就是说,您的函数应该是:const QList&ranways()const{return m_trank;}
。感谢您的帮助,我非常感谢。谢谢vahancho。它适用于第一部分“机场属性”。但我无法访问此处的跑道:const auto&ranways=airport.ranways();我将访问者设置为:QList ranways(){return m_trunway;}获取此错误:从类型为'QList'auto&trunk=airport.ranways()的右值初始化类型为'QList&'的非常量引用无效;此补码:将'const airport'作为'this'参数传递将丢弃限定符[-fppermissive]auto&trunk=airport.ranways();您必须返回对m_ranway对象的引用,而不是副本,也就是说,您的函数应该是:const QList&ranways()const{return m_runway;}
。感谢您的帮助,我非常感谢。
#ifndef AIRPORT_H
#define AIRPORT_H
#include <QGeoCoordinate>
#include <QString>
class Runway
{
public:
Runway(QString name, int qfu, int length_ft, int width, int ils, double freq_Ils,
int loc, double latitude, double longitude, double elevation_ft, double ilsSlope_deg, int ilsRdh_ft)
: m_name(name), m_qfu(qfu), m_length(length_ft), m_width(width),
m_ils(ils), m_freqIls(freq_Ils), m_loc(loc), m_ilsSlope_deg(ilsSlope_deg), m_ilsRdh_ft(ilsRdh_ft)
{
m_position.setLatitude(latitude);
m_position.setLongitude(longitude);
m_position.setAltitude(elevation_ft);
}
QString nameRunway() const { //lecture du code OACI
return m_name;
}
private:
QGeoCoordinate m_position;
QString m_name;
int m_qfu;
int m_length;
int m_width;
int m_ils;
double m_freqIls;
int m_loc;
double m_ilsSlope_deg;
int m_ilsRdh_ft;
};
class Airport
{
public:
Airport(QString code, QString name, double latitude, double longitude,
double elevation_ft, int unknown1, int unknown2, int runwayLenght_ft)
:m_code(code), m_name(name), m_unknown1(unknown1), m_unknown2(unknown2),
m_runwayLen(runwayLenght_ft) {
m_position.setLatitude(latitude);
m_position.setLongitude(longitude);
m_position.setAltitude(elevation_ft);
}
void setPosition(const QGeoCoordinate &c) { //Affectation des nouvelles coordonnees de position
m_position = c;
}
QGeoCoordinate position() const{
return m_position; //Lecture des coordonnees de position
}
QString oaciCode() const { //lecture du code OACI
return m_code;
}
QString airportName() const {
return m_name;
}
int runwayLength() const { //lecture du pays
return m_runwayLen;
}
void addRunway(const Runway &runway){
m_runway << runway;
}
private:
QGeoCoordinate m_position;
QString m_code;
QString m_name;
int m_unknown1;
int m_unknown2;
int m_runwayLen;
QList<Runway> m_runway;
};
#endif // AIRPORT_H
void listAirports()
{
foreach (const Airport &airport, m_apList)
{
// Print out the airport's properties
qDebug() << "Code:" << airport.m_code; // Might be a member functions
qDebug() << "Name:" << airport.m_name;
// etc.
// Print out ranaways
const auto &ranways = airport.ranways();
foreach (const Ranway &ranway, ranways)
{
qDebug() << "m_freqIls" << ranway.m_freqIls;
qDebug() << "m_ilsSlope_deg " << ranway.m_ilsSlope_deg;
// etc.
}
}
}