Qt World Summit: Submit your Presentation

Displaying Widget in Dialog from code

  • Hi,
    I'm trying to display sum_Label in MainWindow but instead it opens a new window and displays the label in the new window.

    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    MainWindow::MainWindow(QWidget *parent) :
        ui(new Ui::MainWindow)
        int num1 = 3;
        int num2 = 5;
    add(num1, num2);
        delete ui;
    void MainWindow::add(int num1, int num2)
        int sum = num1 + num2;
    	QString sumvalue = QString::number (sum);
        QLabel *sum_Label = new QLabel();
        sum_Label->setText (sumvalue);
        sum_Label->show ();

    What can I do to display the label in the MainWindow?
    Thank you.

  • Lifetime Qt Champion

    u need to give it "this" as parent
    new QLabel(this);

  • Thank you!!

Log in to reply