zoukankan      html  css  js  c++  java
  • Attempting to add QLayout "" to MainWindow "", which already has a layout

    给QWidget或者QDialog设置布局的时候方式很简单。创建好一个布局:mainLayout,然后不停地把各个控件往mainLayout里面放,最后调用setLayout(mainLayout)就行了。

    QMainWindow中使用这个方法的时候却不管用,因为QMainWindow是默认有layout的,所以再次设置layout会失效。

    会出现这种提示:

    QWidget::setLayout: Attempting to set QLayout "" on MainWindow "", which already has a layout
    这句话的意思是说,你已经给MainWindow设置过一个布局了,再设置一个会出错。

    该如何给QMainWindow正确地设置布局呢
    要想QMainWidget创建布局,合理的步骤应该是这样的:

    第一步创建一个QWidget实例,并将这个实例设置为centralWidget:

    然后创建一个主布局mainLayout,并把所需要的所有控件都往里面放(工具栏、菜单栏、状态栏除外):
    ...

    最一步就是将widget的布局设置为mainLayout

        widget = new QWidget();  
        this->setCentralWidget(widget);  
        cbox = new QCheckBox(this);  
        cbox->setText("choose");  
        cbox->setChecked(false);  
        button = new QPushButton(this);  
        QVBoxLayout *layout = new QVBoxLayout(this);  
          
        layout->addWidget(cbox);  
        layout->addWidget(button);  
          
        widget->setLayout(layout);  
    
  • 相关阅读:
    Kth element of Two Sorted Arrays
    Populating Next Right Pointers in Each Node I && II
    Average waiting time of SJF and Round Robin scheduling
    LRU Cache
    Calculate H-index
    Get Level of a node in a Binary Tree
    Two Sum
    Intersection of Two Linked Lists
    Symmetric Tree
    Lowest Common Ancestor of Binary (Search) Tree
  • 原文地址:https://www.cnblogs.com/oakentree/p/4433086.html
Copyright © 2011-2022 走看看