QSignalMapper is also worth a look for these multiple-button to one slot jobs.
However, it seems to me that the buttons should each be a custom class instance that tracks its own internal state and renders itself in the appropriate colour for its current state: no containing widget logic required. You might provide a signal to indicate that the state had changed to external observers, and an access function to retrieve (or set if needed) the state from outside. Here's the sort of thing I had in mind:
// fancybutton.h
#ifndef FANCYBUTTON_H
#define FANCYBUTTON_H
#include <QPushButton>
#include <QVector>
#include <QString>
{
Q_OBJECT
public:
FancyButton
(QWidget *parent
= nullptr
);
~FancyButton();
private:
void bumpState(bool checked = false);
int m_state;
static QVector<QString> m_styles;
};
#endif // FANCYBUTTON_H
// fancybutton.h
#ifndef FANCYBUTTON_H
#define FANCYBUTTON_H
#include <QPushButton>
#include <QVector>
#include <QString>
class FancyButton : public QPushButton
{
Q_OBJECT
public:
FancyButton(QWidget *parent = nullptr);
~FancyButton();
private:
void bumpState(bool checked = false);
int m_state;
static QVector<QString> m_styles;
};
#endif // FANCYBUTTON_H
To copy to clipboard, switch view to plain text mode
// fancybutton.cpp
#include "fancybutton.h"
QVector<QString> FancyButton::m_styles =
QVector<QString>(
{
QStringLiteral("QPushButton { background-color: grey; } "
"QPushButton:enabled { background-color: rgb(200,0,0); }"),
QStringLiteral("QPushButton { background-color: grey; } "
"QPushButton:enabled { background-color: rgb(0,200,0); }"),
QStringLiteral("QPushButton { background-color: grey; } "
"QPushButton:enabled { background-color: rgb(0,0,200); }"),
});
FancyButton
::FancyButton(QWidget *parent
) , m_state(0)
{
setStyleSheet(m_styles.at(m_state));
connect(this, &FancyButton::clicked, this, &FancyButton::bumpState);
}
FancyButton::~FancyButton()
{
}
void FancyButton::bumpState(bool /*checked*/)
{
m_state = (m_state + 1) % m_styles.count();
setStyleSheet(m_styles.at(m_state));
}
// fancybutton.cpp
#include "fancybutton.h"
QVector<QString> FancyButton::m_styles =
QVector<QString>(
{
QStringLiteral("QPushButton { background-color: grey; } "
"QPushButton:enabled { background-color: rgb(200,0,0); }"),
QStringLiteral("QPushButton { background-color: grey; } "
"QPushButton:enabled { background-color: rgb(0,200,0); }"),
QStringLiteral("QPushButton { background-color: grey; } "
"QPushButton:enabled { background-color: rgb(0,0,200); }"),
});
FancyButton::FancyButton(QWidget *parent)
: QPushButton(parent)
, m_state(0)
{
setStyleSheet(m_styles.at(m_state));
connect(this, &FancyButton::clicked, this, &FancyButton::bumpState);
}
FancyButton::~FancyButton()
{
}
void FancyButton::bumpState(bool /*checked*/)
{
m_state = (m_state + 1) % m_styles.count();
setStyleSheet(m_styles.at(m_state));
}
To copy to clipboard, switch view to plain text mode
// main.cpp
#include "fancybutton.h"
#include <QApplication>
#include <QWidget>
#include <QGridLayout>
int main(int argc, char *argv[])
{
for (int r = 0; r < 4; ++r) {
for (int c = 0; c < 4; ++c) {
layout->addWidget(new FancyButton(), r, c);
}
}
w.show();
return a.exec();
}
// main.cpp
#include "fancybutton.h"
#include <QApplication>
#include <QWidget>
#include <QGridLayout>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QWidget w;
QGridLayout *layout = new QGridLayout(&w);
for (int r = 0; r < 4; ++r) {
for (int c = 0; c < 4; ++c) {
layout->addWidget(new FancyButton(), r, c);
}
}
w.show();
return a.exec();
}
To copy to clipboard, switch view to plain text mode
Bookmarks