Why I'm not secceed to change the posiyion of the button? helpp c++ qt
-
@RuWex
QLayout
s have to be added ontoQWidget
s, so that when you add child widgets onto that you are controlling where they appear. Yourthis
here must be aQWidget
. Assuming that is the parent widget whose layout you want to set, you need to addthis->setLayout(mainLayout);
However, if your
this
is aQMainWindow
--- I don't know because you don't say --- then that is not the right thing to do. -
@RuWex said in Why I'm not secceed to change the posiyion of the button? helpp c++ qt:
@JonB said in Why I'm not secceed to change the posiyion of the button? helpp c++ qt:
this->setLayout(mainLayout);
yes, my this- *mainWindow:(
so what can I do?Try this
QWidget *widget = new QWidget();
widget->setLayout(mainLayout);
setCentralWidget(widget); -
@RuWex
When you choose to use aQMainWindow
it already has a very specific layout, shown in https://doc.qt.io/qt-6/qmainwindow.html#details. You should start by looking carefully at that. If you do not want the "furniture" added byQMainWindow
--- like menu/toolbar/statusbar all surrounding a "central widget" --- then you probably should not be using it, e.g. a plainQWidget
would probably suffice.But let's say you do want a
QMainWindow
. Then in that picture you must put all your stuff in that Central Widget area. And you must start by giving that area aQWidget
. And you can set that widget's layout. So for your case you probably want:QWidget *centralWidget = new QWidget; this->setCentralWidget(centralWidget); // `this` is a `QMainWindow` QGridLayout *mainLayout = new QGridLayout; centralWidget->setLayout(mainLayout); QPushButton *button = new QPushButton("START TESTING"); mainLayout->addWidget(button, 5,6,5,2);
-
@RuWex said in Why I'm not secceed to change the posiyion of the button? helpp c++ qt:
I tried both of your ways
They are the same way, and they work, as per your question.
The problem is that it takes up the whole window for me and you can't see the rest of the window, and beyond that I want it to disappear after they click on it.
what can we do?
You need to define what you want, we don't know. You might want to show a screenshot with what you have and explain what you want.
-
@JonB
You have (for unknown reason) placed a button at row #5 column #6 and spanning 5 rows and 2 columns. It's not going to make much sense, is perhaps not what you intend, and will move around when you place other widgets on the grid layout.I know nothing about "In the gray square there should have been a cube. that I opened",
The second shot shows about what I expect when you didn't use any layout. If the 3 lines of text there were deliberate you would need to put them into the layout now if you expect to see them.