http://blog.163.com/qimo601@126/blog/static/1582209320138249210813/
#include <QtGlobal> ... #ifdef Q_OS_MAC // mac #endif #ifdef Q_OS_LINUX // linux #endif #ifdef Q_OS_WIN32 // win #endif
See QtGlobal documentation for further information.
代码例子:
#ifdef Q_OS_LINUX
ui->portNameComboBox->addItem( "ttyUSB0");
ui->portNameComboBox->addItem( "ttyUSB1");
ui->portNameComboBox->addItem( "ttyUSB2");
ui->portNameComboBox->addItem( "ttyUSB3");
ui->portNameComboBox->addItem( "ttyS0");
ui->portNameComboBox->addItem( "ttyS1");
ui->portNameComboBox->addItem( "ttyS2");
ui->portNameComboBox->addItem( "ttyS3");
ui->portNameComboBox->addItem( "ttyS4");
ui->portNameComboBox->addItem( "ttyS5");
ui->portNameComboBox->addItem( "ttyS6");
#elif defined (Q_OS_WIN32)
ui->portNameComboBox->addItem("COM0");
ui->portNameComboBox->addItem("COM1");
ui->portNameComboBox->addItem("COM2");
ui->portNameComboBox->addItem("COM3");
ui->portNameComboBox->addItem("COM4");
ui->portNameComboBox->addItem("COM5");
ui->portNameComboBox->addItem("COM6");
ui->portNameComboBox->addItem("COM7");
ui->portNameComboBox->addItem("COM8");
ui->portNameComboBox->addItem("COM9");
ui->portNameComboBox->addItem("COM10");
ui->portNameComboBox->addItem("COM11");
ui->portNameComboBox->addItem("COM12");
ui->portNameComboBox->addItem("COM13");
ui->portNameComboBox->addItem("COM14");
ui->portNameComboBox->addItem("COM15");
#endif
可以自己查看qt帮助文档