January 8, 2011

pico pico
Lab Rat
28 posts

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

 

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

**************************************************************************************************

  1.     QWidget * centralwdg = new QWidget(this); //centralwdg is central widget
  2.  
  3.     QHBoxLayout * mainLayout = new QHBoxLayout(centralwdg);
  4.  
  5.     mButtonLayout = new ButtonLayout(centralwdg); // this layout is left pane
  6.  
  7.     mSplitterRight = new QSplitter(Qt::Vertical, centralwdg);//this splitter would be for other views
  8.  
  9.     mRawDataView = new RawDataView(mSplitterRight);
  10.  
  11.     mDumyWidget = new QWidget(mSplitterRight);
  12.  
  13.     mTableView = new QTableView(mSplitterRight);
  14.  
  15.     mSplitterRight->addWidget(mRawDataView);
  16.  
  17.     mSplitterRight->addWidget(mTableView);
  18.  
  19.     mSplitterRight->addWidget(mDumyWidget);    
  20.  
  21.     mSplitterRight->setCollapsible(0, false);
  22.  
  23.     mSplitterRight->setCollapsible(1, false);
  24.  
  25.     mSplitterRight->setCollapsible(2, false);
  26.  
  27.     mDumyWidget->hide();
  28.  
  29.     //mainLayout->addWidget(mSplitter);
  30.  
  31.     mainLayout->addWidget(mButtonLayout);
  32.  
  33.     mainLayout->addWidget(mSplitterRight,1);
  34.  
  35.  
  36.     setCentralWidget(centralwdg);

7 replies

January 8, 2011

Tobias Hunger Tobias Hunger
Mad Scientist
3149 posts

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.

January 8, 2011

Scylla Scylla
Lab Rat
238 posts

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!

January 8, 2011

Volker Volker
Robot Herder
5428 posts
Tobias Hunger wrote:
Even when insisting on hardcoding UIs the designer is a great tool to experiment with layouts in Qt.

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.

January 8, 2011

Zlatomir Zlatomir
Dinosaur Breeder
326 posts

@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 ;)

January 10, 2011

pico pico
Lab Rat
28 posts

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

January 11, 2011

Tobias Hunger Tobias Hunger
Mad Scientist
3149 posts

pico: We have excellent documentation [doc.qt.nokia.com] 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 [qt.nokia.com]. All I have read do cover the layout system extensively.

Best Regards,
Tobias

January 12, 2011

pico pico
Lab Rat
28 posts

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

 
  ‹‹ [SPLIT] Future of Widgets in Qt      How to use Debugger in qt creator ?? ››

You must log in to post a reply. Not a member yet? Register here!