Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Preprocessor Macros to distinguish between Linux or Windows don't work ("no such slot")
Forum Updated to NodeBB v4.3 + New Features

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

Scheduled Pinned Locked Moved General and Desktop
9 Posts 2 Posters 3.0k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Offline
    N Offline
    Ninawe
    wrote on last edited by
    #1

    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?

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

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

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • N Offline
        N Offline
        Ninawe
        wrote on last edited by
        #3

        Yes I am. Is there a work around?

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

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

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0
          • N Offline
            N Offline
            Ninawe
            wrote on last edited by
            #5

            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...

            1 Reply Last reply
            0
            • N Offline
              N Offline
              Ninawe
              wrote on last edited by
              #6

              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
              }
              @

              1 Reply Last reply
              0
              • SGaistS Offline
                SGaistS Offline
                SGaist
                Lifetime Qt Champion
                wrote on last edited by
                #7

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

                Interested in AI ? www.idiap.ch
                Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                1 Reply Last reply
                0
                • N Offline
                  N Offline
                  Ninawe
                  wrote on last edited by
                  #8

                  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
                  }

                  @

                  1 Reply Last reply
                  0
                  • SGaistS Offline
                    SGaistS Offline
                    SGaist
                    Lifetime Qt Champion
                    wrote on last edited by
                    #9

                    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.

                    Interested in AI ? www.idiap.ch
                    Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                    1 Reply Last reply
                    0

                    • Login

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • Users
                    • Groups
                    • Search
                    • Get Qt Extensions
                    • Unsolved