Errors I am getting in my class definition of Qt Widget Application



  • Hi,

    I've been working with QTimer for my timer application, but I've run into trouble when defining my Stopwatch class in the header file. This is my class definition:
    @
    class Stopwatch : public QObject
    {
    private:
    QTimer timer;
    Clock_Application* ui;
    public:
    Stopwatch();
    private slots:
    void changeTime();
    };

    Stopwatch::Stopwatch()
    {
    connect(&timer, SIGNAL(timeout()), this, SLOT(changeTime()));
    timer.start(1000);
    }

    void Stopwatch::changeTime()
    {
    /* code that tries to access widgets */
    }
    @

    The error I am getting (occurs in changeTime() function) when running my code is this:

    error: 'class Clock_Application' has no member named 'Output'

    The question is how do I get access to my widgets from a class that is NOT the starting class.

    P.S. The changeTime() function is a bit lengthy, but if you need to see it, I will post it.



  • Hi,

    I think you may consider some redesign.

    Some example how to do this in clean way:

    @
    class MainWindow : public QMainWindow
    {
    Q_OBJECT

    public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

    private:
    ClockWidget *clockWidget;
    Stopwach *stopwatch;
    };
    @

    now you're connecting necessary signals and slots in the MainWindow class. In this way you're having object independency.

    If you're insist on access to widget into your stopwatch, pass ClockWidget pointer to it ie.

    @
    class Stopwatch : public QObject
    {
    ...
    private:
    ClockWidget *clockwidget
    ...
    public:
    Stopwatch(ClockWidget *c) : clockwidget(c)
    ...
    }
    @

    Depends what excatly you need from ClockWidget i can give you more detailed advise.



  • gmaro,

    Thanks for the ideas. To answer your question, I am trying to access my textboxes in the widget application so I can change the text.

    I am still dissecting your code though (still learning C++), so your's could solve my problem.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.