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. Add widget from custom class to MainWindow
Forum Updated to NodeBB v4.3 + New Features

Add widget from custom class to MainWindow

Scheduled Pinned Locked Moved Solved General and Desktop
19 Posts 2 Posters 2.2k Views 2 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.
  • T Offline
    T Offline
    Tamfub
    wrote on 4 Oct 2020, 11:24 last edited by Tamfub 10 Apr 2020, 11:50
    #1

    Hi everyone.

    I'm planning to create a form into MainWindow, and I need to add a field for an IPv4 address.
    I've been looking for a smart way to handle this field and came across this solution, that seems suitable to me.

    In the solution, the user creates a custom class IPCtrl that inherits from QFrame and keeps

    QLineEdit *(m_pLineEdit[QTUTL_IP_SIZE])
    

    i.e. a vector of 4 QLineEdits, one for each of the fields of the IP address.

    I created a basic QT widget application (i.e. with main.cpp and mainwindow.h/.cpp/.ui), and created the new class.

    Now, my goal is to show the QLineEdits inside my MainWindow. How can I do this? I tried to:

    • add a private IPCtrl attribute in MainWindow, IPCtrl ip_field

    • set it via a setter (thus requiring the copy constructor for IPCtrl)

    • show it via ip_field.show()

    But it results in two detached windows: MainWindow and ip_address.

    I'm up for any clarification!

    1 Reply Last reply
    0
    • M Offline
      M Offline
      mrjj
      Lifetime Qt Champion
      wrote on 4 Oct 2020, 12:15 last edited by
      #2

      Hi
      The reason that ip_address becomes a window is that you dont give it a parent.
      So to be inside MainWindow it needs to have that as a parent.

      Normally you can do like this

      in .h ( in mainwindow )
      IPCtrl *ip_field;

      in. cpp
      ip_field = new IPCtrl(this);

      and then it should be in Mainwinow.

      Do note, that normally you use a layout in MainWidnow and you insert ip_field to such layout.

      T 1 Reply Last reply 4 Oct 2020, 15:51
      3
      • M mrjj
        4 Oct 2020, 12:15

        Hi
        The reason that ip_address becomes a window is that you dont give it a parent.
        So to be inside MainWindow it needs to have that as a parent.

        Normally you can do like this

        in .h ( in mainwindow )
        IPCtrl *ip_field;

        in. cpp
        ip_field = new IPCtrl(this);

        and then it should be in Mainwinow.

        Do note, that normally you use a layout in MainWidnow and you insert ip_field to such layout.

        T Offline
        T Offline
        Tamfub
        wrote on 4 Oct 2020, 15:51 last edited by
        #3

        @mrjj Got it worked now, but an issue persists: now I have two ip_field's in my MainWindow, and I can't figure out why.

        This is my code:

        mainwindow.h

        class MainWindow : public QMainWindow
        {
            Q_OBJECT
        
        public:
            explicit MainWindow(QWidget *parent = nullptr);
            ~MainWindow();
            IPCtrl ip_field;
        
        
        private:
            Ui::MainWindow *ui;
        };
        

        mainwindow.cpp

        MainWindow::MainWindow(QWidget *parent) :
            QMainWindow(parent),
            ui(new Ui::MainWindow)
        {
            ui->setupUi(this);
            ip_field = new IPCtrl(this);
            ui->formLayout->addRow("IP Address", ip_field.window());
            ip_field.show();
        
        }
        

        mainwindow.ui
        Screenshot_2.png

        This is the result:
        Screenshot_3.png

        Only the ip_field at top calls the IPCtrl methods correctly.

        1 Reply Last reply
        0
        • M Offline
          M Offline
          mrjj
          Lifetime Qt Champion
          wrote on 4 Oct 2020, 15:55 last edited by mrjj 10 Apr 2020, 15:57
          #4

          Hi
          It seems that
          ip_field.window()
          returns something you insert into layout.

          So you both have the one you new (top one) and
          I would guess one that ip_field.window() returns.

          Why are you using the .window() thing ?
          what does it do ?

          It would not accept just
          ui->formLayout->addRow("IP Address", ip_field); ??

          T 1 Reply Last reply 4 Oct 2020, 16:03
          0
          • M mrjj
            4 Oct 2020, 15:55

            Hi
            It seems that
            ip_field.window()
            returns something you insert into layout.

            So you both have the one you new (top one) and
            I would guess one that ip_field.window() returns.

            Why are you using the .window() thing ?
            what does it do ?

            It would not accept just
            ui->formLayout->addRow("IP Address", ip_field); ??

            T Offline
            T Offline
            Tamfub
            wrote on 4 Oct 2020, 16:03 last edited by
            #5

            @mrjj No: documentation says

            void QFormLayout::addRow(QWidget *label, QWidget *field)
            Adds a new row to the bottom of this form layout, with the given label and field.
            

            so it takes a QWidget as a second parameter, and I need a way to retrieve it from ip_field.

            M 1 Reply Last reply 4 Oct 2020, 16:04
            0
            • T Tamfub
              4 Oct 2020, 16:03

              @mrjj No: documentation says

              void QFormLayout::addRow(QWidget *label, QWidget *field)
              Adds a new row to the bottom of this form layout, with the given label and field.
              

              so it takes a QWidget as a second parameter, and I need a way to retrieve it from ip_field.

              M Offline
              M Offline
              mrjj
              Lifetime Qt Champion
              wrote on 4 Oct 2020, 16:04 last edited by
              #6

              @Tamfub
              But is IPCtrl not based on QWidget ???
              like
              class IPCtrl : QWidget

              ?

              M 1 Reply Last reply 4 Oct 2020, 16:07
              0
              • M mrjj
                4 Oct 2020, 16:04

                @Tamfub
                But is IPCtrl not based on QWidget ???
                like
                class IPCtrl : QWidget

                ?

                M Offline
                M Offline
                mrjj
                Lifetime Qt Champion
                wrote on 4 Oct 2020, 16:07 last edited by
                #7

                @mrjj

                Ah saw link :)
                well its
                class IPCtrl : public QFrame

                so yes you can use that directly.

                T 1 Reply Last reply 4 Oct 2020, 16:13
                0
                • M mrjj
                  4 Oct 2020, 16:07

                  @mrjj

                  Ah saw link :)
                  well its
                  class IPCtrl : public QFrame

                  so yes you can use that directly.

                  T Offline
                  T Offline
                  Tamfub
                  wrote on 4 Oct 2020, 16:13 last edited by Tamfub 10 Apr 2020, 16:13
                  #8

                  @mrjj I know, but it complains:
                  Screenshot_4.png

                  M 1 Reply Last reply 4 Oct 2020, 16:17
                  0
                  • T Tamfub
                    4 Oct 2020, 16:13

                    @mrjj I know, but it complains:
                    Screenshot_4.png

                    M Offline
                    M Offline
                    mrjj
                    Lifetime Qt Champion
                    wrote on 4 Oct 2020, 16:17 last edited by
                    #9

                    @Tamfub
                    Hi
                    the first parameter needs to be a QLabel and u give it a char *
                    also it seems that ip_field is not pointer ?

                    T 1 Reply Last reply 4 Oct 2020, 16:24
                    0
                    • M mrjj
                      4 Oct 2020, 16:17

                      @Tamfub
                      Hi
                      the first parameter needs to be a QLabel and u give it a char *
                      also it seems that ip_field is not pointer ?

                      T Offline
                      T Offline
                      Tamfub
                      wrote on 4 Oct 2020, 16:24 last edited by Tamfub 10 Apr 2020, 16:26
                      #10

                      @mrjj Actually it is ok with the Char* in place of the QLabel, so I would put that apart for now.
                      I tried to:

                      • declare IPCtrl *ip_field (a pointer, as you suggested) in mainwindow.h

                      • write ui->formLayout->addRow("IP Address", ip_field->window()); ip_field->show(); (then with the arrows in place of the dots) in mainwindow.cpp

                      the application starts but the UI won't just appear...

                      M 1 Reply Last reply 4 Oct 2020, 16:28
                      0
                      • T Tamfub
                        4 Oct 2020, 16:24

                        @mrjj Actually it is ok with the Char* in place of the QLabel, so I would put that apart for now.
                        I tried to:

                        • declare IPCtrl *ip_field (a pointer, as you suggested) in mainwindow.h

                        • write ui->formLayout->addRow("IP Address", ip_field->window()); ip_field->show(); (then with the arrows in place of the dots) in mainwindow.cpp

                        the application starts but the UI won't just appear...

                        M Offline
                        M Offline
                        mrjj
                        Lifetime Qt Champion
                        wrote on 4 Oct 2020, 16:28 last edited by
                        #11

                        @Tamfub

                        Ok the window() part makes no sense to me.

                        Looking at the code, it just seems like a normal custom widget.
                        Did you change the code shown on SO or used it as is ?

                        T 1 Reply Last reply 4 Oct 2020, 16:30
                        0
                        • M mrjj
                          4 Oct 2020, 16:28

                          @Tamfub

                          Ok the window() part makes no sense to me.

                          Looking at the code, it just seems like a normal custom widget.
                          Did you change the code shown on SO or used it as is ?

                          T Offline
                          T Offline
                          Tamfub
                          wrote on 4 Oct 2020, 16:30 last edited by
                          #12

                          @mrjj I used it as is, no changes.

                          M 1 Reply Last reply 4 Oct 2020, 16:34
                          0
                          • T Tamfub
                            4 Oct 2020, 16:30

                            @mrjj I used it as is, no changes.

                            M Offline
                            M Offline
                            mrjj
                            Lifetime Qt Champion
                            wrote on 4 Oct 2020, 16:34 last edited by
                            #13

                            @Tamfub
                            Ok i did fast test.
                            It just compiles so not sure why you get so much grief from it :)

                            ui->formLayout->addRow("test", field);

                            alt text

                            test project.
                            https://www.dropbox.com/s/v0a5rdlkzi3igvb/IPCtrlTEst.zip?dl=0

                            1 Reply Last reply
                            2
                            • T Offline
                              T Offline
                              Tamfub
                              wrote on 4 Oct 2020, 16:39 last edited by
                              #14

                              @mrjj Ok, it worked also for me now, the error in addRow disappeares when ip_field is declared as a pointer. I did not try this XD

                              Thank you very much!

                              M 1 Reply Last reply 4 Oct 2020, 16:39
                              1
                              • T Tamfub
                                4 Oct 2020, 16:39

                                @mrjj Ok, it worked also for me now, the error in addRow disappeares when ip_field is declared as a pointer. I did not try this XD

                                Thank you very much!

                                M Offline
                                M Offline
                                mrjj
                                Lifetime Qt Champion
                                wrote on 4 Oct 2020, 16:39 last edited by
                                #15

                                @Tamfub
                                Np :)
                                yes it must be pointer OR you must use the & to take address but
                                due to how deletion works, it's best to use pointer :)

                                T 1 Reply Last reply 4 Oct 2020, 16:40
                                0
                                • M mrjj
                                  4 Oct 2020, 16:39

                                  @Tamfub
                                  Np :)
                                  yes it must be pointer OR you must use the & to take address but
                                  due to how deletion works, it's best to use pointer :)

                                  T Offline
                                  T Offline
                                  Tamfub
                                  wrote on 4 Oct 2020, 16:40 last edited by
                                  #16

                                  @mrjj Got it! Do you know why the right border of the textEdit is not visible?

                                  M 1 Reply Last reply 4 Oct 2020, 16:46
                                  0
                                  • T Tamfub
                                    4 Oct 2020, 16:40

                                    @mrjj Got it! Do you know why the right border of the textEdit is not visible?

                                    M Offline
                                    M Offline
                                    mrjj
                                    Lifetime Qt Champion
                                    wrote on 4 Oct 2020, 16:46 last edited by mrjj 10 Apr 2020, 16:47
                                    #17

                                    @Tamfub
                                    It seems the last Edit touches the QFrames border.
                                    but even if i make it HUGE, it still happens so its
                                    its seems to be due to
                                    pLayout->setContentsMargins( 0, 0, 0, 0 );
                                    so you can fix it with
                                    pLayout->setContentsMargins( 0, 0, 1, 0 );
                                    Seems not to give ill side effects.

                                    ps. in
                                    IPCtrl::IPCtrl(QWidget *parent) : QFrame(parent)

                                    T 1 Reply Last reply 4 Oct 2020, 16:49
                                    1
                                    • M mrjj
                                      4 Oct 2020, 16:46

                                      @Tamfub
                                      It seems the last Edit touches the QFrames border.
                                      but even if i make it HUGE, it still happens so its
                                      its seems to be due to
                                      pLayout->setContentsMargins( 0, 0, 0, 0 );
                                      so you can fix it with
                                      pLayout->setContentsMargins( 0, 0, 1, 0 );
                                      Seems not to give ill side effects.

                                      ps. in
                                      IPCtrl::IPCtrl(QWidget *parent) : QFrame(parent)

                                      T Offline
                                      T Offline
                                      Tamfub
                                      wrote on 4 Oct 2020, 16:49 last edited by
                                      #18

                                      @mrjj Fine, thank you again :)

                                      M 1 Reply Last reply 4 Oct 2020, 16:51
                                      0
                                      • T Tamfub
                                        4 Oct 2020, 16:49

                                        @mrjj Fine, thank you again :)

                                        M Offline
                                        M Offline
                                        mrjj
                                        Lifetime Qt Champion
                                        wrote on 4 Oct 2020, 16:51 last edited by
                                        #19

                                        @Tamfub
                                        Np.
                                        please mark as solved if you got it working :)

                                        1 Reply Last reply
                                        1

                                        1/19

                                        4 Oct 2020, 11:24

                                        • Login

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