这些天在做程序的时候,需要用上Qt来布局,但这时候又有一个难题了,如何能不用layout也将组件摆放上窗口呢?网上按“QT如何使用绝对坐标布局”这样的关键字找不到答案。。

    于是,忽然灵机一动,想到使用Qt Designer来解决这个问题,为什么Qt Designer上面还没有布局就可以将组件摆放上去了呢?于是,看看它的源代码就知道了答案。

 

需要用上的相关的关键字有:Geometry, QRect, 知道这两点,就可以从Qt Assistant中找到解决的方法了。

 

效果图:

Dumpt.com

源代码:

 
  
  1. /********************************************************************************  
  2. ** Form generated from reading UI file 'ʹ�þ������겼��Vc2776.ui'  
  3. **  
  4. ** Created: Tue Aug 21 09:11:15 2012  
  5. **      by: Qt User Interface Compiler version 4.8.2  
  6. **  
  7. ** WARNING! All changes made in this file will be lost when recompiling UI file!  
  8. ********************************************************************************/ 
  9.  
  10. #ifndef _4F7F__7528__7EDD__5BF9__5750__6807__5E03__5C40_VC2776_H  
  11. #define _4F7F__7528__7EDD__5BF9__5750__6807__5E03__5C40_VC2776_H  
  12.  
  13. #include <QtCore/QVariant>  
  14. #include <QtGui/QAction>  
  15. #include <QtGui/QApplication>  
  16. #include <QtGui/QButtonGroup>  
  17. #include <QtGui/QHeaderView>  
  18. #include <QtGui/QLabel>  
  19. #include <QtGui/QWidget>  
  20.  
  21. QT_BEGIN_NAMESPACE  
  22.  
  23. class Ui_uiWidget  
  24. {  
  25. public:  
  26.     QLabel *label;  
  27.     QLabel *label_2;  
  28.  
  29.     void setupUi(QWidget *uiWidget)  
  30.     {  
  31.         if (uiWidget->objectName().isEmpty())  
  32.             uiWidget->setObjectName(QString::fromUtf8("uiWidget"));  
  33.         uiWidget->resize(307, 180);  
  34.         label = new QLabel(uiWidget);  
  35.         label->setObjectName(QString::fromUtf8("label"));  
  36.         label->setGeometry(QRect(60, 60, 91, 51));  
  37.         label_2 = new QLabel(uiWidget);  
  38.         label_2->setObjectName(QString::fromUtf8("label_2"));  
  39.         label_2->setGeometry(QRect(180, 100, 101, 41));  
  40.  
  41.         retranslateUi(uiWidget);  
  42.  
  43.         QMetaObject::connectSlotsByName(uiWidget);  
  44.     } // setupUi  
  45.  
  46.     void retranslateUi(QWidget *uiWidget)  
  47.     {  
  48.         uiWidget->setWindowTitle(QApplication::translate("uiWidget""Form", 0, QApplication::UnicodeUTF8));  
  49.         label->setText(QApplication::translate("uiWidget""\347\244\272\344\276\213Label", 0, QApplication::UnicodeUTF8));  
  50.         label_2->setText(QApplication::translate("uiWidget""\347\244\272\344\276\213Label2", 0, QApplication::UnicodeUTF8));  
  51.     } // retranslateUi  
  52.  
  53. };  
  54.  
  55. namespace Ui {  
  56.     class uiWidget: public Ui_uiWidget {};  
  57. // namespace Ui  
  58.  
  59. QT_END_NAMESPACE  
  60.  
  61. #endif // _4F7F__7528__7EDD__5BF9__5750__6807__5E03__5C40_VC2776_H  

 可以看到,这里的两个Label都是将父类设成这个窗口,然后,在定位那里就用了

setGeometry(QRect(180, 100, 101, 41));

QRect的构造函数:QRect ( int x, int y, int width, int height )

 

关于Geometry的一些说明:

const QRect &geometry () const
voidsetGeometry ( int x, int y, int w, int h )
voidsetGeometry ( const QRect & )

geometry : QRect

This property holds the geometry of the widget relative to its parent and excluding the window frame.

When changing the geometry, the widget, if visible, receives a move event (moveEvent()) and/or a resize event (resizeEvent()) immediately. If the widget is not currently visible, it is guaranteed to receive appropriate events before it is shown.

The size component is adjusted if it lies outside the range defined by minimumSize() and maximumSize().

Warning: Calling setGeometry() inside resizeEvent() or moveEvent() can lead to infinite recursion.

See the Window Geometry documentation for an overview of geometry issues with windows.

By default, this property contains a value that depends on the user's platform and screen geometry.