Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Why doesn't signal and slot in the following code work ?

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

Scheduled Pinned Locked Moved Solved General and Desktop
qt creator
7 Posts 3 Posters 1.8k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • AhtiA Offline
    AhtiA Offline
    Ahti
    wrote on last edited by Ahti
    #1

    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);
    }
    
    

    what is a signature ?? Lol

    jsulmJ 1 Reply Last reply
    0
    • AhtiA Ahti

      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);
      }
      
      
      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @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).

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      AhtiA 1 Reply Last reply
      1
      • jsulmJ jsulm

        @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).

        AhtiA Offline
        AhtiA Offline
        Ahti
        wrote on last edited by Ahti
        #3

        @jsulm

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

        what is a signature ?? Lol

        jsulmJ 1 Reply Last reply
        0
        • AhtiA Ahti

          @jsulm

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

          jsulmJ Offline
          jsulmJ Offline
          jsulm
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @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.

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          AhtiA 1 Reply Last reply
          1
          • jsulmJ jsulm

            @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.

            AhtiA Offline
            AhtiA Offline
            Ahti
            wrote on last edited by
            #5

            @jsulm

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

            what is a signature ?? Lol

            1 Reply Last reply
            0
            • AhtiA Offline
              AhtiA Offline
              Ahti
              wrote on last edited by Ahti
              #6

              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. :)

              what is a signature ?? Lol

              1 Reply Last reply
              1
              • BjornWB Offline
                BjornWB Offline
                BjornW
                wrote on last edited by
                #7

                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!

                1 Reply Last reply
                2

                • Login

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved