tags:

views:

526

answers:

2
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.

A: 

Are you sure it's not label->hide() that's crashing the app? Perhaps Qt doesn't like you hiding the central widget. I use Qt but I don't mess with QMainWindow that often.

EDIT: I compiled your code. I can help you a bit. Not sure what the ultimate reason is as I don't use the form generator, but you probably shouldn't be resetting the central widget to your label, as it's also set by the designer, if you open the ui_mainwindow.h file and look in setupGui() you can see that it has a widget called centralWidget that's already set. Since you have used the designer for your GUI, I would use it all the way and put the label widget in there as well. That will likely fix your problems. Maybe someone else can be of more help...

JimDaniel
Ok.. I commented that line too.. Application is getting crashed still..:-(
shadyabhi
You are right.. After your second EDIT, I got the solution.. Thanx
shadyabhi
+6  A: 

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 schedules this root widget for deletion in your window constructor! Nonsense! =)

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

Happy New Year!

kemiisto
i was following a book "The Art of Building Qt Applications by DANIEL MOLKENTIN"... And I didnt get anything like that. I know its my mistake i should have looked the source.. My bad..Anyways, thank you very much and Happy new year...
shadyabhi
Very precise and exact answer. Exactly what I wanted. :)Thanx again
shadyabhi
Daniel Molkentin wrote my favorite book on Qt. So continue amusing reading. In the meantime don't deny reading official documentation and source code to yourself. Sometimes one line of code can tell more than even very thick book.
kemiisto
thanx for the advice.
shadyabhi