Problem with slots and signals.



  • Hello Everyone,

    I am new to Qt(C++). I am trying a new program using only one slider and one labels. I am using slot and signal to. connect the slider with the label.

    My intention is to vary the text on the label using the slider. When i move the slider the text on the label should vary.

    For example if the slider range is from (1-50). And i have a fixed text on the label "cat".

    If i slide the slider by one step the label should display "cat1"
    if i slide it by one more step the label should display "cat2"
    if i slide it by one more step the label should display "cat3"
    ..
    ..
    ..
    if i slide it by one more step the label should display "cat50".

    I hope my explanation is clear.

    I have written a code and i getting the error "No such slot QLabel::img_view.read_image( slider_val, img_view_ptr)"

    i have used the QObject in the header file.

    my header file is as follows



    test.h

    #ifndef TEST_H
    #define TEST_H

    #endif // TEST_H
    #include<QObject>
    #include<QHBoxLayout>
    #include<QSpinBox>
    #include<QSlider>
    #include<QFileDialog>
    #include<QLabel>
    #include<iostream>

    class test: public QObject
    {
    Q_OBJECT
    public:
    QWidget *window1;
    QSlider *slider;
    QLabel *myLabel, *label;
    QHBoxLayout *layout;

    //test();
    //~test();
    

    public slots:
    void read_image(int, test*);

    };



    main.cpp

    #include "mainwindow.h"
    #include <QApplication>
    #include"test.h"
    #include<QObject>
    #include<QHBoxLayout>
    #include<QSpinBox>
    #include<QSlider>
    #include<QFileDialog>
    #include<QLabel>
    #include<iostream>

    int main(int argc, char *argv[])
    {
    QApplication a(argc, argv);
    //MainWindow w;
    //w.show();

    test img_view;
    
    test *img_view_ptr;
    
    img_view_ptr = &img_view;
    

    img_view.window1 = new QWidget;
    img_view.window1->setWindowTitle("image slider");

    img_view.slider = new QSlider(Qt::Horizontal);
    img_view.label = new QLabel(img_view.window1);
    img_view.myLabel = new QLabel(img_view.window1);
    img_view.layout = new QHBoxLayout;

    img_view.label->setText("holahola");

    img_view.slider->setRange(0,10);
    int slider_val = img_view.slider->value();
    QObject::connect(     img_view.slider,  SIGNAL(valueChanged(int)),
                          img_view.myLabel, SLOT(img_view.read_image( slider_val, img_view_ptr)));
    

    img_view.layout->addWidget(img_view.slider);
    img_view.layout->addWidget(img_view.label);

    img_view.window1->setLayout(img_view.layout);
    img_view.window1->show();

    return a.exec();
    

    }

    void test::read_image(int val, test *img_view_ptr)
    {

    QString display = "holahola" + val;
    img_view_ptr->label->setText(display);
    

    }



    Can anyone please help me in solving this problem.


  • Moderators

    @demonferrari said in Problem with slots and signals.:

    QObject::connect( img_view.slider, SIGNAL(valueChanged(int)),
    img_view.myLabel, SLOT(img_view.read_image( slider_val, img_view_ptr)));

    2 mistakes:

    1. parameters in the slot should only contain the types not any names
    2. you can't connect a signal with 1 parameter to a slot expecting 2 parameters


  • @raven-worx

    Hi Thanks for the reply.

    I followed your instructions and did the changes

    QObject::connect( img_view.slider, SIGNAL(valueChanged(int)),
    img_view.myLabel, SLOT(img_view.read_image(int)));

    but i am still having the same error "No such slot QLabel::img_view.read_image(int)"


  • Moderators

    @demonferrari
    ok sry, you had actually 3 mistakes, not 2 :)
    img_view.read_image() is not a valid function-name.

    I guess this is what you want?

    QObject::connect( img_view.slider, SIGNAL(valueChanged(int)),
    img_view.myLabel, SLOT(read_image(int)));
    

    Where is read_image() defined? on img_view.myLabel or on img_view ?

    Also to make sure: did you also change the function signature (definition) of the read_image method?! Simply removing the parameter in the connect statement won't change anything unless you also do not remove the parameter from the actual function definition.



  • @raven-worx

    I have also made the corresponding changes in the function parameters :) . I forgot to mention in my previous reply. Sorry about that.

    read_image() is a function slot present in test class.



    public slots:
    void read_image(int);



    it is defined in main.cpp .
    read_image(int) function just updates the string and tries to display it on the label.

    void test::read_image(int val)
    {

    QString display = "holahola" + val;
    this->label->setText(display);
    

    }


  • Moderators

    @demonferrari
    then do

    QObject::connect( img_view.slider, SIGNAL(valueChanged(int)), &img_view, SLOT(read_image(int)) );
    


  • @raven-worx said in Problem with slots and signals.:

    1. you can't connect a signal with 1 parameter to a slot expecting 2 parameters

    thats actually not true. take this example :-P

    #ifndef TEST_H
    #define TEST_H
    
    #include <QObject>
    
    class Test : public QObject
    {
        Q_OBJECT
    public:
        explicit Test(QObject *parent = nullptr);
    
    signals:
        void signalWithOneArg(int number);
    
    public slots:
        void slotWithTwoArgs(int number, QString text = "I'm a text");
    };
    
    #endif // TEST_H
    
    
    #include "test.h"
    #include <QDebug>
    
    Test::Test(QObject *parent) : QObject(parent)
    {
        connect(this, SIGNAL(signalWithOneArg(int)), this, SLOT(slotWithTwoArgs(int)));
        emit signalWithOneArg(10);
    }
    
    void Test::slotWithTwoArgs(int number, QString text)
    {
        qDebug() << number << text;
    }
    
    

    works fine, but only with old Signal/SLot syntax for obvious reasons.



  • @raven-worx

    Hi, i did the changes and it worked correctly without any errors.

    I didnt understand why you passed the address of img_view (&img_view)? Why wasn't it working before and why is it working now?


  • Moderators

    @demonferrari said in Problem with slots and signals.:

    I didnt understand why you passed the address of img_view (&img_view)?

    because the connect() method expects a pointer to an object.

    @demonferrari said in Problem with slots and signals.:

    Why wasn't it working before and why is it working now?

    it didn't work before for the reasons i already mentioned? Or what do you mean?


  • Lifetime Qt Champion

    Hi,

    @J-Hilk it's a bit of nitpicking, in your example, your slot doesn't really expect 2 parameters as one of them has a default value. So for what @raven-worx wrote, let's rephrase it a bit: you can't connect a signal with one parameter to a slot which requires two parameters.


Log in to reply
 

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