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. How could I fix (x, y) of a widget in a mainwindow?
Forum Update on Monday, May 27th 2025

How could I fix (x, y) of a widget in a mainwindow?

Scheduled Pinned Locked Moved General and Desktop
8 Posts 5 Posters 6.4k Views
  • 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.
  • P Offline
    P Offline
    pico
    wrote on last edited by
    #1

    Hi, Sir:
    I have a widget and a splitter in a mainwindow, and, I insert them into a QHBoxLayout by using addwidget().
    And, the widget has several buttons.

    After running, things look good. But, if I expand the window's size, the first button's position will go down.

    Could anyone teach me how to fix the position of these buttons even I make the window's size bigger?

    Thanks a lot.

    BR,

    Pico

    Here is my code segment


    @
    QWidget * centralwdg = new QWidget(this); //centralwdg is central widget

    QHBoxLayout * mainLayout = new QHBoxLayout(centralwdg);
    
    mButtonLayout = new ButtonLayout(centralwdg); // this layout is left pane
    
    mSplitterRight = new QSplitter(Qt::Vertical, centralwdg);//this splitter would be for other views
    
    mRawDataView = new RawDataView(mSplitterRight);
    
    mDumyWidget = new QWidget(mSplitterRight);
    
    mTableView = new QTableView(mSplitterRight);
    
    mSplitterRight->addWidget(mRawDataView);
    
    mSplitterRight->addWidget(mTableView);
    
    mSplitterRight->addWidget(mDumyWidget);    
    
    mSplitterRight->setCollapsible(0, false);
    
    mSplitterRight->setCollapsible(1, false);
    
    mSplitterRight->setCollapsible(2, false);
    
    mDumyWidget->hide();
    
    //mainLayout->addWidget(mSplitter);
    
    mainLayout->addWidget(mButtonLayout);
    
    mainLayout->addWidget(mSplitterRight,1);
    
    
    setCentralWidget(centralwdg);
    

    @

    1 Reply Last reply
    0
    • T Offline
      T Offline
      tobias.hunger
      wrote on last edited by
      #2

      If you leave out the layouts then you can position widgets as x, y positions, but that is most likely not what you want: Resizing will no longer work properly then as the widgets will stay at their given position no matter how big the window is.

      What I think you want to do is either fix the vertical size of the widgets or add some spacer element above or below your horizontal layout (place the horizontal layout inside a vertical one and add a spacer element where desired). That way the spacer will expand when resizing the window vertically and not the elements inside your horizontal layout.

      I recommend playing with the UI designer: It is really easy to change add/remove layouts there, it offers immediate feedback on the effects of the changed settings and is way simpler to edit than hardcoded UIs, too.

      Even when insisting on hardcoding UIs the designer is a great tool to experiment with layouts in Qt.

      1 Reply Last reply
      0
      • S Offline
        S Offline
        Scylla
        wrote on last edited by
        #3

        I totaly agree with Tabias. The easiest way to create and test the form, is to use the designer. You can create a compex layout without coding one line. And best you can see what happens in the preview. This saves a lot of time for me!

        1 Reply Last reply
        0
        • G Offline
          G Offline
          goetz
          wrote on last edited by
          #4

          [quote author="Tobias Hunger" date="1294481222"]Even when insisting on hardcoding UIs the designer is a great tool to experiment with layouts in Qt.[/quote]

          And it's easy to call uic manually on the .ui file, grab the generated code for the UI setup and adapt it further. That helped me a lot in the past.

          http://www.catb.org/~esr/faqs/smart-questions.html

          1 Reply Last reply
          0
          • ZlatomirZ Offline
            ZlatomirZ Offline
            Zlatomir
            wrote on last edited by
            #5

            @Volker, that is a really good tip, thank you

            //i used to look at ui_CLASS.h implementation (and sometimes use layouts names to add "coded" widgets to them) but i didn't had the idea of getting the code from there, thanks ;)

            https://forum.qt.io/category/41/romanian

            1 Reply Last reply
            0
            • P Offline
              P Offline
              pico
              wrote on last edited by
              #6

              Hi, Tobias:
              Thanks for your recommendations.

              I indeed use Qt Designer sometimes, but, I don't know how to integrate the different components (not basic components) in a layout now. So, I don't use it here.

              I want to add two different components in a window. But, I just want to fix (x, y) of the top-left position of the first components (It indeed is a vertical layout with several buttons and a vertical spacer). So, if I can not use another vertical layout to include original horizontal layout and new spacer. It seems to limit the second component's height.

              Could you teach me more to solve this view issue?

              Thanks a lot.
              BR,
              Pico

              1 Reply Last reply
              0
              • T Offline
                T Offline
                tobias.hunger
                wrote on last edited by
                #7

                pico: We have "excellent documentation":http://doc.qt.nokia.com/4.7/how-to-learn-qt.html which introduces the layout system. Please read it.

                If our documentation does not help then I can point you to lots of "great books on Qt programming":http://qt.nokia.com/developer/books/. All I have read do cover the layout system extensively.

                Best Regards,
                Tobias

                1 Reply Last reply
                0
                • P Offline
                  P Offline
                  pico
                  wrote on last edited by
                  #8

                  Hi, Tobias:
                  Thanks a lot.
                  I will read them firstly.

                  1 Reply Last reply
                  0

                  • Login

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