Important: Please read the Qt Code of Conduct -

Sticking multiple windows to each other

  • Hello!
    I'm currently writing an application on Qt4 that creates multiple small windows and one large window. What can I do to make the small windows "stick" to the sides of the large one? I tried setting the position with QMoveEvent, but then the windows are kind of aligned one over the other, I need them to look like they are on the same level.

  • Hi and welcome to devnet

    You could have a look to QBoxLayout . Just as a first thought, I would use a hoirzontal layout with a large widget on the left. In the second (right) position there would be vertical layout in which you place all the other widgets.

    You can start with designer and place widgets as you like. This may help you to write it as program or possibly the output of designer is already what you need.

  • Thanks for the fast reply!
    The small windows are actually outside of the main window (and each window inherits the QWidget class). I don't understand how I could apply QBoxLayout in this case.

    To give you a better understanding I create the instances
    ps = new Server; <-- small side window
    pc = new Client; <-- small side window
    pgf = new GameField; <-- main window
    (ps,pc and pgf are all pointers to the three classes) and in the constructor of each instance there is a method setupUi, which I guess writes all the content of each class into new windows. I want the borders of these generated windows to stick to the left and right side of the main window (GameField), kind of like in WinAmp (

  • @Alex-Sivris
    What I described would be within the main window. Your initial description was scarse in that respect.
    AFAIK you could have just your main window in the beginning and when those addtional information come up you can add this into the QVBoxLayout. It is a way to go, but certainly a matter of taste.
    Another possibility could be QToolBar, but that would be also within the main window.
    Sorry, this is beyond my experience and expertize.

  • Moderators

    I wouldn't try to manage this from within moveEvent. This requires a window to know about all other windows and you would end up with a lot of duplicated code in each window class, and then have to implement it every time you add a window. Not that great scaling.

    I would create a separate class to manage this. A sorta external layout if you will. Give it a method to add/remove widgets to track. When adding a widget to track install that class as an event filter and track move events of added widget there. This way you'll have just one place to implement all of it and maintain.

    As for the positioning itself there's no way around it - you will have to track the geometries of the windows and act accordingly.
    I would try to keep two lists: vertical and horizontal edges of the tracked windows. Keep them sorted. When a window moves take it out of the list and on each move find a new place to put it (e.g. with lower bound). If one of the neighboring edges is close enough (within some delta) of the new position adjust that position to "stick".

Log in to reply