View Full Version : A lot of checkeable images
IRON_MAN
17th June 2009, 08:43
Hello,
I'm creating a window with a lot of images but although I know how to add one I have problems adding the rest of them.
I have done a class to insert the first image.
Do I have to another class in the same. h and .cpp files?Itake many errors if I do like this! this is the class that I'm doing:
changingbutton::changingbutton(QWidget *parent)
: QPushButton(parent)
spirit
17th June 2009, 08:50
hm, why do you subclass QPushButton?
the fastes way it's to put a layout on your widget (for exmample QGridLayout) and then in a loop add a label (QLabel) with image into layout.
another way, it's to create you own widget which will load images and draw it in its paintEvent.
IRON_MAN
17th June 2009, 09:38
I don't use a label because the image must behave as a button. When I push it, I have to see one picture and when i do not, another one.
At first I tried doing with a label, but I didn't get my goal.
Could you tell me how can i do the same with a label?
spirit
17th June 2009, 09:44
the first option is to subclass QLabel and add signal clicked.
the second otpion is to install event filter for a label and process QMouseEvent.
but if you want to use buttons then I don't understand why you sublass QPushButton if you can use clicked signal for changing images.
spirit
17th June 2009, 10:41
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>
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
test.cpp
#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));
}
main.cpp
#include <QApplication>
#include "test.h"
int main(int argc, char **argv)
{
QApplication app(argc, argv);
Test test;
test.show();
return app.exec();
}
IRON_MAN
22nd June 2009, 15:25
The code that I'm using to disabled a button is the following one:
QPushButton *xxx= new QPushButton;
xxx->setDisabled(true);
xxx->setIcon(QIcon("IMAGE1.bmp"));
xxx->setText("disabled");
How can I disabled a button?If I do like this i don't lock the function of the button.
IRON_MAN
22nd June 2009, 15:26
The code that I'm using to disabled a button is the following one:
QPushButton *xxx= new QPushButton;
xxx->setDisabled(true);
xxx->setIcon(QIcon("IMAGE1.bmp"));
xxx->setText("disabled");
How can I disabled a button?If I do like this i don't lock the function of the button.
IRON_MAN
1st July 2009, 16:48
To disabled a button I only need to put this sentence:
setDisabled(true).
I have asked a stupid question!
know I have another problem. My images are .bmp. I have loaded them to my new proyect giving a new name as you did "00b.qrc"... and then, in the place that you set in note of test.cpp:
const QString bin = QString::number(r) + QString::number(c) + "b.png";
const QString fin = QString::number(r) + QString::number(c) + "f.png";
I change for :
const QString bin = QString::number(r) + QString::number(c) + "b.bmp";
const QString fin = QString::number(r) + QString::number(c) + "f.bmp";
What I'm doing wrong?
Powered by vBulletin® Version 4.2.5 Copyright © 2024 vBulletin Solutions Inc. All rights reserved.