布局

通常,子Widget是通过使用布局对象在窗口中进行排列的,而不是通过指定位置和大小进行排列的。在此,构造一个并排排列的标签和行编辑框Widget:

QLabel *label = new QLabel(tr("Name:"));

QLineEdit *lineEdit = new QLineEdit();

 

QHBoxLayout *layout = new QHBoxLayout();

layout->addWidget(label);

layout->addWidget(lineEdit);

window->setLayout(layout);

 

 

复制代码
由于Widget可包含其他Widget,所以布局可用来提供按不同层次分组的Widget。这里,要在显示查询结果的表视图上方、窗口顶部的行编辑框旁,显示一个标签:
QLabel *queryLabel = new QLabel(tr("Query:"));
QLineEdit *queryEdit = new QLineEdit();
QTableView *resultView = new QTableView();
 
QHBoxLayout *queryLayout = new QHBoxLayout();
queryLayout->addWidget(queryLabel);
queryLayout->addWidget(queryEdit);
 
QVBoxLayout *mainLayout = new QVBoxLayout();
mainLayout->addLayout(queryLayout);
mainLayout->addWidget(resultView);
window->setLayout(mainLayout);
复制代码

 

 

Qt提供了QHBoxLayout类、QVBoxLayout类及QGridLayout类等的基本布局管理,分别是水平排列布局、垂直排列布局和网格排列布局。它们之间的继承关系如图:
 
 
复制代码
addWidget()方法用于向布局中加入需要布局的控件,addWidget()的函数原型如下:
void addWidget
(
    QWidget *widget,                   //需要插入的控件对象
    int  fromRow,                          //插入的行
    int  fromColumn,                   //插入的列
    int  rowSpan,                          //表示占用的行数
    int  columnSpan,                   //表示占用的列数
    Qt::Alignment  alignment=0          //描述各个控件的对齐方式
)
addLayout ()方法用于向布局中加入需要布局的子布局,addLayout ()的函数原型如下:
void addLayout
(
    QLayout *layout,                      //表示需要插入的子布局对象
    int row,                                  //插入的起始行
    int column,                          //插入的起始列
    int rowSpan,                          //表示占用的行数
    int columnSpan,                       //表示占用的列数
    Qt::Alignment alignment=0              //指定对齐方式
)
复制代码

 

 

新建Qt  应用,基类选择“QDialog”,取消“创建界面”复选框的选中状态。
打开“dialog.h”头文件,在头文件中声明对话框中的各个控件。添加代码所示。
添加如下的头文件:

#include <QLabel>

#include <QLineEdit>

#include <QComboBox>

#include <QTextEdit>

#include <QGridLayout>

 

 

复制代码
“dialog.h”头文件,在类中声明对话框中的各个控件。添加代码所示。
private:
    QLabel *UserNameLabel; QLineEdit *UserNameLineEdit;
    QLabel *NameLabel; QLineEdit *NameLineEdit;
    QLabel *SexLabel; QComboBox *SexComboBox;
    QLabel *DepartmentLabel; QTextEdit *DepartmentTextEdit;
    QLabel *AgeLabel; QLineEdit *AgeLineEdit;
    QLabel *OtherLabel; QGridLayout *LeftLayout;

    /*********右侧*********/
    QLabel *HeadLabel; QLabel *HeadIconLabel;
    QPushButton *UpdateHeadBtn; QHBoxLayout *TopRightLayout;
    QLabel *IntroductionLabel; QTextEdit *IntroductionTextEdit;
    QVBoxLayout *RightLayout;

    /*--------------------- 底部 --------------------*/
    QPushButton *OkBtn; QPushButton *CancelBtn;
    QHBoxLayout *ButtomLayout;
复制代码

 

 

复制代码
“dialog.cpp”文件,在类Dialog的构造函数中添加代码。
在“dialog.cpp”文件的开始部分加入以下头文件:
#include<QLabel>
#include<QLineEdit>
#include<QComboBox>
#include<QPushButton>
#include<QFrame>
#include<QGridLayout>
#include<QPixmap>
#include<QHBoxLayout>
复制代码

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
wxPython布局是一种用于创建GUI界面的布局方式。主要有四种布局方式:Box布局、StaticBox布局、Grid布局和FlexGrid布局。其中,Box布局是基于水平或垂直方向排列组件的简单布局方式。StaticBox布局在Box布局的基础上添加了一个静态框作为容器,用于将相关组件组合在一起。Grid布局是将组件排列在网格中的布局方式,可以指定每个组件在网格中的位置和占用的行列数。FlexGrid布局是一种灵活的网格布局方式,可以指定每个组件在网格中的位置和占用的行列数,并可以动态调整行列的大小。 在wxPython中,可以使用相应的布局类来创建布局管理器,然后将组件添加到布局管理器中,最后将布局管理器设置给面板或窗口对象。通过指定组件在布局管理器中的位置和占用的行列数,可以实现灵活的布局效果。 以下是一个使用FlexGrid布局的示例代码: ```python import wx class MyFrame(wx.Frame): def __init__(self): super().__init__(parent=None, title="FlexGrid布局器", size=(400, 200)) self.Centre() panel = wx.Panel(parent=self) fgs = wx.FlexGridSizer(3, 2, 10, 10) title = wx.StaticText(panel, label='标题:') author = wx.StaticText(panel, label='作者:') review = wx.StaticText(panel, label='内容:') tcl = wx.TextCtrl(panel) tc2 = wx.TextCtrl(panel) tc3 = wx.TextCtrl(panel, style=wx.TE_MULTILINE) fgs.AddMany([ title, (tcl, 1, wx.EXPAND), author, (tc2, 1, wx.EXPAND), review, (tc3, 1, wx.EXPAND) ]) fgs.AddGrowableRow(0, 1) fgs.AddGrowableRow(1, 1) fgs.AddGrowableRow(2, 3) fgs.AddGrowableCol(0, 1) fgs.AddGrowableCol(1, 2) panel.SetSizer(fgs) class App(wx.App): def OnInit(self): frame = MyFrame() frame.Show() return True def OnExit(self): print('应用程序退出') return 0 if __name__ == '__main__': app = App() app.MainLoop() ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值