Preprocessor Macros to distinguish between Linux or Windows don't work ("no such slot")



  • Hi,
    I have an application I want to get to work on Linux and on Windows 7 by using macros to decide which parts of the code to use. I have some slots and functions, that aren't needed on linux and vice versa.

    Here is my minimal code:
    Mainwindow. h
    @#ifndef MAINWINDOW_H
    #define MAINWINDOW_H

    #include <QMainWindow>
    #include <QtGui>

    class MainWindow : public QMainWindow
    {
    Q_OBJECT

    public:

    MainWindow(QWidget *parent = 0);
    ~MainWindow();
    

    public slots:
    #ifdef _WIN32
    void onPushButtonClicked();
    #endif
    private:

     QPushButton* Pushbutton ;
    

    };

    #endif // MAINWINDOW_H
    @
    mainwindow.cpp
    @
    #include "mainwindow.h"
    #include <QtGui>

    MainWindow::MainWindow(QWidget parent)
    : QMainWindow(parent)
    {
    QWidget
    mainWidget = new QWidget(this);
    setCentralWidget(mainWidget);
    QWidget* mainPage = new QWidget(mainWidget);
    QVBoxLayout* mainLayout = new QVBoxLayout(mainPage);
    Pushbutton = new QPushButton("Test",mainPage);
    mainLayout->addWidget(Pushbutton);
    mainPage->setLayout(mainLayout);
    #ifdef _WIN32
    connect (Pushbutton, SIGNAL(clicked()), this, SLOT(onPushButtonClicked()));
    #endif
    }

    MainWindow::~MainWindow()
    {

    }
    #ifdef _WIN32
    void MainWindow::onPushButtonClicked()
    {
    Pushbutton->setStyleSheet("background-color: black");
    }
    #endif
    @
    Now the slot onPushButtonClicked() isn't found anymore:

    Start ~\TestPreprocessor-build-Desktop-Debug\debug\TestPreprocessor.exe...Object::connect: No such slot MainWindow::onPushButtonClicked() in ..\TestPreprocessor\mainwindow.cpp:17

    Only if I use my own defined macro or if I delete the #ifdef from the header file it works. But then I get a linker problem when compiling this on linux, as expected.

    Can anyone tell me how to do this correctly?


  • Lifetime Qt Champion

    Hi,

    Are you by any chance using Qt 4 ? If so, surrounding slots with ifdef won't work



  • Yes I am. Is there a work around?


  • Lifetime Qt Champion

    No there's not, however you can simply ifdef the content of the slot itself so it won't do anything on Linux.



  • Ok, thanks putting the #ifdef inside the function works. It's only problematic, if I have functions that take variables that are only defined by libraries existing in my linux project. For example keyboards, which I don't need for a desktop application in Windows...



  • Ok, thanks putting the #ifdef inside the function works. It's only problematic, if I have functions that take variables that are only defined by libraries existing in my linux project. For example keyboards, which I don't need for a desktop application in Windows...
    @
    #ifndef MAINWINDOW_H
    #define MAINWINDOW_H

    #include <QMainWindow>
    #include <QtGui>

    class MainWindow : public QMainWindow
    {
    Q_OBJECT

    public:
    MainWindow(QWidget *parent = 0);
    ~MainWindow();
    public slots:

    void onPushButtonClicked();
    

    private:
    QPushButton* Pushbutton ;
    };

    #endif // MAINWINDOW_H
    @
    @
    #include "mainwindow.h"
    #include <QtGui>

    MainWindow::MainWindow(QWidget parent)
    : QMainWindow(parent)
    {
    QWidget
    mainWidget = new QWidget(this);
    setCentralWidget(mainWidget);
    // mainWidget->resize(800,480),setFixedSize(sizeHint());

    QWidget* mainPage = new QWidget(mainWidget);
    QVBoxLayout* mainLayout = new QVBoxLayout(mainPage);
    Pushbutton = new QPushButton("Test",mainPage);
    mainLayout->addWidget(Pushbutton);
    mainPage->setLayout(mainLayout);
    

    #ifdef _WIN32
    connect (Pushbutton, SIGNAL(clicked()), this, SLOT(onPushButtonClicked()));
    #endif
    }

    MainWindow::~MainWindow()
    {

    }

    void MainWindow::onPushButtonClicked()
    {
    #ifdef _WIN32
    Pushbutton->setStyleSheet("background-color: black");
    #endif
    }
    @


  • Lifetime Qt Champion

    Why would function parameters have something to do there ? Can you show an example ?



  • I have a linux application running on a AM335x of Texas Instruments, which has no keyboard. So I use some Keyboard wrappers provided by another department at my firm. These libraries are only for Linux, so I #ifdef them in the header. But if I would like to use them as a variable in a function, it wouldn't be known to the compiler, see the example here:
    @
    #ifndef MAINWINDOW_H
    #define MAINWINDOW_H

    #include <QMainWindow>
    #include <QtGui>
    #ifdef unix
    #include "Keyboards/keyboards.h"
    #endif
    class MainWindow : public QMainWindow
    {
    Q_OBJECT

    public:
    MainWindow(QWidget *parent = 0);
    ~MainWindow();
    public slots:

    void onPushButtonClicked();
    void useKeyboard(EnglishKeyboard testKeyboard2); // <----------------
    

    private:
    QPushButton* Pushbutton ;
    #ifdef unix
    EnglishKeyboard testKeyboard;
    #endif
    };

    #endif // MAINWINDOW_H
    @

    @
    #include "mainwindow.h"
    #include <QtGui>

    MainWindow::MainWindow(QWidget parent)
    : QMainWindow(parent)
    {
    QWidget
    mainWidget = new QWidget(this);
    setCentralWidget(mainWidget);
    // mainWidget->resize(800,480),setFixedSize(sizeHint());

    QWidget* mainPage = new QWidget(mainWidget);
    QVBoxLayout* mainLayout = new QVBoxLayout(mainPage);
    Pushbutton = new QPushButton("Test",mainPage);
    mainLayout->addWidget(Pushbutton);
    mainPage->setLayout(mainLayout);
    

    #ifdef _WIN32
    connect (Pushbutton, SIGNAL(clicked()), this, SLOT(onPushButtonClicked()));
    #endif
    }

    MainWindow::~MainWindow()
    {

    }

    void MainWindow::useKeyboard(EnglishKeyboard testKeyboard2)
    {
    testKeyboard2;
    }

    void MainWindow::onPushButtonClicked()
    {
    #ifdef _WIN32
    Pushbutton->setStyleSheet("background-color: black");
    #endif
    }

    @


  • Lifetime Qt Champion

    You should rather use the Q_OS_XXX macros from Qt.

    You can either ifdef things like you are currently doing, or you could consider using the PIMPL idiom and thus only compile the right private part depending on the platform.


Log in to reply
 

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