Custom widget not showing
-
Hi,
I am new to Qt and I am trying to create some custom widgets for my application.Here is my issue:
I have the following main :
QApplication a(argc, argv); QtWidgetsApplication w; QWidget* central_widget = new QWidget(&w); QListWidget* listWidget = new QListWidget(central_widget); listWidget->setObjectName(QString::fromUtf8("listWidget")); listWidget->setGeometry(QRect(20, 20, 256, 192)); w.setCentralWidget(central_widget); w.show(); return a.exec();
This works fine. However if I create a basic custom widget of my own that inherits from QListWidget like this :
class DeviceList : public QListWidget { public: DeviceList(QObject* parent) {} };
and replace
QListWidget* listWidget = new QListWidget(central_widget);
with
DeviceList* listWidget = new DeviceList(central_widget);
then my widget will not show and the main window is empty.
If however I make the custom widget itself the central onew.setCentralWidget(listWidget);
then it will actually appear.
I am not sure what I am doing wrong but I feel like I am missing something fundamental here when it comes to custom widget inheritance.
-
Hi,
I am new to Qt and I am trying to create some custom widgets for my application.Here is my issue:
I have the following main :
QApplication a(argc, argv); QtWidgetsApplication w; QWidget* central_widget = new QWidget(&w); QListWidget* listWidget = new QListWidget(central_widget); listWidget->setObjectName(QString::fromUtf8("listWidget")); listWidget->setGeometry(QRect(20, 20, 256, 192)); w.setCentralWidget(central_widget); w.show(); return a.exec();
This works fine. However if I create a basic custom widget of my own that inherits from QListWidget like this :
class DeviceList : public QListWidget { public: DeviceList(QObject* parent) {} };
and replace
QListWidget* listWidget = new QListWidget(central_widget);
with
DeviceList* listWidget = new DeviceList(central_widget);
then my widget will not show and the main window is empty.
If however I make the custom widget itself the central onew.setCentralWidget(listWidget);
then it will actually appear.
I am not sure what I am doing wrong but I feel like I am missing something fundamental here when it comes to custom widget inheritance.
@eyrkon said in Custom widget not showing:
DeviceList(QObject* parent) {}
You forgot to call QListWidget constructor here passing it the parent.
-
@eyrkon said in Custom widget not showing:
DeviceList(QObject* parent) {}
You forgot to call QListWidget constructor here passing it the parent.
-
-
@eyrkon said in Custom widget not showing:
DeviceList(QObject* parent) {}
You forgot to call QListWidget constructor here passing it the parent.
-
@jsulm
I assume C++ always calls the base constructor which takes no parameters if you do not specify?
And if there is no base with no parameters then it's an error? -
@jsulm
I know, that was not the question! :) The question (well, two questions) is about C++ behaviour as I phrased it, please? :) Don't make me go ask ChatGPT, she & I have had some disagreements about facts... !@JonB said in Custom widget not showing:
The question (well, two questions) is about C++ behaviour as I phrased it, please?
I'm not @jsulm , but you are right. Since most
QWidget
-based classes havenullptr
as default arg forparent
in their c'tor, it's basically empty. And aQObject
/QWidget
with noparent
is "standalone" or a toplevel-widget and needswidget.show()
to be visible.
When there would be no default and no other empty base/super class c'tor available, I think it wouldn't even compile.Don't make me go ask ChatGPT, she & I have had some disagreements about facts... !
WDYM?! Elephants do not lay eggs?!
(Source):D
-
@JonB said in Custom widget not showing:
The question (well, two questions) is about C++ behaviour as I phrased it, please?
I'm not @jsulm , but you are right. Since most
QWidget
-based classes havenullptr
as default arg forparent
in their c'tor, it's basically empty. And aQObject
/QWidget
with noparent
is "standalone" or a toplevel-widget and needswidget.show()
to be visible.
When there would be no default and no other empty base/super class c'tor available, I think it wouldn't even compile.Don't make me go ask ChatGPT, she & I have had some disagreements about facts... !
WDYM?! Elephants do not lay eggs?!
(Source):D