设置了布局,但是QWidget看起来仍然是空的

A layout is set, but the QWidget still looks empty

本文关键字:看起来 仍然是 QWidget 但是 布局 设置      更新时间:2023-10-16

我目前正在做一个基本的学校项目,我不知道如何解决我目前遇到的一个问题。请记住,我是c++和Qt的初学者,所以错误可能是微不足道的(我希望它是!)

我正在尝试使用我最近开发的一个类:文章编辑器。这个类继承自QWidget,基本上是一个小窗口,您可以在其中编辑文章(标题和文本)并保存修改。现在,我已经设法以一种基本的方式使用这个类,通过创建ArticleEditor的实例并在main.cpp文件中显示它,但现在我正试图做一些更棘手的事情。

关键是在选择文件后打开ArticleEditor小部件/窗口。我已经实现了文件选择部分没有问题,但是当我选择了我的文件,文章编辑器小部件打开和…完全是空的。我在其构造函数中的setSize中设置的大小被考虑在内,但是我设置和添加到布局中的小部件,也在构造函数中,不存在,也不可见。

下面是我的代码,可以帮助你理解情况:

ArticleEditor.cpp :enableSave()用于在编辑两个文本字段之一时启用保存按钮,saveArticle()是另一个方法,在这里不重要。

ArticleEditor::ArticleEditor(Article* article, QWidget *parent) :
    QWidget(parent)
{
    title = new QLineEdit();
    title->setFixedWidth(180);
    title->move(10,10);
    text = new QTextEdit();
    text->setFixedSize(180,110);
    text->move(10,45);
    save = new QPushButton();
    save->setText("Save");
    save->setFixedWidth(80);
    save->move(10,170);
    save->setEnabled(false);
    title->setText(article->getTitle());
    text->setText(article->getText());
    QObject::connect(save, SIGNAL(clicked()), this, SLOT(saveArticle()));
    QObject::connect(title, SIGNAL(textChanged(const QString)), this, SLOT(enableSave()));
    QObject::connect(text, SIGNAL(textChanged()), this, SLOT(enableSave()));
    layout = new QVBoxLayout;
    layout->addWidget(title);
    layout->addWidget(text);
    layout->addWidget(save);
    this->setFixedSize(200,200);
    this->setLayout(layout);
}

ArticleEditor.h

class ArticleEditor : public QWidget
    {
        Q_OBJECT
    public:
        explicit ArticleEditor(Article* article, QWidget* parent);
    private:
        QLineEdit* title;
        QTextEdit* text;
        QPushButton* save;
        QVBoxLayout* layout;
        Article* ressource;
    public slots:
        void saveArticle();
        void enableSave();
    private slots:
    };

选择一个文件后(我不打算详细说明,因为获取路径已经很好了),我做如下操作:

ArticleEditor aE(&a, articleWidget); // calling the constructor with an article I've fetched using the path, and setting articleWidget (a private attribute) to be the parent
articleWidget->show(); // showing the result

这里,articleWidget是我的类的一个属性,被创建为articleEditor实例的父小部件。

我也试过直接设置布局到父小部件(parent->setLayout(layout)而不是这个->setLayout(layout),但每当我这样做,小部件的内容显示,但我的信号/插槽连接不再工作…

如果有人能告诉我我做错了什么,我将非常感激。

编辑:我刚刚注意到,即使添加小部件到我的布局后,当我尝试布局->isEmpty(),我得到true作为返回值…

我认为在ArticleEditor.cpp你需要使你的布局的子部件

layout = new QVBoxLayout(this);

你的小部件也应该是ArticleEditor小部件的子部件;即title, textsave

类本身很好,但是,您确实应该使用koan推荐的方法,它使代码更加清晰。

你的问题是你设置了你的ArticleWidget的父类,但没有添加布局。所以不用

ArticleEditor aE(&a, articleWidget);
articleWidget->show(); // showing the result (by the way, if you are not using new, when create widget, it shuold be articleWidget.show();)

像这样做:

ArticleEditor aE=new ArticleEditor(&a, articleWidget); //creating articleEditor aE with articleWidget as parent
QVBoxLayout* articleWidgetLayout=new QVBoxLayout(articleWidget); // creating layout in articleWidget
articleWidgetLayout->addWidget(aE); //adding aE to it
articleWidget->show(); // showing the result