欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

qt 分割窗口

程序员文章站 2024-03-15 15:41:23
...

左右分隔窗口例子:

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

#pragma execution_character_set("utf-8")

int main(int argc, char *argv[])
{
    qDebug("Mainframe start...\n");
    QApplication a(argc, argv);
    QFont font("ZYSong18030" ,12);
    a.setFont(font);
    QTextCodec::setCodecForLocale(QTextCodec::codecForName("UTF-8"));

    //主窗口左右分隔
    QSplitter *splitMain=new QSplitter(Qt::Horizontal,0);
    //QTextEdit *textLeft=new QTextEdit(QObject::tr("left Widget"),splitMain);

    QTextEdit *textLeft=new QTextEdit();
    textLeft->setAlignment(Qt::AlignCenter);
    textLeft->setText(QObject::tr("左窗口 "));

    QTextEdit *pRightEdt =new QTextEdit();
    pRightEdt ->setAlignment(Qt::AlignCenter);
    pRightEdt ->setText(QObject::tr("右窗口 "));


    splitMain->addWidget(textLeft);
    splitMain->addWidget(pRightEdt);


    splitMain->setStretchFactor(0, 1);//下面两句设置左右两列的宽度比为1:4
    splitMain->setStretchFactor(1, 4);

    splitMain->setWindowTitle(QObject::tr("Splitter 窗口例子"));
    splitMain->show();
    //MainWindow w;
    //w.show();

    return a.exec();
}

运行效果如下:


qt 分割窗口


左右(上下)拆分

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

#pragma execution_character_set("utf-8")

int main(int argc, char *argv[])
{
    qDebug("Mainframe start...\n");
    QApplication a(argc, argv);
    QFont font("ZYSong18030" ,12);
    a.setFont(font);
    QTextCodec::setCodecForLocale(QTextCodec::codecForName("UTF-8"));

    //主窗口左右分隔
    QSplitter *splitMain=new QSplitter(Qt::Horizontal,0);
    QTextEdit *textLeft=new QTextEdit(QObject::tr("left Widget"),splitMain);
    textLeft->setAlignment(Qt::AlignCenter);





    splitMain->addWidget(textLeft);

    splitMain->setStretchFactor(0, 1);//下面两句设置左右两列的宽度比为1:4
    splitMain->setStretchFactor(1, 4);



    QSplitter *pRightSpliter = new QSplitter(Qt::Vertical, splitMain);

    QTextEdit* pRightTopEdt = new QTextEdit(pRightSpliter);
    pRightTopEdt->setText(QObject::tr("右上窗口"));
    QTextEdit* pRightBtmEdt = new QTextEdit(pRightSpliter);
    pRightBtmEdt->setText(QObject::tr("右下窗口"));


    pRightSpliter->setStretchFactor(0, 1);//右面窗口上下窗口高度比为1:4
    pRightSpliter->setStretchFactor(1, 4);


    splitMain->setWindowTitle(QObject::tr("Splitter 窗口例子"));
    splitMain->show();
    //MainWindow w;
    //w.show();

    return a.exec();
}

界面如下:

qt 分割窗口

相关标签: qt分隔窗口