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.
  • 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
        • M Offline
          M 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
          • M 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 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 Offline
                    J 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 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 Offline
                        J 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

                        14/15

                        26 Feb 2019, 14:02

                        • Login

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