#include <QtGui>
#include "test.h"
: QDialog(parent
), m_rows
(5), m_columns
(2) {
init();
}
void Test::init()
{
for (int r = 0; r < m_rows; ++r) {
for (int c = 0; c < m_columns; ++c) {
pb->setProperty("state", Background);
fp.
fill(QColor(qrand
()%255, qrand
()%255, qrand
()%255
));
fp.save(fin);//must be optimized
bp.
fill(QColor(qrand
()%255, qrand
()%255, qrand
()%255
));
bp.save(bin);//must be optimized
pb->setProperty("backgroundImageName", bin);
pb->setProperty("foregroundImageName", fin);
//pb->setText("Background");
connect(pb, SIGNAL(clicked()), SLOT(changeImage()));
m_gl->addWidget(pb, r, c);
}
}
}
void Test::changeImage()
{
QPushButton *pb
= qobject_cast<QPushButton
*>
(sender
());
if (!pb)
return;
const State state = State(pb->property("state").toInt());
// pb->setText(state == Background ? "Foreground" : "Background");
pb->setProperty("state", state == Background ? Foreground : Background);
const QString fileName
= (state
== Background ? pb
->property
("foregroundImageName").
toString() : pb
->property
("backgroundImageName").
toString());
pb
->setIcon
(QIcon(fileName
));
}
#include <QtGui>
#include "test.h"
Test::Test(QWidget *parent)
: QDialog(parent), m_rows(5), m_columns(2)
{
m_gl = new QGridLayout(this);
init();
}
void Test::init()
{
for (int r = 0; r < m_rows; ++r) {
for (int c = 0; c < m_columns; ++c) {
QPushButton *pb = new QPushButton();
pb->setProperty("state", Background);
const QString bin = QString::number(r) + QString::number(c) + "b.png";
const QString fin = QString::number(r) + QString::number(c) + "f.png";
QPixmap fp(10, 10);
fp.fill(QColor(qrand()%255, qrand()%255, qrand()%255));
fp.save(fin);//must be optimized
QPixmap bp(10, 10);
bp.fill(QColor(qrand()%255, qrand()%255, qrand()%255));
bp.save(bin);//must be optimized
pb->setProperty("backgroundImageName", bin);
pb->setProperty("foregroundImageName", fin);
//pb->setText("Background");
pb->setIcon(QIcon(bp));
connect(pb, SIGNAL(clicked()), SLOT(changeImage()));
m_gl->addWidget(pb, r, c);
}
}
}
void Test::changeImage()
{
QPushButton *pb = qobject_cast<QPushButton *>(sender());
if (!pb)
return;
const State state = State(pb->property("state").toInt());
// pb->setText(state == Background ? "Foreground" : "Background");
pb->setProperty("state", state == Background ? Foreground : Background);
const QString fileName = (state == Background ? pb->property("foregroundImageName").toString() : pb->property("backgroundImageName").toString());
pb->setIcon(QIcon(fileName));
}
To copy to clipboard, switch view to plain text mode
Bookmarks