Can't set a button to a certain position by only using code
-
Hello!
I am a beginner at Qt and I want to design a window this way:
The problem is that whatever I do, I can't set the bottom left layout to its position. It ends being on the top left corner.
setLayout(middleLayout); //setting the top left layout QHBoxLayout* topLeftLayout= new QHBoxLayout; QPixmap image("name.png"); imageLabel->setPixmap(image); topLeftLayout->addWidget(imageLabel); topLeftLayout->setAlignment(Qt::AlignLeft | Qt::AlignTop); //setting the bottom left layout - doesn't align QHBoxLayout* bottomLeftLayout=new QHBoxLayout; bottomLeftLayout->addWidget(imageButton); bottomLeftLayout->setAlignment(Qt::AlignLeft | Qt::AlignBottom); //adding the buttons inside middle layout middleLayout->setContentsMargins(400,100,400,100); middleLayout->addWidget(lineEditButtonExample); middleLayout->addWidget(pushButtonExample); middleLayout->setAlignment(Qt::AlignCenter);
I want them to adjust to their assigned positions no matter the size of the window.
Is there a way to implement this or should I position the image button under the existent QPushButton (include the image button in the middle layout)?
Note that I don't want to use the Qt Designer part as I only need the .h and .cpp files.
-
@thatbeatrice said in Can't set a button to a certain position by only using code:
Note that I don't want to use the Qt Designer part as I only need the .h and .cpp files.
In some cases QtDesigner helps to understand layouts and what you have to do to get what you want.
You could add a spacer to push the bottom box down
-
@thatbeatrice QLayoutItem::setAlignment() tells the layout item (widget or QLayoutItem subclass) where to put itself inside the space given to it by its parent layout. The middle layout is given the entire client area of the widget containing this code to manage. You are not putting your top or bottom layout items into a layout, so their position is entirely up to you to manage (and you are not).
These are two different options
#include <QApplication> #include <QWidget> #include <QLabel> #include <QVBoxLayout> #include <QGridLayout> int main(int argc, char **argv) { QApplication app(argc, argv); QWidget w; QLabel *top = new QLabel("Top Left", &w); QLabel *mid = new QLabel("Middle right", &w); QLabel *bot = new QLabel("Bottom right", &w); #if 1 // Option 1 QVBoxLayout *layout = new QVBoxLayout(&w); top->setAlignment(Qt::AlignLeft | Qt::AlignTop); mid->setAlignment(Qt::AlignCenter); bot->setAlignment(Qt::AlignLeft | Qt::AlignBottom); layout->addWidget(top); layout->addWidget(mid); layout->addWidget(bot); #else // Option 2 QGridLayout *layout = new QGridLayout(&w); layout->addWidget(top, 0, 0, Qt::AlignLeft | Qt::AlignTop); layout->addWidget(mid, 1, 1, Qt::AlignCenter); layout->addWidget(bot, 2, 0, Qt::AlignLeft | Qt::AlignBottom); #endif w.resize(800, 600); w.show(); return app.exec(); }
Whether you want to use the code that Designer and
uic
produce, it can be very informative to see how it constructs things. -