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

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.
  • SGaistS Offline
    SGaistS Offline
    SGaist
    Lifetime Qt Champion
    wrote on 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
    1
    • SGaistS SGaist

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

      B Offline
      B Offline
      Burke212
      wrote on 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
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on 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 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 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
            3
            • mrjjM mrjj

              @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 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 last edited by
                #10
                dialog = new QDialog();
                dialog->setLayout(hLayout);
                

                What is that ?

                B 1 Reply Last reply
                2
                • M mpergand
                  dialog = new QDialog();
                  dialog->setLayout(hLayout);
                  

                  What is that ?

                  B Offline
                  B Offline
                  Burke212
                  wrote on 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
                  0
                  • M Offline
                    M Offline
                    mpergand
                    wrote on 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

                      @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 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 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
                        0
                        • B Burke212

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

                          • Login

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