Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [SOLVED] Label display of an Int

[SOLVED] Label display of an Int

Scheduled Pinned Locked Moved General and Desktop
7 Posts 5 Posters 6.7k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    Primordium
    wrote on last edited by
    #1

    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_NAMESPACE

    namespace Ui {
    class MainWindow;
    }

    class MainWindow : public QMainWindow
    {
    Q_OBJECT

    public:
    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);
    }

    @

    1 Reply Last reply
    0
    • G Offline
      G Offline
      Gute
      wrote on last edited by
      #2

      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

      1 Reply Last reply
      0
      • P Offline
        P Offline
        Primordium
        wrote on last edited by
        #3

        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]

        1 Reply Last reply
        0
        • D Offline
          D Offline
          Delay
          wrote on last edited by
          #4

          Did you create two diceout objects? One on line 10 and another one in line 21. Is it correct?

          "No one can go back to the past and make a new beginning, but anyone can start now and make a new ending." Chico Buarque de Holanda

          1 Reply Last reply
          0
          • M Offline
            M Offline
            mlong
            wrote on last edited by
            #5

            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.

            Software Engineer
            My views and opinions do not necessarily reflect those of anyone -- living or dead, real or fictional -- in this universe or any other similar multiverse node. Void where prohibited. Your mileage may vary. Caveat emptor.

            1 Reply Last reply
            0
            • M Offline
              M Offline
              mlong
              wrote on last edited by
              #6

              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.

              Software Engineer
              My views and opinions do not necessarily reflect those of anyone -- living or dead, real or fictional -- in this universe or any other similar multiverse node. Void where prohibited. Your mileage may vary. Caveat emptor.

              1 Reply Last reply
              0
              • G Offline
                G Offline
                giesbert
                wrote on last edited by
                #7

                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);
                }
                @

                Nokia Certified Qt Specialist.
                Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

                1 Reply Last reply
                0

                • Login

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved