C++ Qt QVBoxLayout显示不出按钮和文本框,只有一个带有菜单的窗口框架,大神求救。
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C++ Qt QVBoxLayout显示不出按钮和文本框,只有一个带有菜单的窗口框架,大神求救。相关的知识,希望对你有一定的参考价值。
//mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
class QMenu;
class QAction;
class QPushButton;
class QLineEdit;
class QVBoxLayout;
class MainWindow : public QMainWindow
Q_OBJECT
public:
MainWindow(QWidget *parent = 0);
~MainWindow();
private:
void createMenus();
void createActions();
void createMain();
private:
QMenu *closeMenu;
QAction *closeAction;
QPushButton *button;
QLineEdit *lineEdit;
QVBoxLayout *layout;
;
#endif // MAINWINDOW_H
//mainwindow.cpp
#include "mainwindow.h"
#include <QAction>
#include <QMenuBar>
#include <QMenu>
#include <QPushButton>
#include <QLineEdit>
#include <QVBoxLayout>
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent)
createActions();
createMenus();
createMain();
MainWindow::~MainWindow()
void MainWindow::createActions()
closeAction = new QAction(tr("&Close"),this);
connect(closeAction,SIGNAL(triggered()),this,SLOT(close()));
void MainWindow::createMenus()
closeMenu = menuBar()->addMenu("Close");
closeMenu->addAction(closeAction);
void MainWindow::createMain()
button = new QPushButton;
lineEdit = new QLineEdit;
layout = new QVBoxLayout;
layout->addWidget(button);
layout->addWidget(lineEdit);
setLayout(layout);
setWindowTitle(tr("Main Window"));
//mian.h
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
改成
QWidget *mainwidget=new QWidget(this);
mainwidget->setLayout(layout);
this->setCentralWidget(mainwidget);
Qt垂直/水平布局管理器(QBoxLayout,QHBoxLayout, QVBoxLayout)
1.解释
QBoxLayout:可以在水平方向或垂直方向上排列控件,由QHBoxLayout、QVBoxLayout所继承。
QHBoxLayout:水平布局,在水平方向上排列控件。
QVBoxLayout:垂直布局,在垂直方向上排列控件。
同时我们注意到水平布局、垂直布局除了构造时的方向(LeftToRight、TopToBottom)不同外,其它均相同。
2.示例代码
2.1QHBoxLayout, QVBoxLayout实现
1 #include "widget.h" 2 #include <QApplication> 3 #include <QPushButton> 4 #include <QHBoxLayout> 5 #include <QVBoxLayout> 6 7 int main(int argc, char *argv[]) 8 { 9 QApplication a(argc, argv); 10 Widget w; 11 w.setWindowTitle("My App");//设置窗口标题 12 13 //创建3个Button 14 QPushButton *button1 = new QPushButton("one"); 15 QPushButton *button2 = new QPushButton("two"); 16 QPushButton *button3 = new QPushButton("three"); 17 18 QHBoxLayout *hlayout = new QHBoxLayout;//创建一个水平布局管理器 19 //将button加入水平布局器 20 hlayout->addWidget(button1); 21 hlayout->addWidget(button2); 22 hlayout->addWidget(button3); 23 24 QVBoxLayout *vlayout = new QVBoxLayout;//创建垂直布局器 25 //将button加入垂直布局器 26 vlayout->addWidget(button1); 27 vlayout->addWidget(button2); 28 vlayout->addWidget(button3); 29 30 w.setLayout(hlayout);//将水平布局器加入主窗口 31 //w.setLayout(vlayout);//将垂直布局器加入主窗口 32 33 w.show(); 34 35 return a.exec(); 36 }
2.2Functions
. setMargin(int)可以设置左、上、右、下的外边距,设置之后,他们的外边距是相同的。
. setContentsMargins(int left, int top, int right, int bottom)与其功能相同,但是可以将左、上、右、下的外边距设置为不同的值。
. setContentsMargins(const QMargins &margins) 设置外边距(同上)
. setSpacing(int) 间距设置
. addStretch()添加了一个伸缩空间(QSpacerItem)。
. setStretchFactor(QWidget *w, int stretch) , setStretchFactor(QLayout *l, int stretch) 设置控件、布局的拉伸系数
以上是关于C++ Qt QVBoxLayout显示不出按钮和文本框,只有一个带有菜单的窗口框架,大神求救。的主要内容,如果未能解决你的问题,请参考以下文章
Qt:如何在 QVBoxLayout 中交替对齐 QTextEdit?