#ifndef BUTTONMATRIX_H
#define BUTTONMATRIX_H
#include <QPushButton>
#include <QObject>
#include <QSignalMapper>
{
Q_OBJECT
private:
int nrow;
int ncol;
public:
ButtonMatrix(int nrow, int ncol);
~ButtonMatrix();
public slots:
void SetItem
(const QString &ID
);
// this slot is called by the signal clicked() of the button identified by ID };
#endif // BUTTONMATRIX_H
#ifndef BUTTONMATRIX_H
#define BUTTONMATRIX_H
#include <QPushButton>
#include <QObject>
#include <QSignalMapper>
class ButtonMatrix : QObject
{
Q_OBJECT
private:
QPushButton ** p;
int nrow;
int ncol;
QSignalMapper *signalMapper;
public:
ButtonMatrix(int nrow, int ncol);
~ButtonMatrix();
void Show(QWidget*);
public slots:
void SetItem(const QString &ID); // this slot is called by the signal clicked() of the button identified by ID
};
#endif // BUTTONMATRIX_H
To copy to clipboard, switch view to plain text mode
#include "buttonmatrix.h"
#include <QPushButton>
#include "mainwindow.h"
#include <QSignalMapper>
#include <QString>
#include <QChar>
#include "stdio.h"
#include "mymacros.h"
#include "string.h"
ButtonMatrix::ButtonMatrix(int nr, int nc)
{
ncol=nc;
nrow = nr;
for (int i=0; i<nrow; i++)
}
void ButtonMatrix
::Show(QWidget *obj
) {
int alt = (obj->height()-100)/nrow;
QString ID;
// the format of the ID string will be "iijj" where i = index of rows, j = index of columns char aux1[3], aux2[3], aux3[5]; //used to convert into strings and then append the i&j indexes
for (int i=0; i<nrow; i++)
{
if (i>=10)
sprintf(aux1,"%d", i);
else
sprintf(aux1,"0%d", i); // this ensures that the format of the first part of ID will be "ii"
for (int j=0; j<ncol; j++)
{
if (j>=10)
sprintf(aux2,"%d", j);
else
sprintf(aux2,"0%d", j); // this ensures that the format of the second part of ID will be "jj"
p[i][j].setParent(obj);
p[i][j].setGeometry(50+(j*alt), 70 +(i*alt),alt, alt);
connect(&p[i][j], SIGNAL(clicked()), signalMapper, SLOT(map())); //connect the clicked() slot of the ij-th button with the map() slot of the signal mapper...
sprintf(aux3, "%s%s", aux1, aux2);
ID = tr(aux3); // (creating the ID string in "iijj" format)
signalMapper->setMapping(&p[i][j], ID); //...and then associate ID to the ij-th button of the matrix
p[i][j].show();
}
}
connect(signalMapper,
SIGNAL(mapped
(const QString &)),
this,
SLOT(SetItem
(const QString &)));
// when a button of the matrix gets clicked the SetItem slot of the ButtonMatrix will be called }
void ButtonMatrix
::SetItem(const QString & ID
) // HERE I'd like a few more arguments for SetItem to work properly. {
int i, j;
const QChar *aux
= ID.
data();
i = aux[0].digitValue()*10 + aux[1].digitValue();
j = aux[2].digitValue()*10 + aux[3].digitValue();
// here I need an integer and a custom object coming from the main
//but the SLOT cannot gain more parameters than one
}
ButtonMatrix::~ButtonMatrix()
{
for (int i=0; i<nrow; i++)
delete [] p[i];
delete p;
}
#include "buttonmatrix.h"
#include <QPushButton>
#include "mainwindow.h"
#include <QSignalMapper>
#include <QString>
#include <QChar>
#include "stdio.h"
#include "mymacros.h"
#include "string.h"
ButtonMatrix::ButtonMatrix(int nr, int nc)
{
ncol=nc;
nrow = nr;
p = new QPushButton*[nrow];
for (int i=0; i<nrow; i++)
p[i] = new QPushButton[ncol];
}
void ButtonMatrix::Show(QWidget *obj)
{
int alt = (obj->height()-100)/nrow;
signalMapper= new QSignalMapper(this);
QString ID; // the format of the ID string will be "iijj" where i = index of rows, j = index of columns
char aux1[3], aux2[3], aux3[5]; //used to convert into strings and then append the i&j indexes
for (int i=0; i<nrow; i++)
{
if (i>=10)
sprintf(aux1,"%d", i);
else
sprintf(aux1,"0%d", i); // this ensures that the format of the first part of ID will be "ii"
for (int j=0; j<ncol; j++)
{
if (j>=10)
sprintf(aux2,"%d", j);
else
sprintf(aux2,"0%d", j); // this ensures that the format of the second part of ID will be "jj"
p[i][j].setParent(obj);
p[i][j].setGeometry(50+(j*alt), 70 +(i*alt),alt, alt);
connect(&p[i][j], SIGNAL(clicked()), signalMapper, SLOT(map())); //connect the clicked() slot of the ij-th button with the map() slot of the signal mapper...
sprintf(aux3, "%s%s", aux1, aux2);
ID = tr(aux3); // (creating the ID string in "iijj" format)
signalMapper->setMapping(&p[i][j], ID); //...and then associate ID to the ij-th button of the matrix
p[i][j].show();
}
}
connect(signalMapper, SIGNAL(mapped(const QString &)),
this, SLOT(SetItem(const QString &))); // when a button of the matrix gets clicked the SetItem slot of the ButtonMatrix will be called
}
void ButtonMatrix::SetItem(const QString & ID) // HERE I'd like a few more arguments for SetItem to work properly.
{
int i, j;
const QChar *aux = ID.data();
i = aux[0].digitValue()*10 + aux[1].digitValue();
j = aux[2].digitValue()*10 + aux[3].digitValue();
// here I need an integer and a custom object coming from the main
//but the SLOT cannot gain more parameters than one
}
ButtonMatrix::~ButtonMatrix()
{
for (int i=0; i<nrow; i++)
delete [] p[i];
delete p;
}
To copy to clipboard, switch view to plain text mode
Bookmarks