当前位置:编程学习 > wap >>

QT纵向布局 为啥后面的覆盖了前面的widget?

--------------------编程问答--------------------
图片运行效果 --------------------编程问答-------------------- 如果工程建立后只是在构造函数添加了如上代码,那么运行后是正常的。
楼主会不会是在.ui文件中拉了某个控件,这个控件刚好把前面的widget给挡住了? --------------------编程问答-------------------- QVBoxLayout * mainLayout = new QVBoxLayout(this);将这个改为
QVBoxLayout * mainLayout = new QVBoxLayout;

qt 同一时刻只能设置1个布局,设置方式,可以

1.通过构造函数的方式,串指针,像这样
QVBoxLayout * mainLayout = new QVBoxLayout(this);
2.通过setLayout()方式 --------------------编程问答-------------------- http://blog.csdn.net/rabinsong/article/details/8986883可以看看这篇文章,有布局的介绍,一看就明白 --------------------编程问答-------------------- 把你代码中的resize去掉试试 --------------------编程问答-------------------- 都试过 没效果~ --------------------编程问答-------------------- ui->setupUi(this);楼主你这个也相当于安装了布局啊,1次只能有1个布局,其他的就会重叠,作为同1个父窗口的孩子 --------------------编程问答-------------------- QMainWindow,可以设置状态条,内容区,工具栏,菜单条,

都有对应的处理方式,

你要木使用ui设计,

要木使用自动生成界面,使用界面布局器,

QWidget * contentArea = new QWidget(this);
        contentArea->setObjectName("contentWidget");
        contentArea->setStyleSheet("QWidget#contentWidget{background-color:#eee;}");
        contentArea->resize(this->width(),this->height()-70);
        contentArea->setContentsMargins(15, 0, 15, 0);
 
        QWidget * titleBar = new QWidget(this);
        titleBar->setObjectName("titleWidget");
        titleBar->setStyleSheet("QWidget#titleWidget{background-color:#99dd00;}");
        titleBar->resize(this->width(),70);
        titleBar->setContentsMargins(0, 0, 0, 0);
 
        mainLayout->addWidget(titleBar);
        mainLayout->addWidget(contentArea);

正如你这些处理的一样,


通过QMainWindow的
addToolBar ( QToolBar * toolbar )//设置工具条

setCentralWidget ( QWidget * widget )//设置内容区域,

setMenuBar ( QMenuBar * menuBar )

通过这三个函数就可以实现你的功能,
--------------------编程问答-------------------- 代码和UI发生冲突了;要么纯UI(尤其layout),要么纯代码;
下面代码就很正常;
#include <QtGui/QApplication>
#include "mainwindow.h"
#include <QVBoxLayout>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
//    MainWindow w;
//    w.show();
    QWidget *widget = new QWidget();

    widget->resize(720,400);//显示大小
    QVBoxLayout * mainLayout = new QVBoxLayout();

    QWidget * contentArea = new QWidget();
    contentArea->setObjectName("contentWidget");
    contentArea->setStyleSheet("QWidget#contentWidget{background-color:red;}");
    contentArea->setContentsMargins(15, 0, 15, 0);

    QWidget * titleBar = new QWidget();
    titleBar->setObjectName("titleWidget");
    titleBar->setStyleSheet("QWidget#titleWidget{background-color:#99dd00;}");
    titleBar->setContentsMargins(0, 0, 0, 0);

    mainLayout->addWidget(titleBar);
    mainLayout->addWidget(contentArea);
    widget->setLayout(mainLayout);
    widget->show();

    return a.exec();
}
补充:移动开发 ,  Qt
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,