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) :
        QMainWindow(parent),
        ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
    
        int num1 = 3;
        int num2 = 5;
    
    add(num1, num2);
    
    }
    
    MainWindow::~MainWindow()
    {
        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.


  • Qt Champions 2016

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



  • Thank you!!


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.