Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 518175
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T07:54:37+00:00 2026-05-13T07:54:37+00:00

MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { this->setupUi(this); this->setupActions(); this->setWindowTitle(tr(CuteEdit)); label = new QLabel(tr(No

  • 0
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow)
{
    this->setupUi(this);
    this->setupActions();
    this->setWindowTitle(tr("CuteEdit"));
    label = new QLabel(tr("No Open Files"));
    this->setCentralWidget(label);
    label->setAlignment(Qt::AlignCenter);
}

By above code, I get a GUI like this(Its a screenshot of whole screen, Only observe the window displayed in middle of page of ebook). (I used QT Designer)

Now, i want user to select File->Open.. A Dialog appears and file gets selected.. Its contents are to be displayed in *textEdit widget..
Function for that is below..

void MainWindow::loadFile()
{
    QString filename = QFileDialog::getOpenFileName(this);
    QFile file(filename);
    if (file.open(QIODevice::ReadOnly|QIODevice::Text))
    {
        label->hide();
        textEdit->setPlainText(file.readAll());
        mFilePath = filename;
        QMainWindow::statusBar()->showMessage(tr("File successfully loaded."), 3000);
    }
}

The window crashes at line:-

textEdit->setPlainText(file.readAll());

But if i comment the line:-

this->setCentralWidget(label);

i mean i remove label as being the central widget, the program runs as expected.. Why?

And also, I am not clear about the concept of CentralWidget. Pls guide.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T07:54:37+00:00Added an answer on May 13, 2026 at 7:54 am

    JimDaniel is right in his last edit. Take a look at the source code of setCentralWidget():

    void QMainWindow::setCentralWidget(QWidget *widget)
    {
      Q_D(QMainWindow);
      if (d->layout->centralWidget() && d->layout->centralWidget() != widget) {
        d->layout->centralWidget()->hide();
        d->layout->centralWidget()->deleteLater();
      }
      d->layout->setCentralWidget(widget);
    }
    

    Do you see that if your MainWindow already had centralWidget() Qt schedules this object for deletion by deleteLater()?

    And centralWidget() is the root widget for all layouts and other widgets in QMainWindow. Not the widget which is centered on window. So each QMainWindow produced by master in Qt Creator already has this root widget. (Take a look at your ui_mainwindow.h as JimDaniel proposed and you will see).

    And you schedule this root widget for deletion in your window constructor! Nonsense! =)

    I think for you it’s a good idea to start new year by reading some book on Qt. =)

    Happy New Year!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 345k
  • Answers 345k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer The Global.asax file cannot find the code-behind class MvcApplication in… May 14, 2026 at 5:54 am
  • Editorial Team
    Editorial Team added an answer The code above seems more like the correct way to… May 14, 2026 at 5:53 am
  • Editorial Team
    Editorial Team added an answer If the wrapped (protected) application is managed, you can embed… May 14, 2026 at 5:53 am

Related Questions

The application I'm working on has a custom UI that required me to remove
I am new to QT and I am doing some learning. I would like
I've been trying to make my classes completely self contained, but I'm having some
Consider the code below: #!/usr/bin/env python from PyQt4 import QtCore, QtGui import os,sys class
I have a KMainWindow: //file.h class MainWindow: public KMainWindow { public: MainWindow(QWidget *parent =

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.