I'm currently working on a basic school project, and I can't figure how to solve a problem that I'm currently encountering. Please keep in mind that I'm a beginner in both C++ and Qt, so the error might be trivial (and I'd like it to be!)
I'm trying to use a class which I developed recently: ArticleEditor. This class inherits from QWidget and is basically meant to be a small window where you can edit articles (a title and a text) and save modifications. For now, I've managed to use this class in a basic way, by creating an instance of ArticleEditor and show it in a main.cpp file, but now I'm trying to do something more tricky.
The point is to open an ArticleEditor widget/window after picking a file. I've achieved the file picking part without issues, but when I've chosen my file, the ArticleEditor widget opens and... is totally empty. The size which I set in a setSize inside its constructor is taken into account, but the widgets I'm setting and adding into the layout, also in the constructor, are not present, nor visible.
Here is my code, which should help you to understand the situation :
ArticleEditor.cpp : enableSave() is meant to enable the save button when you edit one of the two text fields, saveArticle() is another method which is not important here.
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:
};
After picking a file (I'm not gonna give details about this because getting the path is working fine already), I do something like:
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
Here, articleWidget is an attribute of my class, created to be the articleEditor instance's parent widget.
I've also tried setting directly the layout to the parent widget (parent->setLayout(layout) instead of this->setLayout(layout), but whenever I do this, the content of the widget shows, but my signals/slots connections are not working anymore...
If someone could explain me what I'm doing wrong, I'd be very grateful.
EDIT : I just noticed that even after adding the widgets to my layout, when I try layout->isEmpty(), I get true as return value...