每次按下“添加客户端”按钮 - 减号按钮,客户端名称,客户端计数器和加号按钮都以水平线添加到gridLayout。我想在按下加号/减号按钮时更新计数(QLabel)。
这是我的代码:
#include "widget.h"
#include "ui_widget.h"
#include <QDebug>
#include <QGridLayout>
#include <QLabel>
int row = 0;
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
}
Widget::~Widget()
{
delete ui;
}
void Widget::on_pushButton_clicked()
{
QPushButton* minus_button = new QPushButton();
QPushButton* plus_button = new QPushButton();
minus_button->setText("-");
plus_button->setText("+");
QLabel* client_name = new QLabel(ui->lineEdit->text(), this);
QLabel* client_count = new QLabel("", this);
ui->gridLayout->addWidget(minus_button, row, 1, Qt::AlignTop|Qt::AlignHCenter);
ui->gridLayout->addWidget(client_name, row, 2, Qt::AlignTop|Qt::AlignHCenter);
ui->gridLayout->addWidget(client_count, row, 3, Qt::AlignTop|Qt::AlignHCenter);
ui->gridLayout->addWidget(plus_button, row++, 4, Qt::AlignTop|Qt::AlignHCenter);
connect(minus_button, SIGNAL(clicked(bool)), this, SLOT(minusCount()));
connect(plus_button, SIGNAL(clicked(bool)), this, SLOT(plusCount()));
}
void Widget::minusCount(){
//Here I want to somehow reference the client_count label which is in the same row
}
void Widget::plusCount(){
//Here I want to somehow reference the client_count label which is in the same row
}
编辑:在Kevin的评论之后,这里是任何可能需要它的人的工作代码。
#include "widget.h"
#include "ui_widget.h"
#include <QDebug>
#include <QGridLayout>
#include <QLabel>
#include <QSignalMapper>
QSignalMapper* m_minusMapper;
QSignalMapper* m_plusMapper;
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
m_minusMapper = new QSignalMapper(this);
connect(m_minusMapper, SIGNAL(mapped(QWidget*)), this, SLOT(minusCount(QWidget*)));
m_plusMapper = new QSignalMapper(this);
connect(m_plusMapper, SIGNAL(mapped(QWidget*)), this, SLOT(plusCount(QWidget*)));
}
Widget::~Widget()
{
delete ui;
}
void Widget::on_pushButton_clicked()
{
QPushButton* minus_button = new QPushButton();
minus_button->setObjectName("minus" + QString::number(row));
minus_button->setText("-");
QPushButton* plus_button = new QPushButton();
plus_button->setObjectName("plus" + QString::number(row));
plus_button->setText("+");
QLabel* client_name = new QLabel(ui->lineEdit->text(), this);
QLabel* client_count = new QLabel("0", this);
ui->gridLayout->addWidget(minus_button, row, 1, Qt::AlignVCenter|Qt::AlignHCenter);
ui->gridLayout->addWidget(client_name, row, 2, Qt::AlignVCenter|Qt::AlignHCenter);
ui->gridLayout->addWidget(client_count, row, 3, Qt::AlignVCenter|Qt::AlignHCenter);
ui->gridLayout->addWidget(plus_button, row++, 4, Qt::AlignVCenter|Qt::AlignHCenter);
connect(minus_button, SIGNAL(clicked()), m_minusMapper, SLOT(map()));
connect(plus_button, SIGNAL(clicked()), m_plusMapper, SLOT(map()));
m_minusMapper->setMapping(minus_button, client_count);
m_plusMapper->setMapping(plus_button, client_count);
}
void Widget::minusCount(QWidget *widget)
{
QLabel *client_count = qobject_cast<QLabel*>(widget);
QString initCount = client_count->text();
int newCount = initCount.toInt() - 1;
client_count->setText(QString::number(newCount));
}
void Widget::plusCount(QWidget *widget)
{
QLabel *client_count = qobject_cast<QLabel*>(widget);
QString initCount = client_count->text();
int newCount = initCount.toInt() + 1;
client_count->setText(QString::number(newCount));
}
答案 0 :(得分:4)
凯文原则上的答案很好,但是既然我们现在已经2016年了,MSVC2010,gcc 4.7和Clang 3.1(以及可能的早期版本)支持lambdas / C ++ 11,我&#39 ; d想添加一个没有QSignalMapper的版本:
static void minusCount(QLabel *label)
{
QString initCount = label->text();
int newCount = initCount.toInt() - 1;
label->setText(QString::number(newCount));
}
static void plusCount(QLabel *label)
{
QString initCount = label->text();
int newCount = initCount.toInt() + 1;
label->setText(QString::number(newCount));
}
void Widget::on_pushButton_clicked()
{
// ... add the labels and buttons, then:
connect(minus_button, &QPushButton::clicked,
client_count, [client_count]() { minusCount(client_count); });
connect(plus_button, &QPushButton::clicked,
client_count, [client_count]() { plusCount(client_count); });
}
此处按钮单击应更改的client_count
标签将在lambda中捕获,并直接传递给处理它的函数。
minusCount
和plusCount
也可以是静态函数,因为它们只对作为参数传递的标签进行操作,因此它们独立于任何Widget
实例。 / p>
答案 1 :(得分:2)
最简单的方法是使用QSignalMapper
// in the constructor of your class
m_minusSignalMapper = new QSignalMapper(this);
connect(m_minusSignalMapper, SIGNAL(mapped(QWidget*)), this, SLOT(minusCount(QWidget*)));
m_plusSignalMapper = new QSignalMapper(this);
connect(m_plusSignalMapper, SIGNAL(mapped(QWidget*)), this, SLOT(plusCount(QWidget*)));
// in your button slot you connect to the respective mapper instead
connect(minus_button, SIGNAL(clicked()), m_minusMapper, SLOT(map()));
connect(plus_button, SIGNAL(clicked()), m_plusMapper, SLOT(map()));
// and you set up the mapping
m_minusMapper->setMapping(minus_button, client_count);
m_plusMapper->setMapping(plus_button, client_count);
// in the the two receiver slots, the argument is the label
void Widget::minusCount(QWidget *widget)
{
QLabel *client_count = qobject_cast<QLabel*>(widget);
}