[SOLVED] Label display of an Int
-
Im trying to get a label to display a random value stored on a variable generated on a button click, but the label displays nothing. Sorry for the poor explanation hope you understand.
mainwindow.h
@
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
QT_BEGIN_NAMESPACE
class QLabel;
QT_END_NAMESPACEnamespace Ui {
class MainWindow;
}class MainWindow : public QMainWindow
{
Q_OBJECTpublic:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();private slots:
void on_d2button_clicked();private:
Ui::MainWindow *ui;
QLabel *diceout;
};#endif // MAINWINDOW_H
@mainwindow.cpp
@
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QString>MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
diceout = new QLabel;
}MainWindow::~MainWindow()
{delete ui;
}
void MainWindow::on_d2button_clicked()
{
diceout = new QLabel;
int gervalue = rand() % 2 + 1;
diceout->setNum(gervalue);
}@
-
Sorry it's my fault, now post is updated, still not working...
It was my mistake when copied and pasted here.[quote author="Gute" date="1300074596"]MainWindow::on_d2button_clicked() is not declared in the slot section i think, on_pushButton_4_clicked() != on_d2button_clicked(), may be you change the button's name[/quote]
-
Hi,
you should create the label as child and add it to the layout,or even better, add it to the ui created bny the GUI editor.
BUT: never create a widget in all places where you want to access it, it's enough to create it once.
If diceout is part of the ui, it's the easies way, then it would be:
@
void MainWindow::on_d2button_clicked()
{
int gervalue = rand() % 2 + 1;
ui->diceout->setNum(gervalue);
}
@