QT基础篇-布局管理

Posted 1532387167llx

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了QT基础篇-布局管理相关的知识,希望对你有一定的参考价值。

1、分割窗口类QSplitter类

运行结果:

技术图片

实现过程:

 技术图片

代码部分:

技术图片
 1 /***设置字体***/
 2     QFont font("ZYsong18030",12);
 3     a.setFont(font);
 4     /***水平分割***/
 5     QSplitter *mainSplitter=new QSplitter(Qt::Horizontal,0);
 6     QTextEdit *leftTextEdit=new QTextEdit("Left Widget");
 7     leftTextEdit->setAlignment(Qt::AlignCenter);
 8     mainSplitter->addWidget(leftTextEdit);
 9     /***垂直分割***/
10     QSplitter *topSplitter=new QSplitter(Qt::Vertical,mainSplitter);
11     /***设置滑条拉伸的时候是否实时更新(默认设置为true,false不是实时更新)***/
12     topSplitter->setOpaqueResize(false);
13     QTextEdit *topTextEdit=new QTextEdit("Top Widget");
14     topSplitter->addWidget(topTextEdit);
15     topTextEdit->setAlignment(Qt::AlignCenter);
16     QTextEdit *bottomTextEdit=new QTextEdit("Bottom Widget",topSplitter);
17     bottomTextEdit->setAlignment(Qt::AlignCenter);
18     /***设置是否固定不变,第一个参数为控件序号,第二个参数大于1表示是可伸缩控件***/
19     mainSplitter->setStretchFactor(1,1);
20     mainSplitter->setWindowTitle("Splitter");
21     mainSplitter->show();
View Code

2、停靠窗口类QDockWidget类

 运行结果:

技术图片

实现过程:

技术图片

代码部分:

技术图片
 1 this->setWindowTitle("QDockWidget");
 2     QFont font("ZYsong18030",12);
 3     this->setFont(font);
 4     /***中央窗体***/
 5     QTextEdit *mainTextEdit=new QTextEdit("Main Window",this);
 6     mainTextEdit->setAlignment(Qt::AlignCenter);
 7     this->setCentralWidget(mainTextEdit);
 8     /***停靠窗口1***/
 9     QDockWidget *dockWidget1=new QDockWidget("DockWidget1",this);
10     /***设置停靠窗口1的特征及活动区域***/
11     dockWidget1->setFeatures(QDockWidget::DockWidgetMovable);
12     dockWidget1->setAllowedAreas(Qt::LeftDockWidgetArea | Qt::RightDockWidgetArea);
13     QTextEdit *textEdit1=new QTextEdit("this dockwidget1 can be moved between docks by the user.");
14     dockWidget1->setWidget(textEdit1);
15     this->addDockWidget(Qt::RightDockWidgetArea,dockWidget1);
16     /***停靠窗口2***/
17     QDockWidget *dockWidget2=new QDockWidget("DockWidget2",this);
18     dockWidget2->setFeatures(QDockWidget::DockWidgetFloatable | QDockWidget::DockWidgetClosable);
19     dockWidget2->setAllowedAreas(Qt::RightDockWidgetArea);
20     QTextEdit *textEdit2=new QTextEdit("this dockwidget2 can be floated as an independent window,and can be closed.");
21     dockWidget2->setWidget(textEdit2);
22     this->addDockWidget(Qt::RightDockWidgetArea,dockWidget2);
23     /***停靠窗口3***/
24     QDockWidget *dockWidget3=new QDockWidget("DockWidget3",this);
25     dockWidget3->setFeatures(QDockWidget::AllDockWidgetFeatures);
26     QTextEdit *textEdit3=new QTextEdit("this dockwidget3 can be closed,moved,and floated.");
27     dockWidget3->setWidget(textEdit3);
28     this->addDockWidget(Qt::RightDockWidgetArea,dockWidget3);
29 
View Code

3、堆栈窗口类QStackedWidget类

运行结果:

技术图片

实现过程:

技术图片

代码部分1(纯代码):

技术图片
 1 QDialog *dialog=new QDialog();
 2     this->setCentralWidget(dialog);
 3     /***新建一个QListWidget类并加入3个项目***/
 4     QListWidget *list=new QListWidget();
 5     list->insertItem(0,"window1");
 6     list->insertItem(1,"window2");
 7     list->insertItem(2,"window3");
 8     /***新建3个列表项对应的3个切换label***/
 9     QLabel *label1=new QLabel("WindowTest1");
10     QLabel *label2=new QLabel("WindowTest2");
11     QLabel *label3=new QLabel("WindowTest3");
12     /***将label加入堆栈窗口中***/
13     QStackedWidget *stack=new QStackedWidget();
14     stack->addWidget(label1);
15     stack->addWidget(label2);
16     stack->addWidget(label3);
17     QHBoxLayout *hLayout=new QHBoxLayout;
18     hLayout->setMargin(5);
19     hLayout->setSpacing(5);
20     hLayout->addWidget(list);
21     hLayout->addWidget(stack,0,Qt::AlignCenter);
22     hLayout->setStretchFactor(list,1);
23     hLayout->setStretchFactor(stack,3);
24     centralWidget()->setLayout(hLayout);
25     connect(list,SIGNAL(currentRowChanged(int)),stack,SLOT(setCurrentIndex(int)));
View Code

代码部分2(设计器+代码):

技术图片
 1 构造函数
 2 ui->listWidget->insertItem(0,"window1");
 3     ui->listWidget->insertItem(1,"window2");
 4     ui->listWidget->insertItem(2,"window3");
 5 
 6     ui->stackedWidget->addWidget(ui->page);
 7     ui->stackedWidget->addWidget(ui->page_2);
 8     ui->stackedWidget->addWidget(ui->page_3);
 9 列表窗口行值改变时的响应函数
10 void Dialog::on_listWidget_currentRowChanged(int currentRow)
11 
12     ui->stackedWidget->setCurrentIndex(currentRow);
13 
View Code

4、综合实例:修改用户资料

 运行结果:

(1)基本信息

技术图片

(2)联系方式

技术图片

(3)详细资料

技术图片

代码部分1(纯代码):

技术图片
 1 基本信息
 2  /**** 左侧 ****/
 3     UserNameLabel =new QLabel(tr("用户名:"));
 4     UserNameLineEdit =new QLineEdit;
 5 
 6     NameLabel =new QLabel(tr("姓名:"));
 7     NameLineEdit =new QLineEdit;
 8 
 9     SexLabel =new QLabel(tr("性别:"));
10     SexComboBox =new QComboBox;
11     SexComboBox->addItem(tr(""));
12     SexComboBox->addItem(tr(""));
13 
14     DepartmentLabel =new QLabel(tr("部门:"));
15     DepartmentTextEdit =new QTextEdit;
16 
17     AgeLabel =new QLabel(tr("年龄:"));
18     AgeLineEdit =new QLineEdit;
19 
20     OtherLabel =new QLabel(tr("备注:"));
21     OtherLabel->setFrameStyle(QFrame::Panel|QFrame::Sunken);
22 
23     LeftLayout =new QGridLayout();
24     LeftLayout->addWidget(UserNameLabel,0,0);
25     LeftLayout->addWidget(UserNameLineEdit,0,1);
26 
27     LeftLayout->addWidget(NameLabel,1,0);
28     LeftLayout->addWidget(NameLineEdit,1,1);
29 
30     LeftLayout->addWidget(SexLabel,2,0);
31     LeftLayout->addWidget(SexComboBox,2,1);
32 
33     LeftLayout->addWidget(DepartmentLabel,3,0);
34     LeftLayout->addWidget(DepartmentTextEdit,3,1);
35 
36     LeftLayout->addWidget(AgeLabel,4,0);
37     LeftLayout->addWidget(AgeLineEdit,4,1);
38 
39     LeftLayout->addWidget(OtherLabel,5,0,1,2);
40     LeftLayout->setColumnStretch(0,1);
41     LeftLayout->setColumnStretch(1,3);
42     /****右侧****/
43     HeadLabel =new QLabel(tr("头像: "));                    //右上角部分
44     HeadIconLabel =new QLabel;
45     QPixmap icon("312.png");
46     HeadIconLabel->setPixmap(icon);
47     HeadIconLabel->resize(icon.width(),icon.height());
48     UpdateHeadBtn =new QPushButton(tr("更新"));
49 
50     TopRightLayout =new QHBoxLayout();
51     TopRightLayout->setSpacing(20);
52     TopRightLayout->addWidget(HeadLabel);
53     TopRightLayout->addWidget(HeadIconLabel);
54     TopRightLayout->addWidget(UpdateHeadBtn);
55 
56     IntroductionLabel =new QLabel(tr("个人说明:"));        //右下角部分
57     IntroductionTextEdit =new QTextEdit;
58 
59     RightLayout =new QVBoxLayout();
60     RightLayout->setMargin(10);
61     RightLayout->addLayout(TopRightLayout);
62     RightLayout->addWidget(IntroductionLabel);
63     RightLayout->addWidget(IntroductionTextEdit);
64     /*************************************/
65     QGridLayout *mainLayout =new QGridLayout(this);
66     mainLayout->setMargin(15);
67     mainLayout->setSpacing(10);
68     mainLayout->addLayout(LeftLayout,0,0);
69     mainLayout->addLayout(RightLayout,0,1);
70     mainLayout->setSizeConstraint(QLayout::SetFixedSize);
View Code
技术图片
 1 联系方式
 2 EmailLabel =new QLabel(tr("电子邮件:"));
 3     EmailLineEdit =new QLineEdit;
 4 
 5     AddrLabel =new QLabel(tr("联系地址:"));
 6     AddrLineEdit =new QLineEdit;
 7 
 8     CodeLabel =new QLabel(tr("邮政编码:"));
 9     CodeLineEdit =new QLineEdit;
10 
11     MoviTelLabel =new QLabel(tr("移动电话:"));
12     MoviTelLineEdit =new QLineEdit;
13     MoviTelCheckBook =new QCheckBox(tr("接收留言"));
14 
15     ProTelLabel =new QLabel(tr("办公电话:"));
16     ProTelLineEdit =new QLineEdit;
17 
18     mainLayout =new QGridLayout(this);
19     mainLayout->setMargin(15);
20     mainLayout->setSpacing(10);
21     mainLayout->addWidget(EmailLabel,0,0);
22     mainLayout->addWidget(EmailLineEdit,0,1);
23     mainLayout->addWidget(AddrLabel,1,0);
24     mainLayout->addWidget(AddrLineEdit,1,1);
25     mainLayout->addWidget(CodeLabel,2,0);
26     mainLayout->addWidget(CodeLineEdit,2,1);
27     mainLayout->addWidget(MoviTelLabel,3,0);
28     mainLayout->addWidget(MoviTelLineEdit,3,1);
29     mainLayout->addWidget(MoviTelCheckBook,3,2);
30     mainLayout->addWidget(ProTelLabel,4,0);
31     mainLayout->addWidget(ProTelLineEdit,4,1);
32     mainLayout->setSizeConstraint(QLayout::SetFixedSize);
View Code
技术图片
 1 详细资料
 2 NationalLabel =new QLabel(tr("国家/地址:"));
 3     NationalComboBox =new QComboBox;
 4     NationalComboBox->insertItem(0,tr("中国"));
 5     NationalComboBox->insertItem(1,tr("美国"));
 6     NationalComboBox->insertItem(2,tr("英国"));
 7 
 8     ProvinceLabel =new QLabel(tr("省份:"));
 9     ProvinceComboBox =new QComboBox;
10     ProvinceComboBox->insertItem(0,tr("江苏省"));
11     ProvinceComboBox->insertItem(1,tr("山东省"));
12     ProvinceComboBox->insertItem(2,tr("浙江省"));
13 
14     CityLabel =new QLabel(tr("城市:"));
15     CityLineEdit =new QLineEdit;
16 
17     IntroductLabel =new QLabel(tr("个人说明:"));
18     IntroductTextEdit =new QTextEdit;
19 
20     mainLayout =new QGridLayout(this);
21     mainLayout->setMargin(15);
22     mainLayout->setSpacing(10);
23     mainLayout->addWidget(NationalLabel,0,0);
24     mainLayout->addWidget(NationalComboBox,0,1);
25     mainLayout->addWidget(ProvinceLabel,1,0);
26     mainLayout->addWidget(ProvinceComboBox,1,1);
27     mainLayout->addWidget(CityLabel,2,0);
28     mainLayout->addWidget(CityLineEdit,2,1);
29     mainLayout->addWidget(IntroductLabel,3,0);
30     mainLayout->addWidget(IntroductTextEdit,3,1);
31 修改关闭按钮
32 stack =new QStackedWidget(this);
33     stack->setFrameStyle(QFrame::Panel|QFrame::Raised);
34     baseInfo =new BaseInfo();
35     contact =new Contact();
36     detail =new Detail();
37     stack->addWidget(baseInfo);
38     stack->addWidget(contact);
39     stack->addWidget(detail);
40 
41     AmendBtn =new QPushButton(tr("修改"));
42     CloseBtn =new QPushButton(tr("关闭"));
43     QHBoxLayout *BtnLayout =new QHBoxLayout;
44     BtnLayout->addStretch(1);
45     BtnLayout->addWidget(AmendBtn);
46     BtnLayout->addWidget(CloseBtn);
47 
48     QVBoxLayout *RightLayout =new QVBoxLayout(this);
49     RightLayout->setMargin(10);
50     RightLayout->setSpacing(6);
51     RightLayout->addWidget(stack);
52     RightLayout->addLayout(BtnLayout);
View Code

悄悄咪咪说一句:纯代码是真的很冗余

代码部分2(设计器):

 这就很简单了(代码部分只需要将QListWidget的行索引改变时的信号与QStackedWidget的setCurrentIndex(currentRow)槽连接即可)

技术图片
1 void UserInfor::on_ListWidget_currentRowChanged(int currentRow)
2 
3     ui->StackedWidget->setCurrentIndex(currentRow);
4 
View Code

小总结:在这个实例的设计过程中,我发现QLabel有setFrameStyle()函数设置,但QLineEdit类是没有的;

              在设计器中可以直接设置frameShape为Panel,frameShadow为Sunken;

              QComboBox下拉框中的各项可以直接在设计器中双击添加,QListWidget中的项目可以在设计器中点击直接添加;

              QStackedWidget在设计器中添加不同层次的窗口时,不同窗口是显示在不同的page上的,需要在每个page上布好局。

以上是关于QT基础篇-布局管理的主要内容,如果未能解决你的问题,请参考以下文章

Python Qt GUI设计:窗口布局管理方法(基础篇—4)

QT软件开发之基础控件--2.2 布局控件组

Qt入门系列开发教程基础控件篇QMainWindow主窗口

《PyQT5软件开发 - 基础篇》第3章 PyQt5布局管理

Qt-布局管理器

QT开发(二十一)——QT布局管理器