take a look at this example (it can be optimazed, cashing imagez ect.), it's just for demonstration a buttons approach
test.h
#ifndef TEST_H
#define TEST_H
#include <QDialog>
{
Q_OBJECT
public:
private:
void init();
private slots:
void changeImage();
private:
enum State { Background, Foreground };
int m_rows;
int m_columns;
};
#endif//TEST_H
#ifndef TEST_H
#define TEST_H
#include <QDialog>
class Test: public QDialog
{
Q_OBJECT
public:
Test(QWidget *parent = 0);
private:
void init();
private slots:
void changeImage();
private:
enum State { Background, Foreground };
int m_rows;
int m_columns;
class QGridLayout *m_gl;
};
#endif//TEST_H
To copy to clipboard, switch view to plain text mode
test.cpp
#include <QtGui>
#include "test.h"
: QDialog(parent
), m_rows
(5), m_columns
(5) {
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(5)
{
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
main.cpp
#include <QApplication>
#include "test.h"
int main(int argc, char **argv)
{
Test test;
test.show();
return app.exec();
}
#include <QApplication>
#include "test.h"
int main(int argc, char **argv)
{
QApplication app(argc, argv);
Test test;
test.show();
return app.exec();
}
To copy to clipboard, switch view to plain text mode
Bookmarks