0

Помогите, пожалуйста, добавить текстовое поле внутрь окна w. Почему-то выходит создать два отдельных окна. Вот мой код

mainclass.cpp

#include "mainclass.h"
#include <QMainWindow>
#include <QTextEdit>
#include <QVBoxLayout>

MainClass::MainClass(QWidget* parent) : QMainWindow(parent) {
    QVBoxLayout *QMTLayout = new QVBoxLayout;
    QTextEdit *TE = new QTextEdit;
    QMTLayout ->addWidget(TE);
    TE->setGeometry(0,0,600,600);
    TE->show();
    setLayout(QMTLayout);
}

main.cpp

#include "mainclass.h"
#include <QApplication>
#include <QTextCodec>
#include <QtGui>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainClass w;

    QTextCodec::setCodecForCStrings(QTextCodec::codecForName("utf-8"));
    w.resize(800, 600);
    w.setWindowIcon(QIcon("C:\\Qt\\TexRed/TLeater.jpg"));
    w.setWindowTitle("Текстовый редактор");
    w.setFocus();

    QHBoxLayout *QMTLayout = new QHBoxLayout;
    QTextEdit *MainTextEdit = new QTextEdit;
    QMTLayout ->addWidget(MainTextEdit, 8);

    w.show();

    return a.exec();
}

1 ответ 1

1

Для того, чтобы виджеты отображались в окне а не как отдельное окно, им нужно задать родителя. Вот как правильно создавать виджеты (это делается внутри класса MainClass)

QHBoxLayout *QMTLayout = new QHBoxLayout(this);
QTextEdit *MainTextEdit = new QTextEdit(this);
QMTLayout ->addWidget(MainTextEdit, 8);
this->setLayout(QMTLayout);

А так же необходимо главному виджету окна установить Layout в который добавил виджет.

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.