PDA

View Full Version : QPushButton with Signals and Slots into a QLienEdit



aliasbody
7th June 2011, 10:42
Hi everyone, (Sorry for my bad english)

I am trying to create a Pin Code for my program, 3 numbers in the horizontal and 3 numbers in the vertical. Then when the user click in the QPushButton with the number, the text of the QPushButton is send to the QLineEdit....

I have already done that :



QPushButton *BtnPin;

char *Numeros[9] = {"1","2","3","4","5","6","7","8","9"};
int posicao = 0;

for (int i=0; i<3; i++)
{
for (int j=0; j<3; j++)
{
BtnPin = new QPushButton(Numeros[posicao],this);
BtnPin->setText(Numeros[posicao]);
BtnPin->setMinimumSize(50,50);
BtnPin->setMaximumSize(50,50);
layout->addWidget(BtnPin,i,j);
connect(BtnPin,SIGNAL(clicked()),this,SLOT(AddPinN umero()));
posicao++;
}
}

ui->frame->setLayout(layout);

All the buttons appears but if I click, it only returns the last number added (9), how can I solve the problem ?

Thanks in advance,
Luis Da Costa

Lykurg
7th June 2011, 10:52
I don't know where something is added, but you are probably looking for QSignalMapper.

aliasbody
7th June 2011, 11:46
Thanks for your help, I have already tried that but it doesn't work.

But this works :


QPushButton *BtnClicado = qobject_cast<QPushButton *>(sender());

Santosh Reddy
8th June 2011, 00:15
QSignalMapper can be used this way


QPushButton *BtnPin;
QSignalMapper* signalMapper = new QSignalMapper(this);

QStringList Numeros = QStringList()<<"1"<<"2"<<"3"<<"4"<<"5"<<"6"<<"7"<<"8"<<"9";
int posicao = 0;

for (int i=0; i<3; i++)
{
for (int j=0; j<3; j++)
{
BtnPin = new QPushButton(Numeros[posicao],this);
BtnPin->setText(Numeros[posicao]);
BtnPin->setMinimumSize(50,50);
BtnPin->setMaximumSize(50,50);
layout->addWidget(BtnPin,i,j);
connect(BtnPin, SIGNAL(clicked()), signalMapper, SLOT(map()));
signalMapper->setMapping(BtnPin, Numeros[posicao]);
posicao++;
}
}

connect(signalMapper, SIGNAL(mapped(const QString &)),
this, SLOT(AddPinNumero(const QString &)));



But this works :


QPushButton *BtnClicado = qobject_cast<QPushButton *>(sender());


This might have worked, you really need to understand why this worked, weather you want to use it this way, else will run to problems later on.

DanH
8th June 2011, 04:18
You've only got one slot for all nine buttons -- how do you tell which button was pressed?

Note that you could in theory use QObject.sender() to get the QPushButton pointer (you'd have to cast the value to a QPushButton*) and then query QPushButton.text() to retrieve the "value" of the button.