Search code examples
qtqtextedit

QTextEdit not displaying


QTextEdit is not displaying on the window and not displaying "hello". Not sure what is wrong here?

#define MAINWINDOW_H
#include <QtGui/QMainWindow>
#include <QtGui/QTextEdit>

    namespace Ui {
        class MainWindow;
    }


    class MainWindow : public QMainWindow
    {
        Q_OBJECT

    public:
        explicit MainWindow(QWidget *parent = 0);

    private:
        QTextEdit *m_textint;
    };
#endif // MAINWINDOW_H

-------------


#include "mainwindow.h"

#include <QtGui/QApplication>

int main(int argc, char *argv[])
{
    QApplication app(argc, argv);

    MainWindow mainWindow;

    mainWindow.setGeometry(QRect(QPoint(100, 100),
                                 QSize(400, 600)));

    mainWindow.show();

    return app.exec();
}

------------------

#include "mainwindow.h"

#include <QtCore/QCoreApplication>

    MainWindow::MainWindow(QWidget *parent)
        : QMainWindow(parent)
    {
        m_textint = new QTextEdit("hello");

        m_textint->setGeometry(QRect(QPoint(10, 10),
                                    QSize(50, 200)));

    }

Solution

  • If you're using QtCreator, you can attach the QTextEdit() object to what is usually declared centralWidget (QWidget).

    I basically took this from the auto-generated code ui_mainwindow.h (using QtCreator).

    void setupUi(QMainWindow *MainWindow)
    {
      QWidget* centralWidget = new QWidget(MainWindow);
      centralWidget->setObjectName(QStringLiteral("centralWidget"));
    
      QTextEdit* textEdit = new QTextEdit(centralWidget);
      textEdit->setObjectName(QStringLiteral("textEdit"));
    
      textEdit->append("Hello");
    }