QOpenGlWidgets QLayer, QPushButton position problem
-
Hello, i'm trying to position buttons and make later dynamic positioning for them but they don't react on setGeometry. There is code for main widget class, menu subclass and for buttons.
class MainWidget : public QOpenGLWidget, protected QOpenGLFunctions { Q_OBJECT public: using QOpenGLWidget::QOpenGLWidget; virtual ~MainWidget(); protected: void mousePressEvent(QMouseEvent *e) override; private: Layouts::mainmenu *myGUI = new Layouts::mainmenu(); }; MainWidget::~MainWidget() { makeCurrent(); //delete myGUI; doneCurrent(); } void MainWidget::mousePressEvent(QMouseEvent *e) { if(e->buttons() == Qt::LeftButton) { myGUI->showSettings(this); update(); } else if(e->buttons() == Qt::RightButton) { myGUI->DestroyMainMenu(this); update(); } } class mainmenu : public QOpenGLWidget { Q_OBJECT QHBoxLayout *l_menu; QPushButton *b_map; QPushButton *b_settings; public: mainmenu(); virtual ~mainmenu(); void showSettings(QWidget *target); void handleSettings(); void DestroyMainMenu(QWidget *target); }; void Layouts::mainmenu::showSettings(QWidget *target) { l_menu = new QHBoxLayout(target); l_menu->setGeometry(QRect(QPoint(100, 100), QSize(400, 50))); b_settings = new QPushButton("My Button", target); b_settings->setGeometry(QRect(QPoint(100, 100), QSize(200, 50))); b_map = new QPushButton("Map", target); b_settings->setGeometry(QRect(QPoint(300, 100), QSize(200, 50))); connect(b_settings, &QPushButton::released, this, &mainmenu::handleSettings); l_menu->addWidget(b_map); l_menu->addWidget(b_settings); b_settings->setText("Created"); }
And effect ss
-
Hi, welcome to the forum.
You've placed your buttons in a layout, so the layout takes care of their positioning. Layouts and manual setting of geometry are two exclusive mechanisms. You can do one or the other, not both.
Also setting geometry on the layout has no effect. Since you gave the layout constructor a widget parameter it now governs that widget and covers the entire widget.
-
Hi, welcome to the forum.
You've placed your buttons in a layout, so the layout takes care of their positioning. Layouts and manual setting of geometry are two exclusive mechanisms. You can do one or the other, not both.
Also setting geometry on the layout has no effect. Since you gave the layout constructor a widget parameter it now governs that widget and covers the entire widget.
Thanks a lot for help, understand what's a problem. But now i have even more questions.
Creating these child widgets without a parameter cause that they're not showing up in main window. (Bsc they're under new widget)
How i can deal with that to create N separated modules which will be showed up when called/created?
I wanted to make N classes with layers and create objects of them when necessary. Is there any good practice to did that?I need to create a UI and later 4Matrix area with camera using QOpenGL to create and manage objects.
-
There' not much to it.
If you don't give widget a parent then it is its own standalone window and shows only when you call show().
If you create a child widget with a parent then child is shown inside parent and you can position it with move/resize/setGeometry etc.
If you create a layout and assign it to a widget (via constructor or setLayout on the widget) then every widget added to the layout becomes a child of the widget governed by that layout and layout takes care of its positioning inside that parent widget.I wanted to make N classes with layers and create objects of them when necessary. Is there any good practice to did that?
If you want to create the widgets on the fly then usually it looks something like this:
// In the setup code of your "container" widget e.g. MainWidget in your case: layout = new QVBoxLayout(this); //layout is a QVBoxLayout* member of MainWidget // When you need to add new widget widget->deleteLater(); // destroy previous widget widget = new YourWidgetWithAllTheStuffInIt(); //widget is a QWidget* member of MainWidget layout->addWidget(widget); // When you need to remove that widget widget->deleteLater(); widget = nullptr;
You can then position your buttons or whatever you want inside that YourWidgetWithAllTheStuffInIt with or without layouts and you can have as many different classes as you need. Keep in mind that any modern resizable app should usually use layouts for this, as doing this manually is just a lot of unnecessary work.