QPushButton with Signals and Slots into a QLienEdit
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 :
Code:
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->setText(Numeros[posicao]);
BtnPin->setMinimumSize(50,50);
BtnPin->setMaximumSize(50,50);
layout->addWidget(BtnPin,i,j);
connect(BtnPin,SIGNAL(clicked()),this,SLOT(AddPinNumero()));
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
Re: QPushButton with Signals and Slots into a QLienEdit
I don't know where something is added, but you are probably looking for QSignalMapper.
Re: QPushButton with Signals and Slots into a QLienEdit
Thanks for your help, I have already tried that but it doesn't work.
But this works :
Code:
QPushButton *BtnClicado
= qobject_cast<QPushButton
*>
(sender
());
Re: QPushButton with Signals and Slots into a QLienEdit
QSignalMapper can be used this way
Code:
int posicao = 0;
for (int i=0; i<3; i++)
{
for (int j=0; j<3; j++)
{
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 &)));
Quote:
But this works :
Code:
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.
Re: QPushButton with Signals and Slots into a QLienEdit
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.