Qt的Layout

开发一个图形界面应用程序,界面的布局影响到界面的美观。在设计一个界面以前,应该考虑到开发的界面可能给不用的用户使用,而用户的屏幕大小、纵横比例、分辨率可能不一样,界面还多是可缩放的,程序应该能够适应这些变化。布局

        前面的程序中都是使用setGeometry()方法定位控件的位置,这个方法比较笨拙。试想若是控件不少,布局这些控件须要编写大量的代码。幸运的是,QT提供了更好的方法布局控件。spa

        经常使用的三种布局方法:.net

(1)使用水平布局类QHBoxLayout;设计

(2)使用垂直布局类QVBoxLayout;blog

(3)使用网格布局类QGridLayout。ci

这三种方法能够嵌套使用。开发

 

控件在布局时能够先不指定父窗口,最后交由Layout统一指定。get

 

示例:it

  1. #include <QApplication>  io

  2. #include <QDialog>  

  3. #include <QPushButton>  

  4. #include <QLineEdit>  

  5. #include <QLayout>  

  6. #include <QLabel>  

  7.   

  8.   

  9. int main(int argc, char *argv[])  

  10. {  

  11.     QApplication a(argc, argv);  

  12.     QDialog *mainWindow = new QDialog;  

  13.   

  14.     QHBoxLayout *topLayout = new QHBoxLayout;  

  15.     QLabel *lbl = new QLabel(QWidget::tr("&Input:"), mainWindow);  

  16.     QLineEdit *lineEdt = new QLineEdit(mainWindow);  

  17.     lbl->setBuddy(lineEdt);  

  18.     topLayout->addWidget(lbl);  

  19.     topLayout->addWidget(lineEdt);  

  20.   

  21.     QHBoxLayout *bomLayout = new QHBoxLayout;  

  22.     QPushButton *btn_ok = new QPushButton(QWidget::tr("OK"), mainWindow);\  

  23.     btn_ok->setDefault(true);  

  24.     QPushButton *btn_cancel = new QPushButton(QWidget::tr("Cancel"), mainWindow);  

  25.     bomLayout->addStretch();  

  26.     bomLayout->addWidget(btn_ok);  

  27.     bomLayout->addStretch();  

  28.     bomLayout->addWidget(btn_cancel);  

  29.     bomLayout->addStretch();  

  30.   

  31.     QVBoxLayout *mainLayout = new QVBoxLayout;  

  32.     mainLayout->addLayout(topLayout);  

  33.     mainLayout->addLayout(bomLayout);  

  34.       

  35.     mainWindow->setLayout(mainLayout);  

  36.   

  37.     mainWindow->resize(300, 100);  

  38.     mainWindow->setWindowTitle(QWidget::tr("Qt Test"));  

  39.     mainWindow->show();  

  40.   

  41.     return a.exec();  

  42. }  


编译运行,界面以下:

 

在界面中,最外部是mainLayout,它的类型是垂直布局类QVBoxLayout。它包含了两个水平布局类QHBoxLayout,分别是topLayout和bomLayout。

 

比起QHBoxLayout和HVBoxLayout, QGridLayout运用更加灵活。

QGridLayout的经常使用方法

(1)addWidget:

  1. //放置一个控件到一个单元格  

  2. void    addWidget ( QWidget * widget, int row, int column, Qt::Alignment alignment = 0 )  

  3. //若是放置的控件超出一个单元格,则使用该方法  

  4. void    addWidget ( QWidget * widget, int fromRow, int fromColumn, int rowSpan, int columnSpan, Qt::Alignment alignment = 0 )  

    1)row:指放置控件的网格行号(行号从0开始);

    2)colum:指放置控件的网格列号(从0开始);

    3)alignment:对齐方式。

    4)fromRow:指放置控件的起始网格行号;

    5)fromColumn:指放置控件的起始网格列号;

    6)rowSpan:指放置控件占多少行;

    7)columnSpan:指放置控件占多少列。


(2)addLayout

  1. void    addLayout ( QLayout * layout, int row, int column, Qt::Alignment alignment = 0 )  

  2. void    addLayout ( QLayout * layout, int row, int column, int rowSpan, int columnSpan, Qt::Alignment alignment = 0 )  

参数与addWidget相似。

(3)setSpacing

  1. void QGridLayout::setSpacing ( int spacing )  

设置控件水平和垂直之间的间隔。

示例:

  1. #include <QApplication>  

  2. #include <QDialog>  

  3. #include <QPushButton>  

  4. #include <QLineEdit>  

  5. #include <QLayout>  

  6. #include <QLabel>  

  7. #include <QTextEdit>  

  8.   

  9.   

  10. int main(int argc, char *argv[])  

  11. {  

  12.     QApplication a(argc, argv);  

  13.     QDialog *mainWindow = new QDialog;  

  14.   

  15.     QGridLayout *gridLayout = new QGridLayout;  

  16.     gridLayout->setColumnStretch(0, 1);  

  17.     gridLayout->setColumnStretch(1, 4);  

  18.     gridLayout->setColumnStretch(2, 1);  

  19.     gridLayout->setColumnStretch(3, 1);  

  20.     gridLayout->setColumnStretch(4, 4);  

  21.   

  22.     gridLayout->setMargin(15);  

  23.     gridLayout->setColumnMinimumWidth(2, 15);  

  24.   

  25.   

  26.     QLabel *lbl1 = new QLabel(QWidget::tr("First Name:"));  

  27.     QLineEdit *edit1 = new QLineEdit;  

  28.     QLabel *lbl2 = new QLabel(QWidget::tr("Last Name:"));  

  29.     QLineEdit *edit2 = new QLineEdit;  

  30.     QLabel *lbl3 = new QLabel(QWidget::tr("Sex:"));  

  31.     QLineEdit *edit3 = new QLineEdit;  

  32.     QLabel *lbl4 = new QLabel(QWidget::tr("Birthday:"));  

  33.     QLineEdit *edit4 = new QLineEdit;  

  34.     QLabel *lbl5 = new QLabel(QWidget::tr("Address:"));  

  35.     QTextEdit *textEdt = new QTextEdit;  

  36.   

  37.   

  38.   

  39.     gridLayout->addWidget(lbl1, 0, 0);  

  40.     gridLayout->addWidget(edit1, 0, 1);  

  41.     gridLayout->addWidget(lbl2, 0, 3);  

  42.     gridLayout->addWidget(edit2, 0, 4);  

  43.     gridLayout->addWidget(lbl3, 1, 0);  

  44.     gridLayout->addWidget(edit3, 1, 1);  

  45.     gridLayout->addWidget(lbl4, 1, 3);  

  46.     gridLayout->addWidget(edit4, 1, 4);  

  47.     gridLayout->addWidget(lbl5, 2, 0);  

  48.     gridLayout->addWidget(textEdt, 3, 0, 2, 5);  

  49.       

  50.     mainWindow->setLayout(gridLayout);  

  51.   

  52.     mainWindow->resize(400, 150);  

  53.     mainWindow->setWindowTitle(QWidget::tr("Qt Test"));  

  54.     mainWindow->show();  

  55.   

  56.     return a.exec();  

  57. }  


编译运行,界面如图:

 

转http://blog.csdn.net/xgbing/article/details/7764326

相关文章
相关标签/搜索