Tutorial :What is the mistake in my code?



Question:

The sample code mentioned below is not compiling. Why?

#include "QprogressBar.h"    #include <QtGui>  #include <QApplication>  #include<qprogressbar.h>  #include <qobject.h>    lass myTimer: public QTimer  {    public:      myTimer(QWidget *parent=0):QTimer(parent)  {}    public slots:  void recivetime();  };  void myTimer::recivetime()      {        }    class Progressbar: public QProgressDialog      {  public:      Progressbar(QWidget *parent=0):QProgressDialog(parent)              {                }          };    int main(int argc, char *argv[])  {      QApplication a(argc, argv);       QObject::connect(QTimer,SIGNAL(timeout()),QTimer,SLOT(recivetime()));       return a.exec();    }  

It is giving me a problem when it tries to connect. I think that maybe it is fine to write the connect code in the main function.


Solution:1

To sum-up the previous comments and answers:

  • the compiler tells you at least what it does not understand if not straight-up what's wrong with your code => if you don't understand what the compiler says post the error message with your question so that it helps those who speak "compilese"
  • "connect" will connect a object's signal with another object's slot -> pass objects to connect, not classes
  • the connected objects must exist for the intended duration of your connection you are connecting now at best automatic instances of QTimer which will be out of scope by the time the connect call ends.

The correct way of doing this:

int main(int argc, char *argv[])      {          QApplication a(argc, argv);            myTimer myTimerObject(a);            QObject::connect(&myTimerObject, SIGNAL(timeout()), &myTimerObject, SLOT(recivetime()));            return a.exec();        }  

As a side note this has nothing to do with Symbian, nor is it specific to Qt 4.x. Also Qt is not QT just as QT is not Qt ;)


Solution:2

Where is your QTimer? I think that's the problem. I haven't done Qt for a while, but as far as I remember, the first and third arguments of connect are pointers to objects, and you don't have a QTimer pointer.


Solution:3

Skilldrick is right !
See the qt doc on signals and slots. The connect method needs a pointer or reference of the sender and receiver object !
But in your code :

QObject::connect(QTimer,SIGNAL(timeout()),QTimer,SLOT(recivetime()));  

QTimer is a class name and not a object of this class ! I mean, you need to create an object. For example :

QTimer* pTimer = new QTimer(a);   // QTimer object     myTimer* pReciever = new myTimer(a);  // Your custom QTimer object with progress bar   QObject::connect(pTimer,SIGNAL(timeout()), pReciever,SLOT(recivetime()));     ...  

Hope it helps !


Solution:4

Not sure, but try:

QObject::connect(myTimer,SIGNAL(timeout()),this,SLOT(recivetime()));  

Oops, thought myTimer was an instance of QTimer rather than a subclass. Make an instance of QTimer and give that as the first parameter. And this as the third.


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »