QT之void QSplitter::setStretchFactor(int index, int stretch)函数及示例

定义

调用setStretchFactor函数用于设定可伸缩控件,第一个参数是指定设置的控件序号,控件序号按对象中控件插入的先后次序从0开始依次编号;第二个参数是一个大于0的值,表示此控件为可伸缩控件。

Updates the size policy of the widget at position index to have a stretch factor of stretch.
stretch is not the effective stretch factor; the effective stretch factor is calculated by taking the initial size of the widget and multiplying it with stretch.

setStretchFactor函数是下面这种代码的简单写法:

  QWidget *widget = splitter->widget(index);
  QSizePolicy policy = widget->sizePolicy();
  policy.setHorizontalStretch(stretch);
  policy.setVerticalStretch(stretch);
  widget->setSizePolicy(policy);

QSplitter分割窗口的实例

#include "mainwindow.h"
#include <QApplication>
#include <QSplitter>
#include <QTextEdit>
#include <QTextCodec>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
//    MainWindow w;
//    w.show();
    //可分割窗口
    QFont font("ZYSong18030",14);
    a.setFont(font);
    QSplitter *smain = new QSplitter(Qt::Horizontal,0);//水平分割窗口
    QTextEdit *tleft = new QTextEdit("left",smain);
    tleft->setAlignment(Qt::AlignCenter);//设置文字居中显示

    QSplitter *sright = new QSplitter(Qt::Vertical,smain);
    sright->setOpaqueResize(false);//设定分割窗口的分隔条在拖拽时是否为实时更新显示,若设为true则实时更新显示
    QTextEdit *tright_top = new QTextEdit("right top",sright);
    tright_top->setAlignment(Qt::AlignCenter);//设置文字居中显示


    QTextEdit *tright_bottom = new QTextEdit("right bottom",sright);
    tright_bottom->setAlignment(Qt::AlignCenter);//设置文字居中显示

    smain->setStretchFactor(1,1);//选择smain中的第二个控件,即sright 
    smain->setWindowTitle("SPlitter");
    smain->show();
    return a.exec();
}

运行效果

这里写图片描述
可伸缩:
这里写图片描述

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值