[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);
}@
-
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
-
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]
-
Did you create two diceout objects? One on line 10 and another one in line 21. Is it correct?
-
Also, the way this is coded, the QLabel(s) called diceout are not parented. This means they will want to pop up as separate top-level widgets. However, diceout->show(); is never called, so they remain invisible.
-
Either way, as Delay pointed out, you have two places where diceout is being created. If you want just one label, the call to new at line 21 prevents it. If you want multiple labels, the one at line 10 is redundant.
-
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);
}
@