[Solved]Borderless/Transculent widget updating problem
-
Hello,
im totaly new to Qt and tried to make an "easy" project just to learn and get used to Qt.
I basically want to achieve a simple digital clock directly on the screen.
To make this happen i made a QLabel and used
@
setStyleSheet("background-: transculent; color: red;");
setAttribute(Qt::WA_TranslucentBackground);
setWindowFlags(Qt::FramelessWindowHint | Qt::Tool | Qt::WindowStaysOnTopHint);
resize(200,150);
show();@If i set a text for the label it works just like i want it to.
However im running into a problem when changing the text of the label via a QTimer signal that calls
@setText(QTime::currentTime().toString(QString("hh:mm:ss.zzz")));@What happens is: The label's text gets changed but the old text still remains underneath it.
In other words it override its content instead of repainting it.
To make it easier to understand think of an old typewriter - if u misstyped and go back to the wrong letter and press a new key u just get 2 letters on the same place on the paper (since its not a computer :)).
I dont know what exactly causes this problem or whats the way to solve it so i had a hard time trying to google for the right terms and hope somebody here can help me out.Info:
Qt 5.0.1
OS WinXP
Complete Code:
main.cpp:
@
QApplication a(argc, argv);
ClockLabel c1;
c1.setStyleSheet("background: transculent; color: red;");
c1.setAttribute(Qt::WA_TranslucentBackground);
c1.setWindowFlags(Qt::FramelessWindowHint | Qt::Tool | Qt::WindowStaysOnTopHint);
c1.resize(200,150);
c1.show();return a.exec();@
clocklabel.h:
@
class ClockLabel : public QLabel
{
Q_OBJECT
public:
explicit ClockLabel(QWidget *parent = 0);private:
QSystemTrayIcon *Tray;
QMenu *Traymenu;
QAction *actClose;
QTimer *timer1;signals:
public slots:
void tick();};@
clocklabel.cpp:
@
ClockLabel::ClockLabel(QWidget *parent) :
QLabel(parent)
{
//Traymenu
actClose = new QAction("Close", this);
connect(actClose,SIGNAL(triggered()),qApp,SLOT(quit()));
Traymenu = new QMenu(this);
Traymenu->addAction(actClose);
Traymenu->setStyleSheet("background: white; color: black; selection-background-color: blue; selection-color: white;");
Tray = new QSystemTrayIcon(this);
Tray->setContextMenu(Traymenu);
Tray->show();
//Traymenu endtimer1 = new QTimer(this); connect(timer1,SIGNAL(timeout()),this,SLOT(tick())); timer1->start(100);
}
void ClockLabel::tick()
{
QString a = QTime::currentTime().toString(QString("hh:mm:ss.zzz"));
this->setText(a);
}@P.S.: Feel free to tell me everything else whats wrong with this code, cause as i said those are my first lines ever written with Qt.
-
There is no such css style as "background: transculent" or even without typo ;) There is "background: transparent", but since you're setting WA_TranslucentBackground attribute it will be ignored anyway, because WA_TranslucentBackground sets WA_NoSystemBackground implicitly.
WA_NoSystemBackground in turn means that there is no background so it won't be filled with anything automatically in paintEvent. This is the cause of your "smudges". Since the background is not repainted there are leftovers from last paint.
To fix this you can overload paint event in your ClockLabel class like this:
@
void ClockLabel::paintEvent(QPaintEvent * event)
{
QPainter p(this);//default QPainter mode is CompositionMode_SourceOver //which blends whatever is painted over the existing contents //since we want to paint with transparency we need to change //this to completely replace the background p.setCompositionMode(QPainter::CompositionMode_Source); //rect() means the client area of our widget p.fillRect(rect(), Qt::transparent); //we call the base implementation to let it do the rest for us //(paint the numbers) QLabel::paintEvent(event);
}
@ -
This typo... such a shame!
Im not used to css styles at all and found "translucent" somewhere on the web and never checked on a doc which makes it more of a shame :).Thank you very much for helping me out with this, its exactly what i've been looking for.