[RESOLVED] Spefic code for Plattaform...



  • Hello everyone!
    I'm trying to make a resource in which I need the make a 2 specific codes for Linux and for Windows.
    I need something like "if (LINUX) {} ELSE IF (WINDOWS)..." or maybe a switch....
    I read something, but always is something with the compilation, but I need something in rutime of my application...
    How can I do that?

    I found "this class":http://doc.qt.nokia.com/4.6/qsysinfo.html, but I don't think that is the easy way to solve that!



  • Why do you need to do run-time detection? If your application is to run on Windows, it will be compiled for Windows. Same applies for Linux/Mac.
    Just use the "Q_OS_*":http://qt-project.org/doc/qt-4.8/qtglobal.html#Q_OS_AIX macros and split your platform-specific code up with those.



  • I need to filter in linux the Serial Port...
    Some Distro show all tty*** and I need to select only ttyACM*...
    On windows I don't need to filter anything... so I need to that in rutime at load a QComboBox with the serial ports...

    Any other better Idea to solve my problem?



  • You don't need run-time, just use #ifdef with Q_OS_* where you want to use if(), and wrap the platform-dependant code like that.
    @
    ...generic code here...
    #ifdef Q_OS_LINUX
    ...Linux specific code here...
    #elif defined(Q_OS_WINDOWS)
    ...Windows specific code here...
    #endif
    ...rest of code here...
    @



  • I will look for more examples, this resource is new for me.
    THanks



  • Thanks Terence!
    I make this code below:

    @
    #ifdef Q_OS_LINUX
    void DialogPortaSerial::refreshLista()
    {
    QList<QextPortInfo> lista = QextSerialEnumerator::getPorts();
    ui->cbListaPortasSeriais->clear();
    foreach (QextPortInfo info, lista) {
    if (info.portName.startsWith("ttyACM"))
    ui->cbListaPortasSeriais->insertItem(0, QIcon(), info.portName, "");
    }
    }
    #elif defined(Q_OS_WINDOWS)
    void DialogPortaSerial::refreshLista()
    {
    QList<QextPortInfo> lista = QextSerialEnumerator::getPorts();
    ui->cbListaPortasSeriais->clear();
    foreach (QextPortInfo info, lista) {
    //TODO: Check if is working nice on Windows.
    ui->cbListaPortasSeriais->insertItem(0, QIcon(), info.portName, "");
    }
    }
    #endif
    @

    Is that right?!



  • There is no "Q_OS_WINDOWS", you should use "Q_OS_WIN" instead.
    However, your design duplicates a lot of code, you can move the #ifdef to make it easier to read (and work on):
    @
    void DialogPortaSerial::refreshLista()
    {
    QList<QextPortInfo> lista = QextSerialEnumerator::getPorts();
    ui->cbListaPortasSeriais->clear();
    foreach (QextPortInfo info, lista) {
    #ifdef Q_OS_LINUX
    if (info.portName.startsWith("ttyACM"))
    #endif
    ui->cbListaPortasSeriais->insertItem(0, QIcon(), info.portName, "");
    }
    }
    @



  • Wow, very very cool!
    Thanks... Now I got the "power" of "#ifdef".

    Thanks again!


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.