目录
一:项目重构
二:VBoxLayout 垂直布局
三:HBoxLayout 水平布局
四:水平垂直布局综合应用
五:创建控件
在使用Qt软件的时候,有时候会出现程序异常结束[而自己的代码逻辑是没有问题的]
此时的解决方法:重新构建项目
垂直布局,代码示例
indexwin.h .cpp
#ifndef INDEXWIN_H
#define INDEXWIN_H
#include
#include//垂直布局
#include
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
#include"indexwin.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
IndexWin w;
w.show();
return a.exec();
}
垂直布局,效果展示
水平布局,代码示例
indexwin.h .cpp
#ifndef INDEXWIN_H
#define INDEXWIN_H
#include
#include//垂直布局
#include//水平布局
#include
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
#include//垂直布局
#include//水平布局
#include
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
#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
#include//垂直布局
#include//水平布局
#include
#include
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
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;iaddWidget(newBtn);
}
//两个窗口垂直
QVBoxLayout *vboxlayout = new QVBoxLayout(this);
vboxlayout->addWidget(topwin);
vboxlayout->addWidget(indexwin);
}
main.cpp
#include "widget.h"
#include
#include"indexwin.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
IndexWin w;
w.show();
return a.exec();
}
效果展示