Manually Implementing Custom Widget in MainWindow
-
You can use your custom widgets like any other widget. What exactly is troubling you ?
-
My hangup is that I don't know the proper way to design a custom widget with it's elements (buttons, sliders, labels...) and layouts, and add it to another widget, such as the MainWindow.
I've made custom widget classes that basically look like this:
button; vertical_layout; vertical_layout->addWidget(button); custom_widget; custom_widget->setLayout(vertical_layout);
Then create an object of that class in MainWindow, and add it to MainWindow's layout. But sometimes I've gotten strange errors, and I've always thought that this way isn't the right way.
I'm just trying to learn what is the best way to make a custom widget and add it to MainWindow.
-
You can use designer or do it by code only. That's up to you.
What strange errors do you get ?
-
I don't always get errors. Typically the widget simply doesn't display.
Maybe it would help if I include a simpler project as an example.
MainWindow.h
#ifndef MAINWINDOW_H #define MAINWINDOW_H #include <QMainWindow> #include <QDialog> #include <QPushButton> #include <QLineEdit> #include <QMessageBox> #include <QVBoxLayout> #include <QHBoxLayout> #include <QGridLayout> #include <QPixmap> #include <QIcon> #include <QDebug> #include "customwidgetclass.h" class MainWindow : public QMainWindow { Q_OBJECT public: explicit MainWindow(QWidget *parent = nullptr); ~MainWindow(); private slots: void buttonClick(); private: void setup(); QDialog *dialog = new QDialog(this); customWidgetClass *CWC; QPushButton *button; QVBoxLayout *vLayout; QWidget *mainWidget; }; #endif // MAINWINDOW_H
MainWindow.cpp
#include "mainwindow.h" #include "ui_mainwindow.h" MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) { setup(); CWC = new customWidgetClass(); connect(button, &QPushButton::released, this, &MainWindow::buttonClick); } MainWindow::~MainWindow() { } void MainWindow::setup(){ button = new QPushButton(); button->setText("Press me"); vLayout = new QVBoxLayout(); vLayout->addWidget(button); vLayout->addWidget(CWC); mainWidget = new QWidget(); setCentralWidget(mainWidget); mainWidget->setLayout(vLayout); } void MainWindow::buttonClick(){ }
CustomWidgeClass.h
#ifndef CUSTOMWIDGETCLASS_H #define CUSTOMWIDGETCLASS_H #include <QWidget> #include <QDialog> #include <QPushButton> #include <QHBoxLayout> #include <QLabel> #include <QDebug> class customWidgetClass { public: customWidgetClass(); private: void setup(); QLabel *label; QLabel *label2; QPushButton *button; QHBoxLayout *hLayout; QDialog *dialog; }; #endif // CUSTOMWIDGETCLASS_H
CustomWidgetClass.cpp
#include "customwidgetclass.h" customWidgetClass::customWidgetClass() { setup(); } void customWidgetClass::setup(){ label = new QLabel(); label->setText("Label1"); label2 = new QLabel(); label2->setText("Label2"); button = new QPushButton(); button->setText("custom button"); hLayout = new QHBoxLayout(); hLayout->addWidget(label); hLayout->addWidget(button); hLayout->addWidget(label2); dialog = new QDialog(); dialog->setLayout(hLayout); }
In this project, I do get an error; in MainWindow.cpp, *vLayout->addWidget(CWC); CANNOT INITIALIZE A PARAMETER OF TYPE 'QWidget *' WITH AN LVALUE OF TYPE 'customWidgetClass '
-
@Burke212 said in Manually Implementing Custom Widget in MainWindow:
class customWidgetClass
Well its not a QWidget as you don't inherited from it. :)
like
class customWidgetClass : public QWidgetso layout wont accept it.
It only likes QWidget subclasses. -
You are correct. I can't believe I missed that!
However, after making the changes, the app still doesn't display any of the elements from customWidetClass. Only the button from MainWindow displays.
I've added the code to inherit from the QWidget class in customWidgetClass, and added *QWidget parent = nullptr to the constructor.
-
As I understand it, custom widgets use a widget, dialog, or other element to sort of group all of the elements together. So I'm using dialog to group the hLayout, button, label, & label2 in the custom widget class.
Is that not the way to do things?
-
dialog->show();
Add this and you will see your buttons :)
I guess that's not what you want, right ?
Why do you create a Dialog ?
if you want the buttons to be in your custom widget, simply put the layout in it, that's all !Anyway, your code is wrong:
setup(); CWC = new customWidgetClass(); connect(button, &QPushButton::released, this, &MainWindow::buttonClick);
in setup your add CWC in the layout, but you instantiate it later !
Should crash for sure ;) -
@Burke212
what @mpergand meant and rightfully pointed out, you create a new QDialog and sign the layout to that.
But that is not what you want to do!this should do what you want it to:
void customWidgetClass::setup(){ label = new QLabel(); label->setText("Label1"); label2 = new QLabel(); label2->setText("Label2"); button = new QPushButton(); button->setText("custom button"); hLayout = new QHBoxLayout(); hLayout->addWidget(label); hLayout->addWidget(button); hLayout->addWidget(label2); setLayout(hLayout); // assigns the layout to your newly created object derived of QWidget I presume. }
-