0
votes

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...

2
You might as well get rid of the move() calls, they won't make any difference -- the QVBoxLayout is going to reposition the widgets you added to it anyway.Jeremy Friesner
Thanks, I forgot about that, getting rid of those extra lines right now.Cécile Fecherolle
made test app, all works for meShf
the class itself works fine, more details on how you call it please, more specifically, about articleWidget. Did you add your ArticleEditor aE to layout in arcticleWidget?Shf

2 Answers

2
votes

I think in ArticleEditor.cpp you need to make your layout a child of the widget

layout = new QVBoxLayout(this);

Your widgets should also be children of the ArticleEditor widget; i.e. title, text and save.

1
votes

The class itself is fine, though, you really should use approach, that koan recommended, it gives a lot more clarity to the code.

Your problem is that you set parent of your ArticleWidget, but didn't added layout to it. So istead of

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();)

Do something like this:

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