Planned maintenance has been done but it did not solve the problem. So work will continue on this and a new time for trying updates will be announced asap.

No widget is shown, strangely



  • Hi all,
    Here's a to-do list project and everything seemingly is OK but nothing is shown except for the base window! It seems as though no widget is laid out! Do you know where the problem is asleep, please?

    task.h:

    #ifndef TASK_H
    #define TASK_H
    
    #include <QWidget>
    #include <QString>
    
    class QCheckBox;
    class QPushButton;
    class QSpacerItem;
    class QHBoxLayout;
    
    class Task : public QWidget
    {
        Q_OBJECT
    
    public:
        explicit Task(const QString&, QWidget* parent = nullptr);
    
        void setName(const QString&);
        QString name() const;
        bool isCompleted() const;
    
        ~Task();
    
    public slots:
        void rename();
    
    signals:
       void removed(Task*);
       void statusChanged(Task*);
    
    private slots:
       void checked(bool);
    
    private:
       QCheckBox* checkbox;
       QPushButton* editButton;
       QPushButton* removeButton;
       QSpacerItem* horizontalSpacer;
       QHBoxLayout* horizontalLayout;
    };
    
    #endif // TASK_H
    
    

    task.cpp:

    #include "task.h"
    #include <QInputDialog>
    #include <QWidget>
    #include <QHBoxLayout>
    #include <QPushButton>
    #include <QSpacerItem>
    #include <QCheckBox>
    
    class QcheckBox;
    class QPushButtun;
    class QSpaceItem;
    
    Task::Task(const QString& name, QWidget* parent) : QWidget (parent)
    {
        checkbox = new QCheckBox;
        editButton = new QPushButton(tr("Edit"));
        removeButton = new QPushButton(tr("Remove"));
        horizontalSpacer = new QSpacerItem(40, 20, QSizePolicy::Expanding, QSizePolicy::Minimum);
    
        horizontalLayout = new QHBoxLayout;
        horizontalLayout->addWidget(checkbox);
        horizontalLayout->addItem(horizontalSpacer);
        horizontalLayout->addWidget(editButton);
        horizontalLayout->addWidget(removeButton);
    
        setLayout(horizontalLayout);
    
        setName(name);
    
        connect(editButton, &QPushButton::clicked, this, &Task::rename);
        connect(removeButton, &QPushButton::clicked, [this]()->void {
            emit removed(this); 
                });
        connect(checkbox, &QCheckBox::toggled, this, &Task::checked);
    
    };
    
    //*************************************
    
    void Task::setName(const QString& name) {
        checkbox->setText(name);
    }
    
    //********************************************
    
    QString Task::name() const {
        return checkbox->text();
    }
    
    //*****************************
    
    bool Task::isCompleted() const {
        return checkbox->isChecked();
    }
    
    //****************************************
    
    void Task::rename() {
        bool ok;
        QString value = QInputDialog::getText(this, tr("Edit task"), tr("Task name"),
                                              QLineEdit::Normal, this->name(), &ok);
        if(ok && !value.isEmpty())
            setName(value);
    }
    
    //************************************
    
    void Task::checked(bool checked) {
        QFont font(checkbox->font());
        font.setStrikeOut(checked);
        checkbox->setFont(font);
    
        emit statusChanged(this);
    }
    
    //**************************************************
    
    Task::~Task() {
        delete checkbox;
        delete editButton;
        delete removeButton;
        delete horizontalSpacer;
    }
    

    test_1.h:

    #ifndef TEST_1_H
    #define TEST_1_H
    
    #include <QMainWindow>
    #include "task.h"
    #include <QVector>
    
    class QLabel;
    class QPushButton;
    class QSpacerItem;
    class QHBoxLayout;
    class QVBoxLayout;
    
    class test_1 : public QMainWindow
    {
        Q_OBJECT
    
    public:
        explicit test_1(QWidget *parent = nullptr);
        void updateStatus();
        ~test_1();
    
    public slots:
        void addTask();
        void removeTask(Task*);
        void taskStatusChanged();
    
    private:
        QVector<Task*> mTasks;
        QLabel* statusLabel;
        QPushButton* addTaskButtun;
        QSpacerItem* verticalSpcaer;
        QSpacerItem* horizontalSpacer;
        QHBoxLayout* horizontalTasksLayout;
        QVBoxLayout* verticalTasksLayout;
    };
    
    #endif // TEST_1_H
    

    test_1.cpp:

    #include "test_1.h"
    #include <QPushButton>
    #include <QLabel>
    #include <QSpacerItem>
    #include <QHBoxLayout>
    #include <QVBoxLayout>
    #include <QString>
    #include <QInputDialog>
    
    test_1::test_1(QWidget *parent)
        : QMainWindow(parent), mTasks()
    {
        statusLabel = new QLabel(tr("Status: 0 todo / 0 done"));
        addTaskButtun = new QPushButton(tr("Add task"));
        verticalSpcaer = new QSpacerItem(20, 40, QSizePolicy::Minimum, QSizePolicy::Expanding);
        horizontalSpacer = new QSpacerItem(40, 20, QSizePolicy::Expanding, QSizePolicy::Minimum);
    
        horizontalTasksLayout = new QHBoxLayout;
        horizontalTasksLayout->addWidget(statusLabel);
        horizontalTasksLayout->addItem(horizontalSpacer);
        horizontalTasksLayout->addWidget(addTaskButtun);
    
        verticalTasksLayout = new QVBoxLayout;
        verticalTasksLayout->addLayout(horizontalTasksLayout);
        verticalTasksLayout->addItem(verticalSpcaer);
    
        setLayout(verticalTasksLayout);
    
        connect(addTaskButtun, &QPushButton::clicked, this, &test_1::addTask);
        updateStatus();
    }
    
    //************************************************
    
    void test_1::addTask() {
       bool ok;
       QString name = QInputDialog::getText(this, tr("Add Task"), tr("Task name"),
                                            QLineEdit::Normal, tr("Untitled task"), &ok);
    
       if(ok && !name.isEmpty()) {
           Task* task = new Task(name);
           connect(task, &Task::removed, this, &test_1::removeTask);
           connect(task, &Task::statusChanged, this, &test_1::taskStatusChanged);
           mTasks.append(task);
           verticalTasksLayout->addWidget(task);
           updateStatus();
       }
    }
    
    //*********************************************
    
    void test_1::removeTask(Task* task) {
       mTasks.removeOne(task);
       verticalTasksLayout->removeWidget(task);
       delete task;
       updateStatus();
    }
    
    //**************************************
    
    void test_1::taskStatusChanged() {
        updateStatus();
    }
    
    //************************************
    
    void test_1::updateStatus() {
        int completedCount = 0;
        
        for(auto t: mTasks)
            if(t->isCompleted())
                completedCount++;
    
        int todoCount = mTasks.size() - completedCount;
        statusLabel->setText(QString("Statuse: %1 todo / %2 completed")
                             .arg(todoCount)
                             .arg(completedCount));
    }
    
    //*****************************
    
    test_1::~test_1()
    {
        delete statusLabel;
        delete addTaskButtun;
        delete verticalSpcaer;
        delete horizontalSpacer;
        delete horizontalTasksLayout;
        delete verticalTasksLayout;
    }
    

    main.cpp:

    #include "test_1.h"
    #include <QApplication>
    
    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
        test_1 w;
        w.show();
    
        return a.exec();
    }
    


  • Hi @tomy , i guess you need to create your own widget and add the layout to it.

    For example you need to do it like this:-

    QWidget *widget = new QWidget;
    QVBoxLayout *vBox = new QVBoxLayout;
    QPushButton *pb1 = new QPushButton;
    QPushButton *pb2 = new QPushButton;
    QPushButton *pb3 = new QPushButton;
    QPushButton *pb4 = new QPushButton;
    
    vBox->addWidget(pb1);
    vBox->addWidget(pb2);
    vBox->addWidget(pb3);
    vBox->addWidget(pb4);
    
    widget->setLayout(vBox);
    setCentralWidget(widget);
    

    So in your code do it like this:-

    QWidget *widget = new QWidget;
    widget->setLayout(verticalTasksLayout);
    setCentralWidget(widget);

  • Qt Champions 2017

    Your test_1 is QMainWindow. Don't set the layout for the mainwindow. Method setLayout(verticalTasksLayout) must be giving the warning at the console. Please look at it.

    Just have blank Widget and set the layout for the blank widget as @Shrinidhi-Upadhyaya already suggested. Add it as setCentralWIdget

    QWidget *w = new QWidget
    w->setLayout(verticalTasksLayout).
    this->setCentralWidget(w);

    This should solve your problem.



  • Thank you,

    1- Why do I need to create another widget while QMainWindow itself is a window/widget and inherits QWidget?

    2-When I run the project, it gives me a blank window, then why aren't the widgets created in the program set onto it?

    3- As for your snippet, I added that and get this issue when I click on Add task:

    setGeometry: Unable to set geometry 120x30+681+345 on QWidgetWindow/'QInputDialogClassWindow'. Resulting geometry: 178x90+681+345 (frame: 8, 31, 8, 8, custom margin: 0, 0, 0, 0, minimum size: 178x90, maximum size: 524287x90).


  • Lifetime Qt Champion

    Hi,

    1. Because QMainWindow can be seen as a "container" widget. It provides you with the scaffolding to add tool bars, dock widgets etc. The central widget is where you will put your custom QMainWindow content. It can be a custom widget or one that you build on the fly. That's up to you. If you don't want/need the features provided by QMainWindow, then use QDialog or QWidget as base class of your custom widget.


  • @SGaist
    Thank you.

    So seemingly we have only widgets or containers in Qt, right?
    And for the containers we need a type of a bigger widget to lay out our smaller widgets (labels, buttons, check boxes, etc) on it. Right again?

    If so, what's the list of all containers in Qt so that I won't mix them up with widgets and I am able to choose the container which meets the needs per project?

    Also, what's the list of the widgets in Qt, so that when I have a container already, I can select the best widget for that container based on my needs?


  • Qt Champions 2018

    Here a list of all Qt widgets: https://doc.qt.io/qt-5/qtwidgets-module.html
    And you should really read this: https://doc.qt.io/qt-5/layout.html


  • Lifetime Qt Champion

    By container widget I meant "a widget made to contain other widgets". Unrelated to QVector and friends.



  • Thank you all very much.

    I think QMainWindow is not acting as a widget, the way I'm familiar with widgets; it mostly works as an application window, in that sense that we, for instance, can put a widget, like a QPushButon, onto a QFormLayout or another widget without coming another widget to the scene, but this for QMainWindow differs.

    Special widgets like QMainWindow are application windows logically, as far as I'm concerned (up to now).
    But now I know we have dozens of Qt widgets on the list above (and it needs much much time to know them and recall the one needed when the developer is coding their project! ¯_(ツ)_/¯ ). The question is, where is the list of those special widgets (or better to say, containers —apart from QVector and so on)?



  • Other question, for the example, is in the test_1.cpp:

    void test_1::updateStatus() {
        int completedCount = 0;
        
        for(auto t: mTasks)
            if(t->isCompleted())
                completedCount++;
    
        int todoCount = mTasks.size() - completedCount;
        statusLabel->setText(QString("Statuse: %1 todo / %2 completed")
                             .arg(todoCount)
                             .arg(completedCount));
    }
    

    t is of the class test_1 but how could it have called a method, bool isCompleted() const;, from another class, task?


  • Qt Champions 2018

    @tomy said in No widget is shown, strangely:

    I think QMainWindow is not acting as a widget, the way I'm familiar with widgets; it mostly works as an application window, in that sense that we, for instance, can put a widget, like a QPushButon, onto a QFormLayout or another widget without coming another widget to the scene, but this for QMainWindow differs.

    That's because QMainWindow has a widgets called Central Widget in its central part.

    "The question is, where is the list of those special widgets (or better to say, containers —apart from QVector and so on)?" - in the documentation: https://doc.qt.io/qt-5/containers.html Also, containers do not have anything to do with widgets.


  • Qt Champions 2018

    @tomy said in No widget is shown, strangely:

    t is of the class test_1 but how could it have called a method, bool isCompleted() const;, from another class, task?

    Like any other method of a class? If updateStatus() is not static you need an instance of test_1 to call any public method on it...



  • @jsulm

    Like any other method of a class? If updateStatus() is not static you need an instance of test_1 to call any public method on it...

    Both t (mTask vector) and updatestatus() are of the same class test_1, whilest iscompleted() is a method of the class task (the whole example is above, in the first post and it works right!)

    That QVector<Task*> mTasks; private data member can at the upmost level call other private/public data members/methods of the same class not another class just because that new class is also added to the project. I mean there should be a way to call them, if my recollection is accurate. Will "include task.h" work it out!?


  • Qt Champions 2018

    @tomy said in No widget is shown, strangely:

    data members/methods of the same class not another class just because that new class is also added to the project. I mean there should be a way to call them

    I don't understand the problem: you can call methods of other classes in a class as long as you have instances of that class:

    void ClassA::someMethod()
    {
        ClassB classB;
        classB.someMethodInClassB();
    }
    

    So, what is the problem?



  • @jsulm

    That's because QMainWindow has a widgets called Central Widget in its central part.

    So worse! It mustn't need another QWidget to be instantiated like in the posts #2, #3. And we should've been able to put the controls directly on it. But it sounds that, it's not a real central widget, but a place/room, that "must" be filled by another widget! :(

    "The question is, where is the list of those special widgets (or better to say, containers —apart from QVector and so on)?" - in the documentation: https://doc.qt.io/qt-5/containers.html Also, containers do not have anything to do with widgets.

    No, I didn't mean that indeed, although that link was also nifty. :)
    I mean those widget/application windows containers like QMainWindow. That work as scaffolding covering the whole project and also need some widgets to be added to be able to put other controls on them, like the one above.



  • @jsulm

    I don't understand the problem: you can call methods of other classes in a class as long as you have instances of that class:

    void ClassA::someMethod()
    {
        ClassB classB;
        classB.someMethodInClassB();
    }
    

    So, what is the problem?

    I know that.
    Oh, so sorry. The vector contains instances of the class Task. :)
    I don't know why I didn't note that, I must have been out of my mind!


  • Qt Champions 2018

    @tomy said in No widget is shown, strangely:

    But it sounds that, it's not a real central widget, but a place/room, that "must" be filled by another widget! :(

    Wrong. There is already a widget by default which you can get using https://doc.qt.io/qt-5/qmainwindow.html#centralWidget
    If you, for some reason, want to set your own widget you can do so as well: https://doc.qt.io/qt-5/qmainwindow.html#setCentralWidget

    QMainWindow simply provides functionality you need in an application window. A QWidget is simply a base class for all widgets and does not (and should not) contain functionality needed in a main window of an application.


  • Lifetime Qt Champion

    @jsulm said in No widget is shown, strangely:

    Wrong. There is already a widget by default which you can get using https://doc.qt.io/qt-5/qmainwindow.html#centralWidget

    Just to clear one thing, as noted in the documentation, by default there is no central widget. You may have one though when using Designer as it might be added automagically for you.



  • Another question,
    Please take a look at the following method of the example:

    void test_1::addTask() {
       bool ok;
       QString name = QInputDialog::getText(this, tr("Add Task"), tr("Task name"),
                                            QLineEdit::Normal, tr("Untitled task"), &ok);
    
       if(ok && !name.isEmpty()) {
           Task* task = new Task(name);
           connect(task, &Task::removed, this, &test_1::removeTask);
           connect(task, &Task::statusChanged, this, &test_1::taskStatusChanged);
           mTasks.append(task);
           verticalTasksLayout->addWidget(task);
           updateStatus();
       }
    }
    

    Both connections are right and work correctly, inside the if condition, but my question is couldn't we declare those two connections somewhere else so that they're more general and not specifically inside that condition?

    Declaring the connections inside a condition to my eyes looks rather weird! What are your opinions, please?


  • Lifetime Qt Champion

    Hi
    Perfectly normal.
    The connect statements are part of the new Task creation so its natural place to have them near where the instance is created.
    if no new task is created, then no connection needed



  • @mrjj
    But it's sort of repetitive, isn't it?
    We here declare the connections once again each time a new task is created, while the whole connections' concept is a sole thing, I surmise.


  • Lifetime Qt Champion

    @tomy
    Oh no.
    A connection is pr instance .
    Meaning, if you want every task to be able to report "statusChanged" then
    each must be connected.
    Its not as a type thing. Its from real object to real object.
    So all must be connected.


Log in to reply