• qt学习(三) qt布局


    使用横向与竖向、网格三种布局嵌套使用后可以组合出很复杂的界面。

    这里向大家推荐这篇博客

    http://www.cnblogs.com/Bonker/p/3454956.html

    我这里使用布局做了一个对话框界面 作为练习

    代码如下

    qt5

    QT creater创建一个Widgets application

    不过没有使用该工程的UI

    修改main.cpp

    //#include "mainwindow.h"
    #include <QApplication>
    #include "QTestDialog.h"
    
    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
        TestDialog* tdialog = new TestDialog;
        tdialog->show();
        return a.exec();
    }
    

    添加 

    QTestDialog.h
    QTestDialog.cpp
    #ifndef QTESTDIALOG_H
    #define QTESTDIALOG_H
    
    #include <QDialog>
    #include <QTextEdit>
    #include <QPushButton>
    #include <QLayout>
    
    class TestDialog:public QDialog
    {
        Q_OBJECT
    public:
        TestDialog(QWidget *parent = 0);
    private:
        QTextEdit* textEdit_1;
        QTextEdit* textEdit_2;
        QTextEdit* textEdit_3;
        QPushButton* pushButton_1;
        QPushButton* pushButton_2;
        QPushButton* pushButton_3;
        QPushButton* pushButton_4;
        QPushButton* pushButton_5;
    };
    #endif // QTESTDIALOG_H
    

      

    #include <QApplication>
    #include "qtestdialog.h"
    
    TestDialog::TestDialog(QWidget *parent)
        : QDialog(parent)
    {
        pushButton_1 = new QPushButton(tr("字体"));
        pushButton_2 = new QPushButton(tr("大小"));
        pushButton_3 = new QPushButton(tr("消息记录"));
    
        QHBoxLayout* toolLayout = new QHBoxLayout;
        toolLayout->addWidget(pushButton_1);
        toolLayout->addWidget(pushButton_2);
        toolLayout->addStretch();
        toolLayout->addWidget(pushButton_3);
    
    
        pushButton_4 = new QPushButton(tr("关闭"));
        pushButton_5 = new QPushButton(tr("发送"));
        QHBoxLayout* buttomLayout = new QHBoxLayout;
        buttomLayout->addStretch();
        buttomLayout->addWidget(pushButton_4);
        buttomLayout->addWidget(pushButton_5);
    
    
        textEdit_1 = new QTextEdit;
        textEdit_2 = new QTextEdit;
        textEdit_2->setMaximumHeight(90);
        QVBoxLayout* leftlayout = new QVBoxLayout;
        leftlayout->addWidget(textEdit_1);
        leftlayout->addLayout(toolLayout);
        leftlayout->addWidget(textEdit_2);
        leftlayout->addLayout(buttomLayout);
    
    
    
        textEdit_3 = new QTextEdit;
        textEdit_3->setMaximumWidth(100);
        QVBoxLayout* rightlayout = new QVBoxLayout;
        rightlayout->addWidget(textEdit_3);
    
        QHBoxLayout* toplayout = new QHBoxLayout;
        toplayout->addLayout(leftlayout);
        toplayout->addLayout(rightlayout);
    
    
        QHBoxLayout *mainLayout = new QHBoxLayout;
        mainLayout->addLayout(toplayout);
        setLayout(mainLayout);
    
    
    }
    

      

     最后效果图

  • 相关阅读:
    TP5 try{}catch{}异常捕获不到 解决办法
    layui2.5 开关在confirm确认了之后在关/开
    JQuery 表单textarea控制字数
    Navicat Premium从远程Mysql数据库复制到本地数据库的方法
    dedecmsV5.7 任意文件上传漏洞修复
    PHP 利用PHPExcel到处数据到Excel;还有导出数据乱码的解决方案。
    Mac Pro 2017款自带php与用brew重装PHP后的地址
    用js传递当前页面的url,丢失了&后面的参数 解决办法
    PHP 超全局变量之$_SERVER
    Linux while和for循环简单分析
  • 原文地址:https://www.cnblogs.com/itdef/p/5562983.html
Copyright © 2020-2023  润新知