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. [SOLVED]Slot not applied, why?
QtWS25 Last Chance

[SOLVED]Slot not applied, why?

Scheduled Pinned Locked Moved General and Desktop
7 Posts 4 Posters 1.7k 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.
  • A Offline
    A Offline
    amonR
    wrote on last edited by
    #1

    Hi everyone, I'm trying to change the color of a stylesheet every 2.5 seconds with a globale variable spread in other classes and a QTimer.
    The change of color is supposed to be applied inside the slot. However, nothing change. Why? Someone has any idea?
    Here is my code:
    @
    //StyleT.h
    #include <QtGui>

    class StyleT : public QMainWindow
    {
    Q_OBJECT

    public:
    StyleT();
    static QString CoulorStyle;
    
    public slots:
    void TransitionStyle();
    
    private:
    QTimer *tempo;
    

    };
    @
    @
    //StyleT.cpp
    #include <QtGui>
    #include "StyleT.h"
    #include "ClassA.h"
    QString StyleT::CouleurStyle = "#F24C04";

    StyleT::StyleT() : QMainWindow()
    {
    tempo = new QTimer(this);
    QObject::connect(tempo, SIGNAL(timeout()), this, SLOT(TransitionStyle()));
    tempo->start(2500);
    }

    void StyleT::TransitionStyle ()
    {
    if (CoulorStyle == "#F24C04")
    {
    CoulorStyle = "#00B4F9";
    }

    if (CoulorStyle == "#00B4F9")
    {
        CoulorStyle= "#F24C04";
    }
    

    }
    @
    @
    //Classe A.cpp
    #include <QtGui>
    #include "StyleT.h"
    #include "ClassA.h"
    ClasseA::ClasseA() : QWidget ()
    {
    QLCDNumber *lcd = new QLCDNumber (this);
    lcd->setSegmentStyle(QLCDNumber::Flat);
    lcd->move(500,23);
    lcd->display(75);
    lcd->setStyleSheet("background-color:transparent; color:"+ StyleT::CoulorStyle + "; border: 2px solid white;");
    }
    @
    @
    //main
    #include <QtGui>
    #include "StyleT.h"
    #include "ClassA.h"
    int main(int argc, char *argv[])
    {
    QApplication app(argc, argv);

    StyleT Window;
    Window.show();
    
    return app.exec(&#41;;
    

    }
    @

    1 Reply Last reply
    0
    • D Offline
      D Offline
      dbzhang800
      wrote on last edited by
      #2

      If you want to change the stylesheet every 2.5 seconds, you should call lcd->setStyleSheet() in your slot.

      1 Reply Last reply
      0
      • A Offline
        A Offline
        amonR
        wrote on last edited by
        #3

        Hi 2,
        thanks for your reply but I don't get it. Lcd is in Classe A.cpp, how can I call lcd->setStyleSheet () from my slot which is in StyleT.cpp ?

        1 Reply Last reply
        0
        • JeroentjehomeJ Offline
          JeroentjehomeJ Offline
          Jeroentjehome
          wrote on last edited by
          #4

          Hi,
          Just changing this variable: CoulorStyle will not update your stylesheet. Only in the constructor of your ClassA will it update. So, no this will not work.
          Why do you use two classes for this? Ok, that is your design ;-)
          Why not add a signal that is emitted every time the coulorStyle is changed, connect that to a 'updateGui' slot and set your stylesheet there. I would recommend to use a parameter in your signal/slot connection there in stead of the public static variable. That is like opening a door through which anyone may change the colourStyle.
          Have fun coding!

          Greetz, Jeroen

          1 Reply Last reply
          0
          • IamSumitI Offline
            IamSumitI Offline
            IamSumit
            wrote on last edited by
            #5

            Hii amonR
            You cannot use have an updation of color in constructor because constructor will called at once when ever an object will create. you need a function in class A .

            void ClassA::changeColour()
            {
            lcd->setStyleSheet("background-color:transparent; color:"+ StyleT::CoulorStyle + "; border: 2px solid white;");
            }
            call the above in TransitionStyle()) by making an object of ClassA (say *pObj;)
            void StyleT::TransitionStyle ()
            {
            if (CoulorStyle == "#F24C04")
            {
            CoulorStyle = "#00B4F9";
            pObj->changeColour();
            }

            if (CoulorStyle == "#00B4F9")
            {
                CoulorStyle= "#F24C04";
              pObj->changeColour();
            }
            

            }

            Try It..

            Be Cute

            1 Reply Last reply
            0
            • A Offline
              A Offline
              amonR
              wrote on last edited by
              #6

              Hi Jero and Sumit, thanks for your replies and your ideas. I'm going to try theme.

              1 Reply Last reply
              0
              • A Offline
                A Offline
                amonR
                wrote on last edited by
                #7

                I've finaly used QStateMachine in order to solve my problem. So no anymore need of global variable and slots. Thanks everyone for your help.

                1 Reply Last reply
                0

                • Login

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