Here's my code:
mywindow.h (ui_form.h was generated using Qt Designer, it's huge so I won't put it here)
#ifndef MYWINDOW_H
#define MYWINDOW_H
#include <QMainWindow>
#include <QColor>
#include "ui_form.h"
{
Q_OBJECT
public:
MyWindow1(const Ui::Form& ui);
~MyWindow1() {}
void setTimer(int ms);
public slots:
void blink() ;
private:
Ui::Form mUi;
};
#endif
#ifndef MYWINDOW_H
#define MYWINDOW_H
#include <QMainWindow>
#include <QColor>
#include "ui_form.h"
class MyWindow1 : public QMainWindow
{
Q_OBJECT
public:
MyWindow1(const Ui::Form& ui);
~MyWindow1() {}
void setTimer(int ms);
public slots:
void blink() ;
private:
Ui::Form mUi;
QColor mColor;
};
#endif
To copy to clipboard, switch view to plain text mode
mywindow.cpp
#include <QPalette>
#include <QIcon>
#include <QtCore/QTimer>
#include "mywindow.h"
MyWindow1
::MyWindow1(const Ui
::Form& ui
) : QMainWindow(){
mUi = ui;
mUi.setupUi(this);
mColor = Qt::red;
}
void MyWindow1::setTimer(int ms)
{
if (!mUi.pushButton_114)
printf("setTimer: invalid button\n");
connect(timer, SIGNAL(timeout()), this, SLOT(blink()));
timer->start(ms);
}
void MyWindow1::blink()
{
if (!mUi.pushButton_114)
{
printf("Invalid button\n");
return;
}
if (mColor == Qt::green)
mColor = Qt::red;
else
mColor = Qt::green;
// DOES NOT WORK
QPalette p
= mUi.
pushButton_114->palette
();
mUi.pushButton_114->setPalette(p);
mUi.pushButton_114->setAutoFillBackground(true);
#if 0
// THIS WORKS
QString style
= mUi.
pushButton_114->styleSheet
();
if (style.contains("green"))
{
mUi.pushButton_114->setStyleSheet(
QString::fromUtf8("background-color: red;"));
}
else
{
mUi.pushButton_114->setStyleSheet(
QString::fromUtf8("background-color: green;"));
}
#endif
}
#include <QPalette>
#include <QIcon>
#include <QtCore/QTimer>
#include "mywindow.h"
MyWindow1::MyWindow1(const Ui::Form& ui) : QMainWindow()
{
mUi = ui;
mUi.setupUi(this);
mColor = Qt::red;
}
void MyWindow1::setTimer(int ms)
{
if (!mUi.pushButton_114)
printf("setTimer: invalid button\n");
QTimer *timer = new QTimer(this);
connect(timer, SIGNAL(timeout()), this, SLOT(blink()));
timer->start(ms);
}
void MyWindow1::blink()
{
if (!mUi.pushButton_114)
{
printf("Invalid button\n");
return;
}
if (mColor == Qt::green)
mColor = Qt::red;
else
mColor = Qt::green;
// DOES NOT WORK
QPalette p = mUi.pushButton_114->palette();
p.setColor(QPalette::Button, mColor);
mUi.pushButton_114->setPalette(p);
mUi.pushButton_114->setAutoFillBackground(true);
#if 0
// THIS WORKS
QString style = mUi.pushButton_114->styleSheet();
if (style.contains("green"))
{
mUi.pushButton_114->setStyleSheet(
QString::fromUtf8("background-color: red;"));
}
else
{
mUi.pushButton_114->setStyleSheet(
QString::fromUtf8("background-color: green;"));
}
#endif
}
To copy to clipboard, switch view to plain text mode
main.cpp
#include <QApplication>
#include "mywindow.h"
int main(int argc, char *argv[])
{
Ui::Form ui;
MyWindow1 *widget = new MyWindow1(ui);
widget->setTimer(500);
widget->show();
return app.exec();
}
#include <QApplication>
#include "mywindow.h"
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
Ui::Form ui;
MyWindow1 *widget = new MyWindow1(ui);
widget->setTimer(500);
widget->show();
return app.exec();
}
To copy to clipboard, switch view to plain text mode
Bookmarks