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. Manually Implementing Custom Widget in MainWindow
QtWS25 Last Chance

Manually Implementing Custom Widget in MainWindow

Scheduled Pinned Locked Moved Solved General and Desktop
source codecustom widgetcirculardependencies
15 Posts 5 Posters 8.1k Views
  • 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.
  • B Offline
    B Offline
    Burke212
    wrote on 13 Feb 2019, 21:19 last edited by Burke212
    #1

    I think I've created a circular dependency in my code, but I'm not sure if it really is circular dependency, and if it's even the correct way to do things.

    I know that it is possible to design a UI that can be instantiated in another UI. For example, a widget with a button that dynamically adds new buttons to the widget when pressed.

    0_1550090382257_Capture.PNG

    I've struggled to replicate this in source/ manually until today. But the way I've designed it confuses me somewhat because it seems like two widgets depend upon each other circularly.

    The following are the steps that lead to the circular dependency I'm referring to:

    1. In MainWindow::setup(), I create the containerWidget followed by the inCl class object which takes containerWidget as a param.
    2. In customComboClass::setup I basically set the layout to the containerWidget.
    3. Back in MainWindow::setup() I add containerWidget to mainLayout.
    4. Then I add inCl to containerWidget's layout. This is where I see the circle complete.

    mainwindow.h

    #ifndef MAINWINDOW_H
    #define MAINWINDOW_H
    
    #include <QMainWindow>
    #include <QVBoxLayout>
    #include <QDebug>
    
    #include "customcomboclass.h"
    
    class MainWindow : public QMainWindow
    {
        Q_OBJECT
    
    public:
        explicit MainWindow(QWidget *parent = nullptr);
        ~MainWindow();
    
    private:
        void setup();
    
        customComboClass *inCl;
    
        QPushButton *call_button;
        QVBoxLayout *mainLayout;
        QWidget *containerWidget;
        QWidget *mainWidget;
    };
    
    #endif // MAINWINDOW_H
    
    

    mainwindow.cpp

    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    
    MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent)
    {
        setup();
    }
    
    MainWindow::~MainWindow()
    {
    }
    
    void MainWindow::setup(){
        containerWidget = new QWidget(this);// Container widget for customComboClass widget object. Holds elements of inheriting class.
        inCl = new customComboClass(containerWidget);// Inheriting class object. Send containing widget as param.
    
        call_button = new QPushButton(this);
        call_button->setText("Make a Call");
    
        mainLayout = new QVBoxLayout();
        mainLayout->addWidget(call_button);
        mainLayout->addWidget(containerWidget);// Add containing widget to layout.
        containerWidget->layout()->addWidget(inCl);// Add inheritng class object to containing widget. Fill containing widget with elements from inheriting class object.
    
        mainWidget = new QWidget();
        mainWidget->setLayout(mainLayout);
        mainWidget->setMinimumSize(120,100);
        setCentralWidget(mainWidget);
    }
    
    

    customcomboclass.h

    #ifndef CUSTOMCOMBOCLASS_H
    #define CUSTOMCOMBOCLASS_H
    
    #include <QWidget>
    #include <QDebug>
    #include <QListWidget>
    #include <QListWidgetItem>
    #include <QHBoxLayout>
    #include <QPushButton>
    #include <QMessageBox>
    
    class customComboClass : public QWidget
    {
        Q_OBJECT
    
    public:
        explicit customComboClass(QWidget *w, QWidget *parent = nullptr);
        ~customComboClass();
    
    public slots:
        void setup(QWidget *w);
    
    private:
        QStringList itemList = {"", "OFF", "Circuit", "Radio"};
        QPushButton *button;
        QListWidget *listWidget;
        QHBoxLayout *hLayout;
    };
    
    #endif // CUSTOMCOMBOCLASS_H
    
    

    customcomboclass.cpp

    #include "customcomboclass.h"
    #include "ui_customcomboclass.h"
    
    customComboClass::customComboClass(QWidget *tWidget, QWidget *parent) :
        QWidget(parent)
    {
        setup(tWidget);
    }
    
    customComboClass::~customComboClass()
    {
    }
    
    void customComboClass::setup(QWidget *w){
        button = new QPushButton();
        button->setText("Press Me");
        button->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
    
        listWidget = new QListWidget();
        listWidget->addItems(itemList);
        listWidget->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
        listWidget->setMaximumHeight(25);
    
        hLayout = new QHBoxLayout();
        hLayout->addWidget(listWidget);
        hLayout->addWidget(button);
    
        w->setLayout(hLayout);
    }
    
    
    
    

    Main.cpp

    #include "mainwindow.h"
    #include <QApplication>
    
    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
        MainWindow w;
        w.show();
    
        return a.exec();
    }
    
    1 Reply Last reply
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 13 Feb 2019, 21:41 last edited by
      #2

      Hi,

      Usually, each widget is responsible for its own setup. They don't depend on another widget like you did. The container widget will create a new contained widget and add it to one of its layout for example. But the contained widget will not care about what the container widget is.

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

      B 1 Reply Last reply 14 Feb 2019, 16:11
      1
      • S SGaist
        13 Feb 2019, 21:41

        Hi,

        Usually, each widget is responsible for its own setup. They don't depend on another widget like you did. The container widget will create a new contained widget and add it to one of its layout for example. But the contained widget will not care about what the container widget is.

        B Offline
        B Offline
        Burke212
        wrote on 14 Feb 2019, 16:11 last edited by
        #3

        @SGaist

        I'm not exactly sure how to pass my custom widget to MainWindow.

        How would you recommend accomplishing this?

        1 Reply Last reply
        0
        • S Offline
          S Offline
          SGaist
          Lifetime Qt Champion
          wrote on 14 Feb 2019, 21:52 last edited by
          #4

          You can use your custom widgets like any other widget. What exactly is troubling you ?

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

          B 1 Reply Last reply 20 Feb 2019, 21:13
          1
          • S SGaist
            14 Feb 2019, 21:52

            You can use your custom widgets like any other widget. What exactly is troubling you ?

            B Offline
            B Offline
            Burke212
            wrote on 20 Feb 2019, 21:13 last edited by
            #5

            @SGaist

            My hangup is that I don't know the proper way to design a custom widget with it's elements (buttons, sliders, labels...) and layouts, and add it to another widget, such as the MainWindow.

            I've made custom widget classes that basically look like this:

            button;
            vertical_layout;
            vertical_layout->addWidget(button);
            custom_widget;
            custom_widget->setLayout(vertical_layout);
            

            Then create an object of that class in MainWindow, and add it to MainWindow's layout. But sometimes I've gotten strange errors, and I've always thought that this way isn't the right way.

            I'm just trying to learn what is the best way to make a custom widget and add it to MainWindow.

            1 Reply Last reply
            0
            • S Offline
              S Offline
              SGaist
              Lifetime Qt Champion
              wrote on 20 Feb 2019, 21:25 last edited by
              #6

              You can use designer or do it by code only. That's up to you.

              What strange errors do you get ?

              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
              0
              • B Offline
                B Offline
                Burke212
                wrote on 25 Feb 2019, 14:14 last edited by
                #7

                I don't always get errors. Typically the widget simply doesn't display.

                Maybe it would help if I include a simpler project as an example.

                MainWindow.h

                #ifndef MAINWINDOW_H
                #define MAINWINDOW_H
                
                #include <QMainWindow>
                #include <QDialog>
                #include <QPushButton>
                #include <QLineEdit>
                #include <QMessageBox>
                #include <QVBoxLayout>
                #include <QHBoxLayout>
                #include <QGridLayout>
                #include <QPixmap>
                #include <QIcon>
                #include <QDebug>
                
                #include "customwidgetclass.h"
                
                class MainWindow : public QMainWindow
                {
                    Q_OBJECT
                
                public:
                    explicit MainWindow(QWidget *parent = nullptr);
                    ~MainWindow();
                
                private slots:
                    void buttonClick();
                
                private:
                    void setup();
                
                    QDialog *dialog = new QDialog(this);
                
                    customWidgetClass *CWC;
                
                
                    QPushButton *button;
                
                    QVBoxLayout *vLayout;
                    QWidget *mainWidget;
                };
                
                #endif // MAINWINDOW_H
                
                

                MainWindow.cpp

                #include "mainwindow.h"
                #include "ui_mainwindow.h"
                
                MainWindow::MainWindow(QWidget *parent) :
                    QMainWindow(parent)
                {
                    setup();
                    CWC = new customWidgetClass();
                    connect(button, &QPushButton::released, this, &MainWindow::buttonClick);
                }
                
                MainWindow::~MainWindow()
                {
                }
                
                void MainWindow::setup(){
                    button = new QPushButton();
                    button->setText("Press me");
                
                    vLayout = new QVBoxLayout();
                    vLayout->addWidget(button);
                    vLayout->addWidget(CWC);
                
                    mainWidget = new QWidget();
                    setCentralWidget(mainWidget);
                    mainWidget->setLayout(vLayout);
                }
                
                void MainWindow::buttonClick(){
                }
                
                

                CustomWidgeClass.h

                #ifndef CUSTOMWIDGETCLASS_H
                #define CUSTOMWIDGETCLASS_H
                
                #include <QWidget>
                #include <QDialog>
                #include <QPushButton>
                #include <QHBoxLayout>
                #include <QLabel>
                #include <QDebug>
                
                class customWidgetClass
                {
                public:
                    customWidgetClass();
                
                private:
                    void setup();
                
                    QLabel *label;
                    QLabel *label2;
                    QPushButton *button;
                    QHBoxLayout *hLayout;
                    QDialog *dialog;
                };
                
                #endif // CUSTOMWIDGETCLASS_H
                
                

                CustomWidgetClass.cpp

                #include "customwidgetclass.h"
                
                customWidgetClass::customWidgetClass()
                {
                    setup();
                }
                
                void customWidgetClass::setup(){
                    label = new QLabel();
                    label->setText("Label1");
                
                    label2 = new QLabel();
                    label2->setText("Label2");
                
                    button = new QPushButton();
                    button->setText("custom button");
                
                    hLayout = new QHBoxLayout();
                    hLayout->addWidget(label);
                    hLayout->addWidget(button);
                    hLayout->addWidget(label2);
                
                    dialog = new QDialog();
                    dialog->setLayout(hLayout);
                }
                
                

                In this project, I do get an error; in MainWindow.cpp, *vLayout->addWidget(CWC); CANNOT INITIALIZE A PARAMETER OF TYPE 'QWidget *' WITH AN LVALUE OF TYPE 'customWidgetClass '

                1 Reply Last reply
                0
                • mrjjM Offline
                  mrjjM Offline
                  mrjj
                  Lifetime Qt Champion
                  wrote on 25 Feb 2019, 14:40 last edited by mrjj
                  #8

                  @Burke212 said in Manually Implementing Custom Widget in MainWindow:

                  class customWidgetClass

                  Well its not a QWidget as you don't inherited from it. :)
                  like
                  class customWidgetClass : public QWidget

                  so layout wont accept it.
                  It only likes QWidget subclasses.

                  B 1 Reply Last reply 25 Feb 2019, 19:31
                  3
                  • mrjjM mrjj
                    25 Feb 2019, 14:40

                    @Burke212 said in Manually Implementing Custom Widget in MainWindow:

                    class customWidgetClass

                    Well its not a QWidget as you don't inherited from it. :)
                    like
                    class customWidgetClass : public QWidget

                    so layout wont accept it.
                    It only likes QWidget subclasses.

                    B Offline
                    B Offline
                    Burke212
                    wrote on 25 Feb 2019, 19:31 last edited by
                    #9

                    @mrjj

                    You are correct. I can't believe I missed that!

                    However, after making the changes, the app still doesn't display any of the elements from customWidetClass. Only the button from MainWindow displays.

                    I've added the code to inherit from the QWidget class in customWidgetClass, and added *QWidget parent = nullptr to the constructor.

                    1 Reply Last reply
                    0
                    • M Offline
                      M Offline
                      mpergand
                      wrote on 25 Feb 2019, 19:49 last edited by
                      #10
                      dialog = new QDialog();
                      dialog->setLayout(hLayout);
                      

                      What is that ?

                      B 1 Reply Last reply 26 Feb 2019, 13:04
                      2
                      • M mpergand
                        25 Feb 2019, 19:49
                        dialog = new QDialog();
                        dialog->setLayout(hLayout);
                        

                        What is that ?

                        B Offline
                        B Offline
                        Burke212
                        wrote on 26 Feb 2019, 13:04 last edited by
                        #11

                        @mpergand

                        As I understand it, custom widgets use a widget, dialog, or other element to sort of group all of the elements together. So I'm using dialog to group the hLayout, button, label, & label2 in the custom widget class.

                        Is that not the way to do things?

                        J.HilkJ 1 Reply Last reply 26 Feb 2019, 13:23
                        0
                        • M Offline
                          M Offline
                          mpergand
                          wrote on 26 Feb 2019, 13:19 last edited by mpergand
                          #12
                          dialog->show();
                          

                          Add this and you will see your buttons :)

                          I guess that's not what you want, right ?
                          Why do you create a Dialog ?
                          if you want the buttons to be in your custom widget, simply put the layout in it, that's all !

                          Anyway, your code is wrong:

                          setup();
                          CWC = new customWidgetClass();
                          connect(button, &QPushButton::released, this, &MainWindow::buttonClick);
                          

                          in setup your add CWC in the layout, but you instantiate it later !
                          Should crash for sure ;)

                          1 Reply Last reply
                          1
                          • B Burke212
                            26 Feb 2019, 13:04

                            @mpergand

                            As I understand it, custom widgets use a widget, dialog, or other element to sort of group all of the elements together. So I'm using dialog to group the hLayout, button, label, & label2 in the custom widget class.

                            Is that not the way to do things?

                            J.HilkJ Offline
                            J.HilkJ Offline
                            J.Hilk
                            Moderators
                            wrote on 26 Feb 2019, 13:23 last edited by
                            #13

                            @Burke212
                            what @mpergand meant and rightfully pointed out, you create a new QDialog and sign the layout to that.
                            But that is not what you want to do!

                            this should do what you want it to:

                            void customWidgetClass::setup(){
                                label = new QLabel();
                                label->setText("Label1");
                            
                                label2 = new QLabel();
                                label2->setText("Label2");
                            
                                button = new QPushButton();
                                button->setText("custom button");
                            
                                hLayout = new QHBoxLayout();
                                hLayout->addWidget(label);
                                hLayout->addWidget(button);
                                hLayout->addWidget(label2);
                            
                                setLayout(hLayout); // assigns the layout to your newly created object derived of QWidget I presume.
                            }
                            

                            Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                            Q: What's that?
                            A: It's blue light.
                            Q: What does it do?
                            A: It turns blue.

                            1 Reply Last reply
                            2
                            • B Offline
                              B Offline
                              Burke212
                              wrote on 26 Feb 2019, 14:02 last edited by
                              #14

                              @mpergand @J-Hilk

                              That's interesting. I had always thought that custom widgets needed an element to group the class together.

                              Now, when I make your suggested changes, the output says "QLayout: Cannot add a null widget to QVBoxLayout".

                              What would cause CWC to return as a null widget?

                              J.HilkJ 1 Reply Last reply 26 Feb 2019, 14:06
                              0
                              • B Burke212
                                26 Feb 2019, 14:02

                                @mpergand @J-Hilk

                                That's interesting. I had always thought that custom widgets needed an element to group the class together.

                                Now, when I make your suggested changes, the output says "QLayout: Cannot add a null widget to QVBoxLayout".

                                What would cause CWC to return as a null widget?

                                J.HilkJ Offline
                                J.HilkJ Offline
                                J.Hilk
                                Moderators
                                wrote on 26 Feb 2019, 14:06 last edited by
                                #15

                                @Burke212

                                change

                                setup();
                                CWC = new customWidgetClass();
                                

                                to

                                CWC = new customWidgetClass();
                                setup();
                                

                                hopefully you did initialize CWC as nullptr, otherwise that would have crashed in release mode 10/10 times.


                                Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                                Q: What's that?
                                A: It's blue light.
                                Q: What does it do?
                                A: It turns blue.

                                1 Reply Last reply
                                3

                                3/15

                                14 Feb 2019, 16:11

                                topic:navigator.unread, 12
                                • Login

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