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. Two classes in the same mainwindow.cpp
Forum Updated to NodeBB v4.3 + New Features

Two classes in the same mainwindow.cpp

Scheduled Pinned Locked Moved Unsolved General and Desktop
13 Posts 3 Posters 1.4k 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.
  • mrjjM Offline
    mrjjM Offline
    mrjj
    Lifetime Qt Champion
    wrote on last edited by mrjj
    #2

    Hi
    Its best with each widget in its own h and cpp file. That way its easier to reuse and
    avoid Mainwindow.cpp/.h to become very large and bloated.
    Creator can actually create the base layout of a new Widget with cpp and h file.

    Select File -> New project and File
    then
    alt text

    Then
    alt text

    Then name it. Use a good name. MyWidget is not good :
    alt text

    then just next and next.

    You now have a new Widget with UI attached you can place other widgets on.
    The .h and cpp and ui file is added to the project.

    Nafab213N 1 Reply Last reply
    3
    • mrjjM mrjj

      Hi
      Its best with each widget in its own h and cpp file. That way its easier to reuse and
      avoid Mainwindow.cpp/.h to become very large and bloated.
      Creator can actually create the base layout of a new Widget with cpp and h file.

      Select File -> New project and File
      then
      alt text

      Then
      alt text

      Then name it. Use a good name. MyWidget is not good :
      alt text

      then just next and next.

      You now have a new Widget with UI attached you can place other widgets on.
      The .h and cpp and ui file is added to the project.

      Nafab213N Offline
      Nafab213N Offline
      Nafab213
      wrote on last edited by
      #3

      @mrjj said in Two classes in the same mainwindow.cpp:

      Hi
      Its best with each widget in its own h and cpp file. That way its easier to reuse and
      avoid Mainwindow.cpp/.h to become very large and bloated.
      Creator can actually create the base layout of a new Widget with cpp and h file.
      Select File -> New project and File
      then

      Ok I understood but I would like my Widget to be the centralwidget of my application.
      How can I connect the Mywidget class with my Qmainwindow class. ?
      Introduce me a sample code to better understand ..
      Thank you

      mrjjM 1 Reply Last reply
      0
      • Nafab213N Offline
        Nafab213N Offline
        Nafab213
        wrote on last edited by
        #4

        @Nafab213 said in Two classes in the same mainwindow.cpp:

        Ok I understood but I would like my Widget to be the centralwidget of my application.
        How can I connect the Mywidget class with my Qmainwindow class. ?
        Introduce me a sample code to better understand ..
        Thank you

        Ok I understood but I would like my Widget to be the centralwidget of my application.
        How can I connect the Mywidget class with my Qmainwindow class. ?
        Introduce me a sample code to better understand ..
        Thank you

        Pablo J. RoginaP 1 Reply Last reply
        1
        • Nafab213N Nafab213

          @mrjj said in Two classes in the same mainwindow.cpp:

          Hi
          Its best with each widget in its own h and cpp file. That way its easier to reuse and
          avoid Mainwindow.cpp/.h to become very large and bloated.
          Creator can actually create the base layout of a new Widget with cpp and h file.
          Select File -> New project and File
          then

          Ok I understood but I would like my Widget to be the centralwidget of my application.
          How can I connect the Mywidget class with my Qmainwindow class. ?
          Introduce me a sample code to better understand ..
          Thank you

          mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by
          #5

          @Nafab213

          Lets say for the sake of it, you did name it MyWidget
          So in MainWindow.cpp
          It would be like

          #include "mywidget.h" // include the new widget
          
          MainWindow::MainWindow(QWidget *parent)
              : QMainWindow(parent), ui(new Ui::MainWindow)
          {
              ui->setupUi(this);
              
              setCentralWidget( new MyWidget(this) ); // create an instance and use as central
           ......
          
          1 Reply Last reply
          3
          • Nafab213N Nafab213

            @Nafab213 said in Two classes in the same mainwindow.cpp:

            Ok I understood but I would like my Widget to be the centralwidget of my application.
            How can I connect the Mywidget class with my Qmainwindow class. ?
            Introduce me a sample code to better understand ..
            Thank you

            Ok I understood but I would like my Widget to be the centralwidget of my application.
            How can I connect the Mywidget class with my Qmainwindow class. ?
            Introduce me a sample code to better understand ..
            Thank you

            Pablo J. RoginaP Offline
            Pablo J. RoginaP Offline
            Pablo J. Rogina
            wrote on last edited by
            #6

            @Nafab213 in addition to @mrjj post, documentation is your friend. See this example for instance.

            Upvote the answer(s) that helped you solve the issue
            Use "Topic Tools" button to mark your post as Solved
            Add screenshots via postimage.org
            Don't ask support requests via chat/PM. Please use the forum so others can benefit from the solution in the future

            1 Reply Last reply
            4
            • Nafab213N Offline
              Nafab213N Offline
              Nafab213
              wrote on last edited by
              #7

              Exactly the example does not help me because they used only one: Mainwindow class without Qwidget for the Centralwidget. When I create the Qt class, the code is incomprehensible to me.
              I would like to know if I can use a new C ++ class (the .h and .cpp file) to do the same thing.
              And it will be at the beginning:

              class Mywidget : public QWidget
              

              If so, what will be the connection code to use as Centralwidget of my mainwindow?
              Thank you for the patience ..

              mrjjM Pablo J. RoginaP 2 Replies Last reply
              0
              • Nafab213N Nafab213

                Exactly the example does not help me because they used only one: Mainwindow class without Qwidget for the Centralwidget. When I create the Qt class, the code is incomprehensible to me.
                I would like to know if I can use a new C ++ class (the .h and .cpp file) to do the same thing.
                And it will be at the beginning:

                class Mywidget : public QWidget
                

                If so, what will be the connection code to use as Centralwidget of my mainwindow?
                Thank you for the patience ..

                mrjjM Offline
                mrjjM Offline
                mrjj
                Lifetime Qt Champion
                wrote on last edited by mrjj
                #8

                @Nafab213

                Hi
                You can do as i shown higher up to have your own widget as central.
                I assume you want to add features/code to it so its more than a normal QWidget.
                ( like moue Events or own paint function)
                Else it would make little sense to subclass it.

                The key is the setCentralWidget to set it as central.

                1 Reply Last reply
                0
                • Nafab213N Nafab213

                  Exactly the example does not help me because they used only one: Mainwindow class without Qwidget for the Centralwidget. When I create the Qt class, the code is incomprehensible to me.
                  I would like to know if I can use a new C ++ class (the .h and .cpp file) to do the same thing.
                  And it will be at the beginning:

                  class Mywidget : public QWidget
                  

                  If so, what will be the connection code to use as Centralwidget of my mainwindow?
                  Thank you for the patience ..

                  Pablo J. RoginaP Offline
                  Pablo J. RoginaP Offline
                  Pablo J. Rogina
                  wrote on last edited by
                  #9

                  @Nafab213 said in Two classes in the same mainwindow.cpp:

                  Exactly the example does not help me because they used only one: Mainwindow class without Qwidget for the Centralwidget

                  Are you talking to @mrjj example or the one I pointed out? If the later, you may need to take a look again at the code:

                  MainWindow::MainWindow()
                      : textEdit(new QPlainTextEdit)
                  {
                      setCentralWidget(textEdit);
                  
                      createActions();
                      ...
                  

                  and QPlainTextEdit is, well, a QWidget after all... (QWidget -> QFrame -> QAbstractScrollArea -> QPlainTextEdit)

                  Upvote the answer(s) that helped you solve the issue
                  Use "Topic Tools" button to mark your post as Solved
                  Add screenshots via postimage.org
                  Don't ask support requests via chat/PM. Please use the forum so others can benefit from the solution in the future

                  Nafab213N 1 Reply Last reply
                  0
                  • Nafab213N Offline
                    Nafab213N Offline
                    Nafab213
                    wrote on last edited by
                    #10

                    I'm afraid of getting lost if I use the Widget. I do not fully master C ++ yet.
                    I want to learn. You will give me a hand .. Thank you

                    First stupid question:
                    Where do I start writing in the.cpp file ?

                    #include "wigcen.h"
                    #include "ui_wigcen.h"
                    
                    wigcen :: wigcen (QWidget * parent):
                         QWidget (parent)
                         ui (new Ui :: wigcen)
                    {
                         ui-> setupUi (this); //   At this level  ?
                    }
                    
                    wigcen :: ~ wigcen ()
                    {
                         delete ui; //   At this level  ?
                    }
                    or
                    wigcen :: wigcen ()
                    {
                    boutoncene = new QPushButton("C E N - P");
                            boutoncene->move(40, 50);
                            boutoncene->setFont(QFont("Times New Roman", 16)); 
                    and more
                    }
                    

                    Thanks.....

                    1 Reply Last reply
                    0
                    • Pablo J. RoginaP Pablo J. Rogina

                      @Nafab213 said in Two classes in the same mainwindow.cpp:

                      Exactly the example does not help me because they used only one: Mainwindow class without Qwidget for the Centralwidget

                      Are you talking to @mrjj example or the one I pointed out? If the later, you may need to take a look again at the code:

                      MainWindow::MainWindow()
                          : textEdit(new QPlainTextEdit)
                      {
                          setCentralWidget(textEdit);
                      
                          createActions();
                          ...
                      

                      and QPlainTextEdit is, well, a QWidget after all... (QWidget -> QFrame -> QAbstractScrollArea -> QPlainTextEdit)

                      Nafab213N Offline
                      Nafab213N Offline
                      Nafab213
                      wrote on last edited by
                      #11

                      @Pablo-J.-Rogina I'm talking about your example. They used Qplaintextedit as the centralwidget. This is my problem the Central Widget.
                      I would like an empty window like Widgetcentral.
                      I will add two buttons in the center of the window after.
                      How to do it ?

                      mrjjM 1 Reply Last reply
                      0
                      • Nafab213N Nafab213

                        @Pablo-J.-Rogina I'm talking about your example. They used Qplaintextedit as the centralwidget. This is my problem the Central Widget.
                        I would like an empty window like Widgetcentral.
                        I will add two buttons in the center of the window after.
                        How to do it ?

                        mrjjM Offline
                        mrjjM Offline
                        mrjj
                        Lifetime Qt Champion
                        wrote on last edited by
                        #12

                        @Nafab213
                        Hi
                        You want to do this from code ?
                        The default GUI project created by creator is ready to go with black widget as central, read for you to
                        add buttons to it by simply dragging it from right to the empty central.
                        alt text

                        1 Reply Last reply
                        0
                        • Nafab213N Offline
                          Nafab213N Offline
                          Nafab213
                          wrote on last edited by
                          #13

                          Yes, I can do that.
                          Yes, I can do that.
                          If I encounter a problem later. I will leave a message ...
                          Thnks

                          1 Reply Last reply
                          0

                          • Login

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