0

I am using Qt code in a ROS node. I have declared a static function setLabel() in my class. The role of this function is to put an image into a QLabel. Now, I want to call this function when I click a button using a signal/slot connection. Please tell me what should I put at place of the question mark.

class ImageDisplay: public QObject
{

Q_OBJECT    

    public slots:
    void setLabel();    

    public: 
    static void imageCallback( ); 

};


void ImageDisplay::setLabel()
{

        QLabel* selectLabel= new QLabel();
        selectLabel->setText("hi");     
        selectLabel->show();
}    

void imageDisplay::imageCallBack()
{
    ImageDisplay obj;

    QObject::connect(selectButton, SIGNAL(clicked()),&obj, SLOT(setLabel()));       
}
skm
  • 5,015
  • 8
  • 43
  • 104
  • I'll rephrase: what version of Qt you are compiling with? – cmannett85 May 31 '13 at 14:24
  • i am using qt 4. I have already tried to do QObject::connect(selectButton, SIGNAL(clicked()),this, SLOT(setLabel())). But then i get an error "‘this’ is unavailable for static member functions" – skm May 31 '13 at 14:28
  • @NikosC.- yes, i saw that post earlier but i really did not understand. In my case the problem is that i am using QT code in ROS so i my unable to use "this" – skm May 31 '13 at 14:54
  • i tried to use QObject::connect(selectButton, SIGNAL(clicked()),STATIC_SLOT(setLabel())); but then i get error "error: ‘STATIC_SLOT’ was not declared in this scope " – skm May 31 '13 at 15:30
  • What did you not understand about the answer? It gives you a clear answer: "No it is not allowed.". – cmannett85 May 31 '13 at 20:41
  • Why do you need your method to be `static`? It looks like bad design idea for me. – SpongeBobFan Jun 03 '13 at 15:17
  • @user2440724 - just use a wrapper QObject derived to trigger the static function. With Qt5 you can connect to any method, including lambdas, so you don't even need the wrapper, just use a lambda instead. – dtech Jun 03 '13 at 16:14

2 Answers2

2

You try this,

QObject::connect(selectButton, SIGNAL(clicked()), listenerObj, SLOT(setLabel()));

listenerObj is the object pointer of class that you declared your slot. If you are unable to use "this" in listener, you declare an active object which contains a public slot of your function setLabel and connect the slot.

declare setLabel() as public slot in header file of your new class

class SomeClass
{
public slots:
void setLabel();
}

then using parent pointer you could show the label in interface

I think some of this will help you.

imalvare
  • 295
  • 1
  • 13
  • Thanks. Actually at the moment i cant check my program before monday. But please tell me, can i declare "Public slots:" in a class when i am not writting my code in QT project. I have just included the header files of QT creator into the code of my ROS node. – skm Jun 01 '13 at 19:39
  • You can if it contains QObject base class inherited with macro. You just refer a class created with QtCreator. – imalvare Jun 03 '13 at 05:09
0

First, get rid of global variable. Why do you need it? Global variables are vary bad and should be avoided.
Second, add Q_OBJECT macro to myQtClass and do qmake.
Third, your setLabel() slot shouldn't be private, make it public if you want to use it outside of myQtClass.

SpongeBobFan
  • 964
  • 5
  • 13
  • thanks, i have updated my code above. Its fine but i am getting the erro because of Q_OBJECT macro. Error is "undefined reference to `vtable for imageDisplay'" – skm Jun 03 '13 at 19:11
  • @user2440724: if `ImageDisplay` is declared in the .cpp file instead of in a separate .h file, then you have to add the following to the end of the .cpp file to force Qt's moc to be run over the .cpp file - by default it runs only against .h files: `#include "ImageDisplay.moc"` (assuming that the source filename is `ImgeDisplay.cpp`) – Michael Burr Jun 03 '13 at 19:20
  • thanks, i tried but now i am getting an error "no such directory" for #include "myCppFileName.moc" – skm Jun 04 '13 at 20:52
  • 1
    Can you just move `ImageDisplay` class declaration to `.h` file? If you cannot, i'd try to google for something like "qobject declaration .cpp", maybe there's some guides about it. But it's much better and easier to just move class declaration to where it supposed to be. – SpongeBobFan Jun 05 '13 at 08:16
  • @user2440724: I think SpongeBobFan's advice is good. But in either case you might need to re-run `qmake` yourself to make sure the moc file gets generated properly. I honestly can't remember if that step is necessary or not. – Michael Burr Jun 05 '13 at 08:36