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

  • SEARCH
  • Home
  • 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

Related Questions

MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this); } In this section of code,
#include mainwindow.h #include ui_mainwindow.h #include <Phonon/MediaSource> #include <QUrl> #include <Phonon/MediaObject> MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent),
I have a KMainWindow: //file.h class MainWindow: public KMainWindow { public: MainWindow(QWidget *parent =
public MainWindow() { CommandManager.AddExecutedHandler(this, ExecuteHandler); } void ExecuteHandler(object sender, ExecutedRoutedEventArgs e) { } Error
I'm trying to enter edit mode on a specific cell like this: void MainWindow::on_addButton_released()
this is a simple OOP QT question. my app consists of main window (QMainWindow)
I'm trying to make a class that extends qwidget, that pops up a new
I managed to get a QPushButton to open a new window when pressed by
So the non- QMdiArea version of my code, MyWidget::MyWidget(QWidget* parent) { ... layout()->setSizeConstraint( QLayout::SetFixedSize
Question asked twice: see Handling multiple ui files in Qt I am new to

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.