设置了布局,但是QWidget看起来仍然是空的
A layout is set, but the QWidget still looks empty
我目前正在做一个基本的学校项目,我不知道如何解决我目前遇到的一个问题。请记住,我是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
, text
和save
类本身很好,但是,您确实应该使用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
- 如果C++类在类方法中具有动态分配,但没有构造函数/析构函数或任何非静态成员,那么它仍然是POD类型吗
- 为什么这个结构的大小是 40,而它看起来应该是 12/24?
- 结构化绑定:当某些内容看起来像引用并且行为类似于引用,但它不是引用时
- 我希望QT-App看起来像QT-App,而不是Android-native
- C++成员函数未声明错误时(当它看起来是错误时)
- 什么是UMThunkStubAMD64,为什么它看起来是一个瓶颈
- 为什么在使用初始化语法时不调用转换运算符,为什么 clang 错误消息看起来是错误的
- 在OpenCV中使用Mat时exc_bad_access,尽管看起来我的索引是正确的
- 为什么此输出看起来不确定?(是十六进制文字的冲刺、printf 还是语法?
- 此代码看起来像是删除了此对象,但事实并非如此
- 使用 c++,为什么我知道它不是时会收到错误消息“字符串下标超出范围”?或者至少看起来是这样
- 钩子内部函数:参数看起来是什么样的?
- 在Eclipse中,当使用汇编器时,汇编看起来是无限的
- 设置了布局,但是QWidget看起来仍然是空的
- 为什么我的除法结果看起来是错的
- 为什么我得到EXC_BAD_ACCESS,即使指针看起来是有效的
- OpenGL/GLFW看起来是透明的
- 看起来像一个引用操作符,但不是c++定义
- 绘制纹理四边形看起来是扭曲的
- 引用看起来是恒定的.为什么不在参数中呢?