tags:

views:

324

answers:

4

Hi..i am new to QT

the sample code mentioned below is not compiling ..please tell me the reason.

#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 problem at Connect Spot. i think it is fine to write connect at main function.

+4  A: 

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.

Skilldrick
Yes man You are right..what a silly mistake i have done :(
Shadow
A: 

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.

Kurt
There is no `this` in main, lest I'm misunderstanding.
GMan
+3  A: 

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 ;)

Lucian
+1  A: 

Hi,
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 !

Matthieu