-1

I have the following code:

class myslot
    {

public:
    Q_OBJECT

    myslot()
        {

        }
    ~myslot()
        {

        }

    typedef enum  Emycars{volvo,benz,tata}cars;


public slots: 
void hellowslot(myslot::cars);
    };

void myslot::hellowslot(myslot::cars cars1)
    {

    }


class mysignal
    {
public:
    Q_OBJECT

public:
      mysignal(myslot *ourslot)
          {

     bool val = QObject::connect(this,SIGNAL(hellowsignal(myslot::Emycars)),ourslot,SLOT(hellowslot(myslot::Emycars)));
          }
      ~mysignal()
          {

          }

signals: 
void hellowsignal(myslot::Emycars);


    };

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);

    myslot slot;
    mysignal sig(&slot);


   // DeleteNow w;
   // w.showMaximized();
    return a.exec();
}

What is the mistake in my code? Is the way in which I have written connect for the function which receives an enum right or not?

JavaAndCSharp
  • 1,507
  • 3
  • 23
  • 47
Naruto
  • 9,476
  • 37
  • 118
  • 201

2 Answers2

7

In order to use the signal/slot mechanism the classes must inherit from QObject (either directly or from a subclass of QObject like QWidget) and declare themselves as such using the Q_OBJECT macro.

So, both your mysignaland myslot must inherit from QObject.

Moreover you must place the macro right after the opening brace of your class, this should give:

class myslot : public QObject
{
    Q_OBJECT
public:
// .../... 
};

class mysignal : public QObject
{
    Q_OBJECT
public:
// .../... 
};
gregseth
  • 12,952
  • 15
  • 63
  • 96
0

You have problem with signal/slot connection ? If yes, then maybe you should do:

bool val = QObject::connect( this, SIGNAL(hellowsignal(myslot::cars)), ourslot, SLOT(hellowslot(myslot::cars)));

becouse you declared your slots with myslot::cars and not myslot::Emycars. Metaobject compiler simply creates strings that are related to your slots, thats why your connection may not work.

Kamil Klimek
  • 12,884
  • 2
  • 43
  • 58