Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Can't set a button to a certain position by only using code
Forum Updated to NodeBB v4.3 + New Features

Can't set a button to a certain position by only using code

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 3 Posters 291 Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    thatbeatrice
    wrote on 7 Nov 2023, 23:55 last edited by
    #1

    Hello!
    I am a beginner at Qt and I want to design a window this way:
    qtcreatorhelp.png

    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.

    P C 2 Replies Last reply 8 Nov 2023, 04:04
    0
    • T thatbeatrice
      7 Nov 2023, 23:55

      Hello!
      I am a beginner at Qt and I want to design a window this way:
      qtcreatorhelp.png

      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.

      P Offline
      P Offline
      Pl45m4
      wrote on 8 Nov 2023, 04:04 last edited by
      #2

      @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

      • https://doc.qt.io/qt-6/qspaceritem.html

      If debugging is the process of removing software bugs, then programming must be the process of putting them in.

      ~E. W. Dijkstra

      1 Reply Last reply
      3
      • T thatbeatrice
        7 Nov 2023, 23:55

        Hello!
        I am a beginner at Qt and I want to design a window this way:
        qtcreatorhelp.png

        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.

        C Offline
        C Offline
        ChrisW67
        wrote on 8 Nov 2023, 06:42 last edited by ChrisW67 11 Aug 2023, 06:45
        #3

        @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.

        1 Reply Last reply
        2
        • T thatbeatrice has marked this topic as solved on 16 Nov 2023, 21:02

        1/3

        7 Nov 2023, 23:55

        • Login

        • Login or register to search.
        1 out of 3
        • First post
          1/3
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved