[Solved] Error: Invalid use of member (...) in static member function



  • I have a UI window created in UI Designer called MonitorWindow. It has a child widget called DualDial. Basically what I'm doing is drawing 2 dials on the MonitorWindow. One is like a second hand, and the other is like a minute hand on a clock. These dials are updated with a timer (that's called by DualDial) that goes off every 100ms. All of this works fine. On the MonitorWindow UI, there's also a text label, timeLabel, that displays the current time, and I want to update that label with the timer that's running in DualDial. I've connected a signal, timeChanged, to a slot called UpdateTimeSlot in MonitorWindow. Here's the monitorwindow.h file:
    @
    namespace Ui {
    class MonitorWindow;
    }
    class MonitorWindow : public QWidget
    {
    Q_OBJECT
    public:
    explicit MonitorWindow(QWidget *parent = 0);
    ~MonitorWindow();
    Ui::MonitorWindow *ui;
    public slots:
    static void UpdateTimeSlot();
    };
    @
    Then, in the MonitorWindow constructor, I instatiate the child widget called DualDial:
    @
    DualDial *dualdial = new DualDial(this);
    @
    In the DualDial constructor, I connect the signal to the slot:
    @
    connect(this, timeChanged(), parent, MonitorWindow::UpdateTimeSlot());
    @
    The timeChanged signal is emitted inside the timer function for DualDial, so this should call MonitorWindow::UpdateTimeSlot. Inside that function I create a string called timeStr and then just update the timeLabel in the UI with the new string:
    @
    ui->timeLabel->setText(timeStr);
    @
    When I try to compile, it gives me the error: invalid use of member 'MonitorWindow::ui' in static member function. It points to the line of code above. What am I doing wrong?



  • Why you want ahve static slot?



  • When the UpdateTimeSlot wasn't static, I got the error: "cannot call member function 'MonitorWindow::UpdateTimeSlot()' without object" when I tried to connect the signal and the slot:
    @
    connect(this, timeChanged(), parent, MonitorWindow::UpdateTimeSlot());
    @



  • I guess parent is pointer to MonitorWindow instance. right?
    When you need:

    @connect(this, SIGNAL(timeChanged()), parent, SLOT(UpdateTimeSlot()));@

    and remove static



  • It then tells me that UpdateTimeSlot was not declared in this scope. Here's the beginning of the dualdial.cpp file with the constructor:
    @
    #include "dualdial.h"
    #include "global.h"
    #include "monitorwindow.h"

    DualDial::DualDial(QWidget *parent) :
    QWidget(parent)
    {
    move(465, 100);
    resize(400, 150);
    connect(this, timeChanged(), parent, UpdateTimeSlot());
    QTimer *timer = new QTimer(this);
    connect(timer, SIGNAL(timeout()), this, SLOT(update()));
    timer->start(10);
    }
    @
    And if you need it, here's the dualdial.h file:
    @
    #include <QtGui>

    class DualDial : public QWidget
    {
    Q_OBJECT
    public:
    explicit DualDial(QWidget *parent = 0);
    signals:
    void timeChanged();
    protected:
    void paintEvent(QPaintEvent *);
    };
    @


  • Moderators

    At first glance, in line 10, you need to wrap your signal and slot names in SIGNAL and SLOT macros.

    @
        connect(this, SIGNAL(timeChanged()), parent, SLOT(UpdateTimeSlot()));
    @

    There may be other issues, but this one is important.



  • And here I thought it was some heavy C++ issue that I wouldn't understand. :o) It was right there in front of me!! Thanks both of you for your help!


Log in to reply
 

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