gpt4 book ai didi

c++ - 单击旁边的按钮时如何引用动态创建的标签[Qt]

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:34:40 25 4
gpt4 key购买 nike

每次按下“添加客户端”按钮 - 一个减号按钮、客户端名称、一个客户端计数器和一个加号按钮被添加到 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));
}

最佳答案

Kevin 的回答原则上没问题,但由于我们现在已经有了 2016 年,而且 MSVC2010、gcc 4.7 和 Clang 3.1(可能还有更早的版本)支持 lambdas/C++11,我想添加一个没有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 中被捕获,并直接传递给处理它的函数。

此时 minusCountplusCount 也可以是静态函数,因为它们只对作为参数传递的标签进行操作,所以它们独立于任何 Widget 实例。

关于c++ - 单击旁边的按钮时如何引用动态创建的标签[Qt],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41211878/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com