0
点赞
收藏
分享

微信扫一扫

Qt的垂直布局和水平布局

新鲜小饼干 2022-05-06 阅读 80
qtc++

一:程序异常结束 --- 解决方法:重新构建项目

Qt软件在使用过程中有时候会出现代码没错但是程序会异常结束。

解决方法:重新构建该项目

二:Qt的垂直布局 --- V

indexwin.h .cpp

#ifndef INDEXWIN_H
#define INDEXWIN_H

#include <QWidget>
#include<QVBoxLayout>//垂直布局
#include<QPushButton>

class IndexWin : public QWidget
{
    Q_OBJECT
public:
    explicit IndexWin(QWidget *parent = 0);

    QWidget *window;
    QPushButton *button1;
    QPushButton *button2;
    QPushButton *button3;
    QPushButton *button4;
    QPushButton *button5;


signals:

public slots:
};

#endif // INDEXWIN_H
#include "indexwin.h"

IndexWin::IndexWin(QWidget *parent) : QWidget(parent)
{
    this->resize(1500,800);

    window = new QWidget(this);
    button1 = new QPushButton("One");
    button2 = new QPushButton("Two");
    button3 = new QPushButton("Three");
    button4 = new QPushButton("Four");
    button5 = new QPushButton("Five");

    QVBoxLayout *layout = new QVBoxLayout;//垂直布局
    layout->addWidget(button1);
    layout->addWidget(button2);
    layout->addWidget(button3);
    layout->addWidget(button4);
    layout->addWidget(button5);

    window->setLayout(layout);
}

main.cpp

#include "widget.h"
#include <QApplication>
#include"indexwin.h"

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    IndexWin w;
    w.show();

    return a.exec();
}

结果:
 

三:Qt的水平布局 --- H

indexwin.h .cpp

#ifndef INDEXWIN_H
#define INDEXWIN_H

#include <QWidget>
#include<QVBoxLayout>//垂直布局
#include<QHBoxLayout>//水平布局
#include<QPushButton>

class IndexWin : public QWidget
{
    Q_OBJECT
public:
    explicit IndexWin(QWidget *parent = 0);

    QWidget *window;
    QPushButton *button1;
    QPushButton *button2;
    QPushButton *button3;
    QPushButton *button4;
    QPushButton *button5;


signals:

public slots:
};

#endif // INDEXWIN_H
#include "indexwin.h"

IndexWin::IndexWin(QWidget *parent) : QWidget(parent)
{
    this->resize(1500,800);

    window = new QWidget(this);
    button1 = new QPushButton("One");
    button2 = new QPushButton("Two");
    button3 = new QPushButton("Three");
    button4 = new QPushButton("Four");
    button5 = new QPushButton("Five");

    QHBoxLayout *layout = new QHBoxLayout;//水平布局
    layout->addWidget(button1);
    layout->addWidget(button2);
    layout->addWidget(button3);
    layout->addWidget(button4);
    layout->addWidget(button5);

    window->setLayout(layout);
}

结果:

四:布局设计 --- 水平的五个按钮(按钮在window中),垂直的两个窗口(window和window1垂直)

indexwin.h .cpp

#ifndef INDEXWIN_H
#define INDEXWIN_H

#include <QWidget>
#include<QVBoxLayout>//垂直布局
#include<QHBoxLayout>//水平布局
#include<QPushButton>

class IndexWin : public QWidget
{
    Q_OBJECT
public:
    explicit IndexWin(QWidget *parent = 0);

    QWidget *window;
    QPushButton *button1;
    QPushButton *button2;
    QPushButton *button3;
    QPushButton *button4;
    QPushButton *button5;
    QWidget *window1;


signals:

public slots:
};

#endif // INDEXWIN_H
#include "indexwin.h"

