sudeepdino008
sudeepdino008

Reputation: 3364

setCentralWidget not declared in scope

#include <QtGui>
#include <QWidget>
#include "notepad.h"

notepad::notepad()
{
    textField = new QTextEdit;
    setCentralWidget(textField);
    setWindowTitle(tr("The building of a notepad...."));
}

This is a file of one of my Qt project. There is some error with the setCentralWidget part. The error is that it is not declared in the scope. But I have included the QWidget class under which it gets included. What is the mistake?

Upvotes: 0

Views: 2849

Answers (2)

Cameron Tinker
Cameron Tinker

Reputation: 9789

Like others have said, setCentralWidget(..) is only a member of QMainWindow. I think the behavior you are looking for can be achieved by adding a layout to your QWidget and then adding your QTextEdit to the layout. I would suggest a QPlainTextEdit as it is setup for editing multiple lines of a text document. QTextEdit is usually used for single line input. Here's some sample code:

notepad::notepad()
{
    QVBoxLayout *layout = new QVBoxLayout();
    QPlainTextEdit *textBox = new QPlainTextEdit();
    layout->addWidget(textBox);
    this->setLayout(layout);
    setWindowTitle(tr("The building of a notepad...."));
}

The layout can be a QVBoxLayout, QHBoxLayout, QGridLayout, etc. It all depends on what you want to achieve with the layout of the form. You can also add to your existing layout by using this->addWidget(QWidget*) instead of using a newly created layout. I hope this helps.

Upvotes: 0

Tom W
Tom W

Reputation: 1334

setCentralWidget is a method on QMainWindow. It is not a top-level function. It would only be in scope here if your notepad class derives from QMainWindow, which I guess it must not.

Upvotes: 3

Related Questions