[SOLVED]Slot not applied, why?
-
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_OBJECTpublic: 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();
}
@ -
If you want to change the stylesheet every 2.5 seconds, you should call lcd->setStyleSheet() in your slot.
-
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! -
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..