Search code examples
qtqtserialport

no match for 'operator==' (operand types are QSerialPortInfo and const QSerialPortInfo)


The following code is part of subclass of QThread in which I am trying to discover any newly connected Serial Ports.

void PortEnumerationThread::run ()
{
  QList<QSerialPortInfo> port_list, discovered_ports;

  discovered_ports = QSerialPortInfo::availablePorts();


  int i;

  while (1)
  {
    port_list = QSerialPortInfo::availablePorts();

    for (i=0;i<port_list.size();i++)
    {
        if (!discovered_ports.contains(port_list.at(i)))
            emit new_port_found (port_list.at(i).portName());
    }
    discovered_ports = port_list;
    sleep ( SLEEP_TIME );
  }

}

Above code is throwing following compilation error at line
if (!discovered_ports.contains(port_list.at(i))):

QtCore/qlist.h: error: no match for 'operator==' (operand types are QSerialPortInfo and const QSerialPortInfo)


Solution

  • If you need to you could always implement your own version of operator== for QSerialPortInfo. You'd need to make certain it didn't have other undesirable side effects elsewhere in your code though.

    Something like...

    #include <tuple>
    #include <QList>
    #include <QSerialPortInfo>
    
    bool operator== (const QSerialPortInfo &a, const QSerialPortInfo &b)
    {
      return
        std::forward_as_tuple(a.description(),
                              a.manufacturer(),
                              a.portName(),
                              a.productIdentifier(),
                              a.serialNumber())
        ==
        std::forward_as_tuple(b.description(),
                              b.manufacturer(),
                              b.portName(),
                              b.productIdentifier(),
                              b.serialNumber());
    }
    

    You'd probably want to check the subset of properties you consider to be of importance for equality -- I just had a rough guess.