QWidget :: setLayout错误:尝试设置已有布局的QLayout [...]

时间:2010-10-03 15:00:18

标签: qt qt4 qlayout

执行时(没有编译错误)我进入控制台

  

QWidget :: setLayout:尝试在CGSearchResult上设置QLayout“”,   已经有布局

我使用以下代码:

CGSearchResult::CGSearchResult(QWidget *parent) : QWidget(parent)
{

    initControls();
    SetTableContent();

}

void CGSearchResult::initControls()
{


   backButton = new QPushButton(tr("&Back"));
   connect(backButton, SIGNAL(clicked()), this, SLOT(showHome()));

   model=new QStandardItemModel();


         QWidget::setFont(QFont("Courier New", 8, QFont::Bold));

        searchTable = new QTableView(this);
        searchTable->showGrid();

        searchTable->resize(720,400);
        searchTable->horizontalHeader()->setDefaultSectionSize(170);
        searchTable->verticalHeader()->setDefaultSectionSize(50);
        searchTable->verticalHeader()->hide();
        searchTable->horizontalHeader()->setResizeMode(QHeaderView::Fixed);
        searchTable->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);


    QGridLayout *layout = new QGridLayout();
    layout->addWidget(backButton, 0, 0, 1, 1);
    layout->addWidget(searchTable, 2, 0, 1, 1);

    setLayout(layout);


}

1 个答案:

答案 0 :(得分:6)

http://qt-project.org/doc/qt-4.8/qwidget.html#setLayout

  

如果此窗口小部件上已安装布局管理器,QWidget将不允许您安装另一个布局管理器。您必须首先删除现有布局管理器(由layout()返回),然后才能使用新布局调用setLayout()。