Qt QScrollArea and layout in code

                            Qt QScrollArea and layout in code


一、参考文档:
    1. Qt 第六章 QScrollArea类给QWidget添加滚动条
        http://blog.csdn.net/commshare/article/details/6753978
    2. 24小时学会QT编程──窗口的缩放和滚动条的使用 
        http://blog.163.com/vic_kk/blog/static/4947052420083308325238/
    3. QWidget::setLayout: Attempting to set QLayout "" on MainWindow "", which already has a layout
        http://blog.csdn.net/zhuyingqingfen/article/details/6562246
    4. QWidget::setLayout: Attempting to set QLayout "" on MainWindow "", which already has a layout 
        http://yebaoshan.blog.163.com/blog/static/20423116720126267524060/

二、cat main.cpp
    #include "mainwindow.h"
    #include <QApplication>
    #include <QScrollArea>
    
    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
    
        MainWindow mainWindow;
    
        // 加入滚动条
        QScrollArea scrollArea;
        scrollArea.setWidget(&mainWindow);
        scrollArea.show();
    
        return a.exec();
    }

三、cat mainwindow.cpp
    #include "mainwindow.h"
    #include <QSize>
    #include <QPushButton>
    #include <QHBoxLayout>
    #include <QTextEdit>
    #include <QWidget>
    
    MainWindow::MainWindow(QWidget *parent)
        : QMainWindow(parent)
    {
        // 设置大小
        this->setMinimumSize(QSize(300, 300));
        this->setMaximumSize(QSize(300, 300));
        // 设置标题
        this->setWindowTitle("About");
    
        QPushButton *button    = new QPushButton("zengjf");
        QTextEdit   *leftEdit  = new QTextEdit(this);
        QTextEdit   *rightEdit = new QTextEdit(this);
    
        QHBoxLayout *layout    = new QHBoxLayout(this);
        layout->addWidget(leftEdit);
        layout->addWidget(button);
        layout->addWidget(rightEdit);
    
        QWidget *widget = new QWidget(this);
        widget->setLayout(layout);
    
        setCentralWidget(widget);
    }
    
    MainWindow::~MainWindow()
    {
    
    }

 

posted on 2015-11-13 16:12  zengjf  阅读(828)  评论(0编辑  收藏  举报

导航