Two classes in the same mainwindow.cpp



  • Hello everyone.
    I am a beginner in C ++ and I am confronted with a particular situation. And I did not find the answer in my classes.
    I have a class
    mainwindow.cpp

    class mainwindow: public QMainWindow
    {
    }
    

    I want to insert a Widget in my program. I declare it in the previous class as

    class mainwindow: public QMainWindow
         public:
         mainwindow ();
         QWidget * Fenetre;
    

    Or I declare it as another class in the mainwindow.cpp

    class mainwindow: public QWidget
    {
    }
    

    What is the normal procedure?

    Thanks


  • Lifetime Qt Champion

    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.



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



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


  • Lifetime Qt Champion

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


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



  • 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 ..


  • Lifetime Qt Champion

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



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



  • 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.....



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


  • Lifetime Qt Champion

    @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



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


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.