Qt Basics and Selection of Base class
-
Hi All ,
I generally get confused about selecting the type of base class; whether to select is to be of QMainWindow, QWidget, or QDialog.
See the following example:
main.cpp
#include "mylayout.h" #include <QApplication> int main(int argc, char *argv[]) { QApplication a(argc, argv); myLayout w; //w.show(); return a.exec(); }
mainLayout.h
#ifndef MYLAYOUT_H #define MYLAYOUT_H #include <QMainWindow> class myLayout : public QMainWindow { Q_OBJECT public: myLayout(QWidget *parent = nullptr); ~myLayout(); }; #endif // MYLAYOUT_H
mainLayout.cpp
#include "mylayout.h" #include <QWidget> #include <QLabel> #include <QVBoxLayout> #include <QHBoxLayout> #include <QSpinBox> myLayout::myLayout(QWidget *parent) : QMainWindow(parent) { .................................................................. .................................................................. ....................................... ............................... ........................... ..................... .............. ........ ...... ..... ... .. . } myLayout::~myLayout() { }
In
main.cpp
, I had to commentw.show
as I was getting an extra blank window and the implementation ofmainlayout.cpp
was shown in another window. So, I commented this redundant window.In the above case, the base class is
QMainWindow
. What differences will I get ,if I select the base class to beQWidget
, apart from the advantages of the respective class properties and functions? -
Hi All ,
I generally get confused about selecting the type of base class; whether to select is to be of QMainWindow, QWidget, or QDialog.
See the following example:
main.cpp
#include "mylayout.h" #include <QApplication> int main(int argc, char *argv[]) { QApplication a(argc, argv); myLayout w; //w.show(); return a.exec(); }
mainLayout.h
#ifndef MYLAYOUT_H #define MYLAYOUT_H #include <QMainWindow> class myLayout : public QMainWindow { Q_OBJECT public: myLayout(QWidget *parent = nullptr); ~myLayout(); }; #endif // MYLAYOUT_H
mainLayout.cpp
#include "mylayout.h" #include <QWidget> #include <QLabel> #include <QVBoxLayout> #include <QHBoxLayout> #include <QSpinBox> myLayout::myLayout(QWidget *parent) : QMainWindow(parent) { .................................................................. .................................................................. ....................................... ............................... ........................... ..................... .............. ........ ...... ..... ... .. . } myLayout::~myLayout() { }
In
main.cpp
, I had to commentw.show
as I was getting an extra blank window and the implementation ofmainlayout.cpp
was shown in another window. So, I commented this redundant window.In the above case, the base class is
QMainWindow
. What differences will I get ,if I select the base class to beQWidget
, apart from the advantages of the respective class properties and functions?@Swati777999 said in Qt Basics and Selection of Base class:
I generally get confused about selecting the type of base class; whether to select is to be of QMainWindow, QWidget, or QDialog.
QWidget is the base class of QMainWindow and QDialog. That means:
- QMainWindow can do everything that QWidget can do. It adds features like a menu bar, status bar, and dockable widgets.
- QDialog can do everything that QWidget can do. It is designed for temporary pop-up windows.
So, if you want a temporary window, use QDialog. If you want a menu bar or status bar or dockable widgets, use QMainWindow. If you don't want any of these, use QWidget.
I was getting an extra blank window and the implementation of
mainlayout.cpp
was shown in another window.That means your code in
mainlayout.cpp
has errors. You should fix that.You should NOT comment out
w.show()
! -
@Swati777999 said in Qt Basics and Selection of Base class:
I generally get confused about selecting the type of base class; whether to select is to be of QMainWindow, QWidget, or QDialog.
QWidget is the base class of QMainWindow and QDialog. That means:
- QMainWindow can do everything that QWidget can do. It adds features like a menu bar, status bar, and dockable widgets.
- QDialog can do everything that QWidget can do. It is designed for temporary pop-up windows.
So, if you want a temporary window, use QDialog. If you want a menu bar or status bar or dockable widgets, use QMainWindow. If you don't want any of these, use QWidget.
I was getting an extra blank window and the implementation of
mainlayout.cpp
was shown in another window.That means your code in
mainlayout.cpp
has errors. You should fix that.You should NOT comment out
w.show()
!@JKSH said in Qt Basics and Selection of Base class:
That means your code in
mainlayout.cpp
has errors. You should fix that.
You should NOT comment outw.show()
!I now have uncommented
w.show()
.CASE 1 - when the base class is
QMainWindow
mainLayout.cpp
#include "mylayout.h" #include <QWidget> #include <QLabel> #include <QVBoxLayout> #include <QHBoxLayout> #include <QSpinBox> myLayout::myLayout(QWidget *parent) : QMainWindow(parent) { QWidget *primWin= new QWidget(); //Primary Window .................................................................. ....................................... ............................... ........................... ..................... .............. ........ ...... ..... ... primWin->setWindowTitle("My Layout"); primWin->show(); ..................................................LINE-1 } myLayout::~myLayout() { }
main.cpp
#include "mylayout.h" #include <QApplication> int main(int argc, char *argv[]) { QApplication a(argc, argv); myLayout w; w.show();.....................................LINE -2 return a.exec(); }
RESULT
:-From the above code, I am getting 2 windows ; one corresponding to
LINE-1
having the titleMy Layout
with desired implementation resulting frommainlayout.cpp
; second one corresponding toLINE-2
having title as the default title of the projectmainlayout
which is blank.CASE 2 - When the base class is
QWidget
CASE 3 - When the base class is
QDialog
For both cases , 2 and 3 , the result obtained is the same as CASE 1 .
I want to get only one window ;that is window-1.
How can I achieve that without commenting
w.show()
? -
@JKSH said in Qt Basics and Selection of Base class:
That means your code in
mainlayout.cpp
has errors. You should fix that.
You should NOT comment outw.show()
!I now have uncommented
w.show()
.CASE 1 - when the base class is
QMainWindow
mainLayout.cpp
#include "mylayout.h" #include <QWidget> #include <QLabel> #include <QVBoxLayout> #include <QHBoxLayout> #include <QSpinBox> myLayout::myLayout(QWidget *parent) : QMainWindow(parent) { QWidget *primWin= new QWidget(); //Primary Window .................................................................. ....................................... ............................... ........................... ..................... .............. ........ ...... ..... ... primWin->setWindowTitle("My Layout"); primWin->show(); ..................................................LINE-1 } myLayout::~myLayout() { }
main.cpp
#include "mylayout.h" #include <QApplication> int main(int argc, char *argv[]) { QApplication a(argc, argv); myLayout w; w.show();.....................................LINE -2 return a.exec(); }
RESULT
:-From the above code, I am getting 2 windows ; one corresponding to
LINE-1
having the titleMy Layout
with desired implementation resulting frommainlayout.cpp
; second one corresponding toLINE-2
having title as the default title of the projectmainlayout
which is blank.CASE 2 - When the base class is
QWidget
CASE 3 - When the base class is
QDialog
For both cases , 2 and 3 , the result obtained is the same as CASE 1 .
I want to get only one window ;that is window-1.
How can I achieve that without commenting
w.show()
?@Swati777999 said in Qt Basics and Selection of Base class:
From the above code, I am getting 2 windows
Of course you get two windows, because you're creating two!
myLayout is already a window - why do you create another one in its constructor?
If you create a QWidget (or a derived class) without a parent and show it it becomes a window (as explained in documentation).
RemoveQWidget *primWin= new QWidget();
-
@Swati777999 said in Qt Basics and Selection of Base class:
How can I achieve that without commenting w.show() ?
By not calling show on the QWidget you created in the myLayout constructor. This widget should, I expect, be set as the central widget of the QMainWindow (QMainWindow::setCentralWidget)
-
@Swati777999 said in Qt Basics and Selection of Base class:
How can I achieve that without commenting w.show() ?
By not calling show on the QWidget you created in the myLayout constructor. This widget should, I expect, be set as the central widget of the QMainWindow (QMainWindow::setCentralWidget)
@ChrisW67 said in Qt Basics and Selection of Base class:
By not calling show on the QWidget you created in the myLayout constructor. This widget should, I expect, be set as the central widget of the QMainWindow (QMainWindow::setCentralWidget)
@jsulm Have a look at it.
In the mainlayout constructor , I made following changes:
QWidget *primWin = new QWidget(this);
Then added ,
QMainWindow::setCentralWidget(primaryWindow);
and removedshow()
function forprimWin
.Yes, it worked for me.
Thanks. :) -
@ChrisW67 said in Qt Basics and Selection of Base class:
By not calling show on the QWidget you created in the myLayout constructor. This widget should, I expect, be set as the central widget of the QMainWindow (QMainWindow::setCentralWidget)
@jsulm Have a look at it.
In the mainlayout constructor , I made following changes:
QWidget *primWin = new QWidget(this);
Then added ,
QMainWindow::setCentralWidget(primaryWindow);
and removedshow()
function forprimWin
.Yes, it worked for me.
Thanks. :)@Swati777999 said in Qt Basics and Selection of Base class:
Yes, it worked for me.
Thanks. :)Great!
Can you now explain why it works?
-
@Swati777999 said in Qt Basics and Selection of Base class:
I generally get confused about selecting the type of base class; whether to select is to be of QMainWindow, QWidget, or QDialog.
QWidget is the base class of QMainWindow and QDialog. That means:
- QMainWindow can do everything that QWidget can do. It adds features like a menu bar, status bar, and dockable widgets.
- QDialog can do everything that QWidget can do. It is designed for temporary pop-up windows.
So, if you want a temporary window, use QDialog. If you want a menu bar or status bar or dockable widgets, use QMainWindow. If you don't want any of these, use QWidget.
I was getting an extra blank window and the implementation of
mainlayout.cpp
was shown in another window.That means your code in
mainlayout.cpp
has errors. You should fix that.You should NOT comment out
w.show()
!@JKSH
If I take base class to be QDialog, I noticed following occurences:-
I get a question mark (instead of minimize option)in the title bar of the main window .
-
The content of the widget occupies a small portion of the window and no longer occupies the entire window when the window is dragged to a larger area.
Can you explain me the reason?
-
I assume you mean the top-level window is a QDialog.
With regard to the minimize button, what you are describing is the default window decorations of a dialog on your platform.
QMainWIndow manages its centralWidget() so it always occupies the entire space inside the window after menu bars, toolbars, status bar and potential docked widgets are taken out. This is an extension of the standard QWidget behaviour.
QDialog uses a layout manager to control its contents; this is the standard QWidget mechanism. If you do not apply a layout with the content widgets added to it then the content widgets are uncontrolled. They will appear on top of and inside their parent QWidget but will not be size managed by it.
-
This post is deleted!
-
@Swati777999 said in Qt Basics and Selection of Base class:
Yes, it worked for me.
Thanks. :)Great!
Can you now explain why it works?
@JKSH said in Qt Basics and Selection of Base class:
@Swati777999 said in Qt Basics and Selection of Base class:
Yes, it worked for me.
Thanks. :)Great!
Can you now explain why it works?
this points to the parent object of the constructor. To display the contents of
primWin
widget , it has to be set as the central widget of the Qmainwindow.