[SOLVED]Qpushbutton and clicked slot.
-
Hi, i have a verticalLayout that there's three labels with a button in each line. when i click on each button the event
@
button_clicked()
{}
@
is firing. this event is for all button i.e when i click on each of button this function is called. now i want when i click on each button the same row that button is in that remove. how can i do that? first of all must be a unique value that i detect which button is clicked and second i must delete that row but i know how.
any idea? -
if you don't use threads and have a fixed number of buttons you can use the sender() method:
@
button_clicked()
{
if( sender() == myButton1 )
{
....
}
}
@
But a more elegant way is using "QSignalMapper":http://qt-project.org/doc/qt-4.8/qsignalmapper.html class. -
thanks for your reply. what is myButton1 ? is the button name? but all of my buttons have same name. this is code:
@
QWidget *oneLineWidget = new QWidget(this);QHBoxLayout *oneLineWidgetLayout = new QHBoxLayout(); { //added these brackets just for the ease of reading. QLabel *labFirst = new QLabel(tr("first label"), oneLineWidget); QLabel *labSecond = new QLabel(tr("second label"), oneLineWidget); QPushButton *bFirst = new QPushButton(tr("first"), oneLineWidget); QPushButton *bSecond = new QPushButton(tr("delete"), oneLineWidget); oneLineWidgetLayout->addWidget(labFirst); oneLineWidgetLayout->addWidget(labSecond); oneLineWidgetLayout->addWidget(bFirst); oneLineWidgetLayout->addWidget(bSecond); } oneLineWidget->setLayout(oneLineWidgetLayout); ui->verticalLayout->addWidget(oneLineWidget); } ui->widget->setLayout(ui->verticalLayout); ui->scrollArea->setWidget(ui->widget);
@
when this function executed one line added. -
in this case go with the QSignalMapper, since you had to map the pushbutton object pointers to the row they are in to remove or at least have to do extra work to find the row they are in.
Edit: sender() retruns a pointer. So you need to compare it to your pushbutton pointer.
-
i do that like this:
@
signals:
void click(int);private:
Ui::MainWindow *ui;QSignalMapper *signalMapper;
@
and in .cpp :
@
void MainWindow::click(int x)
{}
@
it's error is:
not a slot or signal declaration.ok i solve this.many thanks of you.
-
your delare click(int) as signal but implement a function body for it (like a slot) ... this can't work.
Here's how you do it:
@
//init signalsmapper (in constrcutor for example)
signalMapper = new QSignalMapper(this);
connect(signalMapper, SIGNAL(mapped(QWidget )), this SLOT(clicked(QWidget));
@@
/*-
yout init as you already have it but with the property
*/
QWidget *oneLineWidget = new QWidget(this);QHBoxLayout *oneLineWidgetLayout = new QHBoxLayout(); { //added these brackets just for the ease of reading. QLabel *labFirst = new QLabel(tr("first label"), oneLineWidget); QLabel *labSecond = new QLabel(tr("second label"), oneLineWidget); QPushButton *bFirst = new QPushButton(tr("first"), oneLineWidget); QPushButton *bSecond = new QPushButton(tr("delete"), oneLineWidget); signalMapper->setMapping ( bSecond, bSecond ); oneLineWidgetLayout->addWidget(labFirst); oneLineWidgetLayout->addWidget(labSecond); oneLineWidgetLayout->addWidget(bFirst); oneLineWidgetLayout->addWidget(bSecond); } oneLineWidget->setLayout(oneLineWidgetLayout); ui->verticalLayout->addWidget(oneLineWidget);
}
ui->widget->setLayout(ui->verticalLayout);
ui->scrollArea->setWidget(ui->widget);
@
@
//finally the clicked-Slot
void clicked(QWidget* button)
{
//actually this method isn't that trivial, since the index of each button changes once a button is removed
for (int i = 0; i < ui->verticalLayout->count(); i++)
{
if (QWidgetItem myItem = dynamic_cast <QWidgetItem>(ui->verticalLayout->itemAt(i)))
{
if( myItem->widget()->findChildren<QPushButton*>().contains(button) )
{
ui->verticalLayout->removeItem(myItem);
myItem->widget()->deleteLayter();
break;
}
}
}}
@THis should give you an idea how to solve it (not activly tested though).
If you have any questions feel free to ask. ;) -