Unsolved 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.cppclass 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
-
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
Then
Then name it. Use a good name. MyWidget is not good :
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
thenOk 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 youOk 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 -
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 ......
-
-
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 .. -
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 youFirst 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 ? -
@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.
-
Yes, I can do that.
Yes, I can do that.
If I encounter a problem later. I will leave a message ...
Thnks