Problems with connect() and some classes



  • Good night,

    I'm writing some code here but I'm getting this error:

    @Object::connect: No such slot gaugeView::updateValues(30) in ..\teste2\mainwindow.cpp:43
    Object::connect: No such slot gaugeView::updateValues(30) in ..\teste2\mainwindow.cpp:44@

    I tried everything I found at Google, but I can't find the error. The code of the class are:

    Main Window.cpp:
    @#include "mainwindow.h"
    #include "ui_mainwindow.h"
    #include "gaugeview.h"
    #include <QFileDialog>
    #include <QTimer>

    MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
    {
    ui->setupUi(this);

    int ra = rand();
    while(ra > 200)
    ra = rand();

    gaugeView * gauge1 = new gaugeView(centralWidget());
    ref = gauge1;
    QString pointerFile = ":/pointer.png";
    QString backgroundFile = ":/background.png";

    gauge1->setPointer(pointerFile);
    gauge1->setBackground(backgroundFile);
    gauge1->setPosition(0,0);
    gauge1->setDimensions(272,272);
    gauge1->create();
    gauge1->update(ra);

    ra = rand();
    while(ra > 200)
    ra = rand();

    gaugeView * gauge2 = new gaugeView(centralWidget());

    gauge2->setPointer(pointerFile);
    gauge2->setBackground(backgroundFile);
    gauge2->setPosition(0,271);
    gauge2->setDimensions(272,272);
    gauge2->create();
    gauge2->update(ra);

    QTimer * timerRand = new QTimer;
    connect(timerRand,SIGNAL(timeout()),gauge1,SLOT(updateValues(30)));
    connect(timerRand,SIGNAL(timeout()),gauge2,SLOT(updateValues(30)));
    timerRand->start(2000);
    }

    MainWindow::~MainWindow()
    {
    delete ui;
    }
    //void MainWindow::updateValues(gaugeView obj, int num)
    //{
    // obj.update(num);
    //}
    @

    MainWindow.h
    @#ifndef MAINWINDOW_H
    #define MAINWINDOW_H

    #include <QMainWindow>
    #include "gaugeview.h"

    namespace Ui {
    class MainWindow;
    }

    class MainWindow : public QMainWindow
    {
    Q_OBJECT

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

    private:
    Ui::MainWindow *ui;
    gaugeView * ref;

    //public slots:
    // void updateValues(int);
    };

    #endif // MAINWINDOW_H
    @

    gaugeview.h
    @#ifndef GAUGEVIEW_H
    #define GAUGEVIEW_H

    #include <QWidget>
    #include <QGraphicsScene>
    //#include <QPixmap>

    class gaugeView : public QWidget
    {
    Q_OBJECT
    public:
    explicit gaugeView(QWidget *parent = 0);
    void update(int);
    void create();
    void setPointer(QString);
    void setBackground(QString);
    void setPosition(int,int);
    void setDimensions(int,int);
    bool setDigital(bool);

    private:
    QPixmap pixPointer;
    QPixmap pixBackground;
    int posx, posy;
    int actualWidth,actualHeight;
    int actualValue, pastValue;
    bool showDigital;
    QGraphicsView * gp;
    QGraphicsScene * actualScene;
    void createDigital(void);

    public slots:
    void updateValues(int);
    };

    #endif // GAUGEVIEW_H
    @

    gaugeview.cpp
    @#include "gaugeview.h"
    #include <QPainter>
    #include <QGraphicsView>
    #include <QBrush>

    gaugeView::gaugeView(QWidget *parent) :
    QWidget(parent)
    {
    gp = new QGraphicsView(parentWidget());
    gp->move(0,0);
    gp->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    gp->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    gp->lower();
    }

    void gaugeView::setPointer(QString fileLocation)
    {
    pixPointer.fill(Qt::transparent);
    pixPointer.load(fileLocation);
    }

    void gaugeView::setBackground(QString fileLocation)
    {
    pixBackground.fill(Qt::transparent);
    pixBackground.load(fileLocation);
    }

    void gaugeView::setPosition(int x = 0,int y = 0)
    {
    posx = x;
    posy = y;
    gp->move(posx,posy);
    }

    bool gaugeView::setDigital(bool checkCond)
    {
    showDigital = checkCond;
    return checkCond;
    }

    void gaugeView::createDigital()
    {

    }

    void gaugeView::setDimensions(int width,int height)
    {
    actualWidth = width;
    actualHeight = height;
    pixPointer = pixPointer.scaled(actualWidth, actualHeight);
    pixBackground = pixBackground.scaled(actualWidth, actualHeight);
    }

    void gaugeView::create()
    {
    QBrush brushBackground;
    brushBackground.setTexture(pixBackground);

    actualScene = new QGraphicsScene();
    actualScene->clear();
    actualScene->addPixmap(pixPointer);

    gp->setBackgroundBrush(brushBackground);
    gp->setScene(actualScene);
    gp->show();
    }

    void gaugeView::update(int value)
    {
    pastValue = actualValue;
    actualValue = value;

    QSize size = pixPointer.size();
    QPixmap rotatedPixmap(size);
    rotatedPixmap.fill(Qt::transparent);

    QPainter * p = new QPainter(&rotatedPixmap);
    p->setRenderHint(QPainter::SmoothPixmapTransform);
    p->translate(size.width()/2,size.height()/2);
    p->rotate(actualValue);
    p->translate(-size.width()/2,-size.height()/2);
    p->drawPixmap(0,0,pixPointer);
    p->end();
    delete p;

    actualScene->clear();
    actualScene->addPixmap(rotatedPixmap);

    gp->setScene(actualScene);
    gp->move(posx,posy);
    gp->show();
    }

    void gaugeView::updateValues(int num = 0)
    {
    this->update(num);
    }
    @

    If someone have an idea of what is happening, please, post it.



  • You slot should be
    @
    updateValues(int)
    @

    instead of
    @
    updateValues(30)
    @



  • However,

    following statement still wrong
    @
    connect(timerRand,SIGNAL(timeout()),gauge1,SLOT(updateValues(int)));
    @

    params provided by signal should >= params needed by your slot.

    Read the manual carefully. http://doc-snapshot.qt-project.org/4.8/signalsandslots.html



  • So, in this logic, how I can update my gauges with QTimer?


  • Moderators

    Make your gauges member variables so they are accessible throughout your class.

    Then create a slot to do that specific task. For instance:

    @
    // slot:
    void setValuesOnTimeout() {
    gauge1->updateValues(30);
    gauge2->updateValues(30);
    }
    @

    Then connect like:
    @
    connect(timerRand,SIGNAL(timeout()),this,SLOT(setValuesOnTimeout()));
    @

    Edit:

    Alternately... (I think this might work, but I haven't tried it...)

    Make the default values on your updateValues slots = 30.

    Then just connect:
    @
    // These will use the default value of 30
    connect(timerRand,SIGNAL(timeout()),gauge1,SLOT(updateValues()));
    connect(timerRand,SIGNAL(timeout()),gauge2,SLOT(updateValues()));
    @



  • Thank you! It worked.

    [quote author="mlong" date="1335823162"]Make your gauges member variables so they are accessible throughout your class.

    Then create a slot to do that specific task. For instance:

    @
    // slot:
    void setValuesOnTimeout() {
    gauge1->updateValues(30);
    gauge2->updateValues(30);
    }
    @

    Then connect like:
    @
    connect(timerRand,SIGNAL(timeout()),this,SLOT(setValuesOnTimeout()));
    @

    Edit:

    Alternately... (I think this might work, but I haven't tried it...)

    Make the default values on your updateValues slots = 30.

    Then just connect:
    @
    // These will use the default value of 30
    connect(timerRand,SIGNAL(timeout()),gauge1,SLOT(updateValues()));
    connect(timerRand,SIGNAL(timeout()),gauge2,SLOT(updateValues()));
    @[/quote]


Log in to reply
 

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