IndexWin::IndexWin(QWidget *parent) : QWidget(parent)
{
    this->resize(1500,800);

    //多个窗口不要this
    window = new QWidget();
    button1 = new QPushButton("One");
    button2 = new QPushButton("Two");
    button3 = new QPushButton("Three");
    button4 = new QPushButton("Four");
    button5 = new QPushButton("Five");

    QHBoxLayout *layout = new QHBoxLayout;//水平的按钮
    layout->addWidget(button1);
    layout->addWidget(button2);
    layout->addWidget(button3);
    layout->addWidget(button4);
    layout->addWidget(button5);
    window->setLayout(layout);

    window1= new QWidget();

    //this是最大窗口              垂直的窗口
    QVBoxLayout *vlayout = new QVBoxLayout(this);//两个窗口垂直放置
    vlayout->addWidget(window);
    vlayout->addWidget(window1);
}

main.cpp

#include "widget.h"
#include <QApplication>
#include"indexwin.h"

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    IndexWin w;
    w.show();

    return a.exec();
}

结果:

 在window1中放置一个按钮

#include "indexwin.h"

IndexWin::IndexWin(QWidget *parent) : QWidget(parent)
{
    this->resize(1500,800);

    //多个窗口不要this
    window = new QWidget();
    button1 = new QPushButton("One");
    button2 = new QPushButton("Two");
    button3 = new QPushButton("Three");
    button4 = new QPushButton("Four");
    button5 = new QPushButton("Five");

    QHBoxLayout *layout = new QHBoxLayout;//水平的按钮
    layout->addWidget(button1);
    layout->addWidget(button2);
    layout->addWidget(button3);
    layout->addWidget(button4);
    layout->addWidget(button5);
    window->setLayout(layout);

    window1= new QWidget();
    QPushButton *button1 = new QPushButton("One",window1);//在window1中放置一个按钮

    //this是最大窗口              垂直的窗口
    QVBoxLayout *vlayout = new QVBoxLayout(this);//两个窗口垂直放置
    vlayout->addWidget(window);
    vlayout->addWidget(window1);
}

结果:

 

五: 放置控件的两种方法

1.自定义所有控件(不推荐)

2.动态创建所有控件(推荐)

indexwin.h .cpp

#ifndef INDEXWIN_H
#define INDEXWIN_H

#include <QWidget>
#include<QVBoxLayout>//垂直布局
#include<QHBoxLayout>//水平布局
#include<QPushButton>
#include<QLineEdit>

class IndexWin : public QWidget
{
    Q_OBJECT
public:
    explicit IndexWin(QWidget *parent = 0);

    QWidget *leftwin;
    QWidget *topwin;
    QWidget *indexwin;
    QLineEdit *searchEdit;
    QPushButton *userBtn;
    QPushButton *vipBtn;



signals:

public slots:
};

#endif // INDEXWIN_H
#include "indexwin.h"
#include<QStringList>

IndexWin::IndexWin(QWidget *parent) : QWidget(parent)
{
     this->resize(1500,800);

     leftwin = new QWidget();
     topwin = new QWidget();
     indexwin = new QWidget();

     //方法一:自定义所有控件
     //控件水平
     QHBoxLayout *hboxlayout = new QHBoxLayout(topwin);
     searchEdit = new QLineEdit();
     userBtn = new QPushButton();
     vipBtn = new QPushButton();
     hboxlayout->addWidget(searchEdit);
     hboxlayout->addWidget(userBtn);
     hboxlayout->addWidget(vipBtn);

     //方法二:动态创建所有控件
     //按钮控件垂直
     QVBoxLayout *vboxlayout1 = new QVBoxLayout(indexwin);
     QStringList funBtnlist;
     funBtnlist<<"logo"<<"视频"<<"动漫"<<"电影";
     for(int i=0;i<funBtnlist.size();i++)
     {
         QPushButton *newBtn = new QPushButton(funBtnlist.at(i));
         vboxlayout1->addWidget(newBtn);
     }

     //两个窗口垂直
     QVBoxLayout *vboxlayout = new QVBoxLayout(this);
     vboxlayout->addWidget(topwin);
     vboxlayout->addWidget(indexwin);
}

main.cpp

#include "widget.h"
#include <QApplication>
#include"indexwin.h"

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    IndexWin w;
    w.show();

    return a.exec();
}

结果:
 

举报

相关推荐

001-CSS-水平垂直居中布局

0 条评论