PDA

View Full Version : QStringlist problem



anh5kor
27th November 2014, 15:34
Hello
I am using QStringlist in one of my class which return the list to other class for the future use.


class test
{
QStringList test:: getDeviceInfoList()
{

QStringList portList;


if (numDevs > 0)
{
// allocate storage for list based on numDevs
devInfo = (FT_DEVICE_LIST_INFO_NODE*)malloc(sizeof(FT_DEVICE _LIST_INFO_NODE)*numDevs);

// get the device information list
ftStatus = FT_GetDeviceInfoList(devInfo,&numDevs);

if (ftStatus == FT_OK)
{
for (int i = 0; i < numDevs; i++)
{

portList.append(QString(devInfo[i].SerialNumber));
}
}
return portList;
}

}
}

class test2
{
bool test2::connect()
{
bool serialPortResponding = false;

if (m_D2xxport->PortConfiguration( m_D2xxport->getDeviceInfoList().at(0).toStdString().c_str()));
{
serialPortResponding = true;
}
else
{
serialPortResponding = false;

}

return serialPortResponding;

}
}

The above code works fine if my portList contain some data.Some time it happens my portList does not contain any data in the case i get runtime error
on position

if (m_D2xxport->PortConfiguration( m_D2xxport->getDeviceInfoList().at(0).toStdString().c_str()));
I want to send null if my portList is empty or if list is empty send null if full then send portList .
QStringlist always show compile time error if i send Null or 0.

anda_skoa
27th November 2014, 16:00
There is a return missing in your getDeviceInfoList(), when the if condition is not met.
Just return QStringList()

And you need to check the returned list before attempting access by index.

Something like


const QStringList deviceInfos = m_D2xxport->getDeviceInfoList();
if (!deviceInfos.isEmpty() && m_D2xxport->PortConfiguration( deviceInfos.at(0).... )


Cheers,
_

anh5kor
28th November 2014, 10:29
Thanks anda_skoa for the solution .it's working for my code...