Lorenzo Manoni
Lorenzo Manoni

Reputation: 27

How to clear a label in qt creator

this is the first time i write in this site, I'm trying approach me at Qt-creator but I've a problem: I want to delete the text of the label when the user click a button, i've tried some solution but without success

this is the code:

struct finestra{
float costo;
int altezza;
int larghezza;
QString text;
QString costoStr;
};

float Totale=0;
finestra vet[21];
int i=1;

//SOME CODE

 Totale+=vet[i].costo;
 vet[i].costoStr = QString::number(vet[i].costo);
 vet[i].text = vet[i-1].text + "Finestra ad un anta bianca <br>" + "€" + vet[i].costoStr +"<br>";
 ui->TotaleFinestre->setText(QString(vet[i].text));
 i++;

I've tried with this function:

void preventivi::on_pushButton_clicked()
{
   ui->TotaleFinestre->clear();
}

if someone know how to do please answer,

thanks to all and sorry for my bad english.

Upvotes: 1

Views: 7810

Answers (2)

kzsnyk
kzsnyk

Reputation: 2211

As QLabel define the slot void QLabel::clear(), you can also just connect this slot with the clicked() signal that will be emitted after a click on your pushButton, using the QObject::connect method :

QObject::connect(pointer_to_your_pushButton, SIGNAL(clicked()), pointer_to_your_label, SLOT(clear()));

EDIT : Here is a small example

The UI is a QWidget that has a QLabel and a QPushButton. I did that with Qt Designer but it doesn't matter here.

#include "widget.h"
#include "ui_widget.h"

Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    ui->setupUi(this);
    QObject::connect(ui->pushButton, SIGNAL(clicked()), ui->label, SLOT(clear()));
}

Widget::~Widget()
{
    delete ui;
}

You can even do that using "Edit Signals/Slots" inside Qt Designer and make the signal/slot connection between your widgets. ( you won't need to manually call the previous QObject::connect, as it will be done automatically inside the Ui_Widget class, generated by the uic)

enter image description here

Or you can do all without Qt Designer, it's up to you. Hope this helps.

Upvotes: 1

Lynxy
Lynxy

Reputation: 25

Maybe you should try

void preventivi::on_pushButton_clicked()
{
    ui->TotaleFinestre->setText("");
}

Upvotes: 2

Related Questions