Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. No widget is shown, strangely
Qt 6.11 is out! See what's new in the release blog

No widget is shown, strangely

Scheduled Pinned Locked Moved Unsolved General and Desktop
22 Posts 7 Posters 5.8k Views 4 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • tomyT Offline
    tomyT Offline
    tomy
    wrote on last edited by
    #1

    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();
    }
    
    1 Reply Last reply
    0
    • Shrinidhi UpadhyayaS Offline
      Shrinidhi UpadhyayaS Offline
      Shrinidhi Upadhyaya
      wrote on last edited by Shrinidhi Upadhyaya
      #2

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

      Shrinidhi Upadhyaya.
      Upvote the answer(s) that helped you to solve the issue.

      1 Reply Last reply
      7
      • dheerendraD Offline
        dheerendraD Offline
        dheerendra
        Moderators Qt Champions 2024 Qt Champions 2022 Qt Champions 2017
        wrote on last edited by dheerendra
        #3

        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.

        Dheerendra
        @Community Service
        Certified Qt Specialist
        https://www.pthinks.com

        1 Reply Last reply
        7
        • tomyT Offline
          tomyT Offline
          tomy
          wrote on last edited by tomy
          #4

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

          1 Reply Last reply
          0
          • SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on last edited by
            #5

            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.

            Interested in AI ? www.idiap.ch
            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

            tomyT 1 Reply Last reply
            5
            • SGaistS SGaist

              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.
              tomyT Offline
              tomyT Offline
              tomy
              wrote on last edited by
              #6

              @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?

              1 Reply Last reply
              0
              • Christian EhrlicherC Offline
                Christian EhrlicherC Offline
                Christian Ehrlicher
                Lifetime Qt Champion
                wrote on last edited by
                #7

                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

                Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                Visit the Qt Academy at https://academy.qt.io/catalog

                1 Reply Last reply
                4
                • SGaistS Offline
                  SGaistS Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

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

                  Interested in AI ? www.idiap.ch
                  Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                  1 Reply Last reply
                  2
                  • tomyT Offline
                    tomyT Offline
                    tomy
                    wrote on last edited by
                    #9

                    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)?

                    jsulmJ 1 Reply Last reply
                    0
                    • tomyT Offline
                      tomyT Offline
                      tomy
                      wrote on last edited by
                      #10

                      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?

                      jsulmJ 1 Reply Last reply
                      0
                      • tomyT tomy

                        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)?

                        jsulmJ Offline
                        jsulmJ Offline
                        jsulm
                        Lifetime Qt Champion
                        wrote on last edited by jsulm
                        #11

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

                        https://forum.qt.io/topic/113070/qt-code-of-conduct

                        tomyT 1 Reply Last reply
                        0
                        • tomyT tomy

                          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?

                          jsulmJ Offline
                          jsulmJ Offline
                          jsulm
                          Lifetime Qt Champion
                          wrote on last edited by
                          #12

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

                          https://forum.qt.io/topic/113070/qt-code-of-conduct

                          tomyT 1 Reply Last reply
                          0
                          • jsulmJ jsulm

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

                            tomyT Offline
                            tomyT Offline
                            tomy
                            wrote on last edited by
                            #13

                            @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!?

                            jsulmJ 1 Reply Last reply
                            0
                            • tomyT tomy

                              @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!?

                              jsulmJ Offline
                              jsulmJ Offline
                              jsulm
                              Lifetime Qt Champion
                              wrote on last edited by
                              #14

                              @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?

                              https://forum.qt.io/topic/113070/qt-code-of-conduct

                              tomyT 1 Reply Last reply
                              1
                              • jsulmJ jsulm

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

                                tomyT Offline
                                tomyT Offline
                                tomy
                                wrote on last edited by
                                #15

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

                                jsulmJ 1 Reply Last reply
                                0
                                • jsulmJ jsulm

                                  @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?

                                  tomyT Offline
                                  tomyT Offline
                                  tomy
                                  wrote on last edited by tomy
                                  #16

                                  @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!

                                  1 Reply Last reply
                                  0
                                  • tomyT tomy

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

                                    jsulmJ Offline
                                    jsulmJ Offline
                                    jsulm
                                    Lifetime Qt Champion
                                    wrote on last edited by jsulm
                                    #17

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

                                    https://forum.qt.io/topic/113070/qt-code-of-conduct

                                    SGaistS 1 Reply Last reply
                                    1
                                    • jsulmJ jsulm

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

                                      SGaistS Offline
                                      SGaistS Offline
                                      SGaist
                                      Lifetime Qt Champion
                                      wrote on last edited by
                                      #18

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

                                      Interested in AI ? www.idiap.ch
                                      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                                      1 Reply Last reply
                                      2
                                      • tomyT Offline
                                        tomyT Offline
                                        tomy
                                        wrote on last edited by
                                        #19

                                        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?

                                        1 Reply Last reply
                                        0
                                        • mrjjM Offline
                                          mrjjM Offline
                                          mrjj
                                          Lifetime Qt Champion
                                          wrote on last edited by
                                          #20

                                          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

                                          tomyT 1 Reply Last reply
                                          2

                                          • Login

                                          • Login or register to search.
                                          • First post
                                            Last post
                                          0
                                          • Categories
                                          • Recent
                                          • Tags
                                          • Popular
                                          • Users
                                          • Groups
                                          • Search
                                          • Get Qt Extensions
                                          • Unsolved