Why doesn't signal and slot in the following code work ?



  • Whenever i try to run this code the size of text in a label
    doesn't get increased after every second it remains
    same why ?

    it should keep changing the size of text as soon as i click on the startcaringButton ( which is a pushbutton ).

    My Header File :

    ...
        QString Style ;
        QStringList list ;
    
        int currentSize ;
    
        void showTextMessage() ;
    
        QTimer *counter ;
    ...
    

    My Cpp File :

    EyeCare::EyeCare(QWidget *parent) :     // construtor
        QMainWindow(parent),
        ui(new Ui::EyeCare)
    {
        ui->setupUi(this);
    
        Style = ui->label->styleSheet() ;  // default string is "font: 18 Arial;"
        list = Style.split(" ") ;
        currentSize = list[1].toInt() ;    
    }
    void EyeCare::showTextMessage() {
    
        if (currentSize != 76){
    
            currentSize += 2 ;
            Style.replace(6,2,QString::number(currentSize)) ;
            ui->label->setStyleSheet(Style);
        }else
            counter->stop() ;
    }
    
    void EyeCare::on_startcaringButton_clicked()
    {
       
           counter = new QTimer(this) ;
                connect(counter,SIGNAL(timeout()),this,SLOT(showTextMessage())) ;
           counter->start(1000);
    }
    
    

  • Moderators

    @Ahti Did you try to debug? Is currentSize maybe 76? Did the connect() succeed (you can print out its return value to check). Is void showTextMessage() declared as slot? The easiest way to find out what the problem is is to use the debugger: set a break point in showTextMessage to see whether it is called and if so what happens there (and what the values of the variables are).



  • @jsulm

    otherwise its right ?
    and no currentSize i have already set to 18 in the constructor so how could it be 76 ?


  • Moderators

    @Ahti Well, not really. For example you never delete the timer but you always create a new one when the button is pressed. In showTextMessage if currentSize becomes bigger then 76 (for example 75 -> 77) you will never stop. But these issues are not related to your problem, you should really debug first.



  • @jsulm

    i think i miss "pt" in the font :D



  • i fixed it, the problem was with the wrong declaration of my slot function i should have declared it in the 'private slots:' but i had declared it in the 'private' section in the header file. And i am proud i fixed it on my own. :)



  • If you are using Qt5 I would strongly advice you to use the new signal/slot syntax. It has compile time type checking which is a great advantage. Also it allows you to connect to non-slot methods (you can use lambdas). Before I found signals/slots neat but a bit frustrating to work with because of the lack of compile time checks. Now they are just great!


Log in to reply
 

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