PDA

View Full Version : connect a QPushButton matrix with a function



harmodrew
6th August 2010, 01:06
Hello, here I am again! :D

I'm trying to connect the clicked() events of the buttons of a QPushButton matrix with a custom function. The problem is that the function needs the indexes of the clicked button...in the connect() reference is told that I cannot do such a thing:


QPushButton **p;
p = new QPushButton*[nrow];
for (int i=0; i<nrow; i++)
p[i] = new QPushButton[ncol];

for (int i=0; i<nrow; i++)
for (int j=0; j<ncol; j++)
p[i][j].connect(&p[i][j],SIGNAL(clicked()), actObj, SLOT(triggerFunc(int i, int j)));


actObj is an object which holds the slot triggerFunc(int, int).
I need to index the button of p that was clicked and call the triggerFunc() function using i and j properly...are there other ways to connect a widget, object, button, etc, with a function?

thanks again :)

Zlatomir
6th August 2010, 01:08
Look like signal mapper (http://doc.qt.nokia.com/4.6/qsignalmapper.html#details) is what you need

harmodrew
6th August 2010, 01:16
yeah, I thiks it will help! I'll try it tomorrow...good night and thanks a lot! :)

aamer4yu
6th August 2010, 05:32
Also do remember the number of arguments of SLOT must be equal to or less than the arguments of the SIGNAL

harmodrew
6th August 2010, 09:39
but how do I assign an argument of the SLOT to an argument of the SIGNAL? The order must be the same?

harmodrew
6th August 2010, 11:06
I'm having troubles with the code here:

inputmatrix.h:


#ifndef INPUTMATRIX_H
#define INPUTMATRIX_H
#include <QPushButton>
#include <QObject>
#include <QSignalMapper>

class InputMatrix : QObject
{
Q_OBJECT
private:
QPushButton ** p;
int nrow;
int ncol;
QSignalMapper *signalMapper;
public:
InputMatrix();
InputMatrix(int nrow, int ncol);
~InputMatrix();
void Show(QWidget*);
QPushButton GetItem(int i,int j);
public slots:
void SetItem(const QString &ID);
};

#endif // INPUTMATRIX_H



inputmatrix.cpp:


#include "inputmatrix.h"
#include <QPushButton>
#include "mainwindow.h"
#include <QSignalMapper>
#include <QString>
#include <QMessageBox>
#include <QChar>

InputMatrix::InputMatrix()
{}

InputMatrix::InputMatrix(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 InputMatrix::Show(QWidget *obj)
{
int alt = (obj->height()-100)/nrow;

signalMapper= new QSignalMapper(this);

const QString ID = "ij"; // this will identify the buttons in the PushButton matrix
for (int i=0; i<nrow; i++)
{
ID.insert(0,(Qchar) i); // FIRST ERROR
for (int j=0; j<ncol; j++)
{
ID.insert(1,(QChar) j); // SECOND ERROR
p[i][j].setParent(obj); // obj = window on which I'll show the matrix
p[i][j].setGeometry(50+(j*alt), 70 +(i*alt),alt, alt);
connect(&p[i][j], SIGNAL(clicked()), signalMapper, SLOT(map()));
signalMapper->setMapping(&p[i][j], ID);
p[i][j].show();
}
}
connect(signalMapper, SIGNAL(mapped(const QString &)),
this, SLOT(SetItem(const QString &)));

}

void InputMatrix::SetItem(const QString & ID)
{

QMessageBox msg;
QString aux = "You have pressed ";
aux.append(ID);
msg.setText(aux);
msg.exec();

}

QPushButton InputMatrix::GetItem(int i,int j)
{
//return p[i][j];

}

InputMatrix::~InputMatrix()
{
for (int i=0; i<nrow; i++)
delete [] p[i];
delete p;

}


the error is the same at both of the rows I commented in the code. It is:

passing 'const QString' as 'this' argument of 'QString& QString::insert(int, QChar)' discards qualifier

why I'm wrong?

harmodrew
6th August 2010, 11:11
solved :) simply I declared a const ID QString so the compiler was warning me that I was changing a const string :)

but now I have a "undefined reference to 'vtable for InputMatrix' " error on the constructor and destructor prototype lines of code...:(

SOLVED: I only had to run "qmake"