2012-11-15 101 views
5

全部,我保留QGridLayoutQLabels,它们显示多项式的系数。我代表我的多项式使用QList<double>从QGridLayout中删除一行

每次我更新系数时,我都会更新我的标签。当更改列表的大小时,我的方法不是工作正常。 QGridLayout::rowCount()不能正确更新。我想知道是否有办法从QGridLayout中删除行。


代码如下,更多(或更少)QLabels

int count = coefficients->count(); //coefficients is a QList<double> * 
if(count != (m_informational->rowCount() - 1)) //m_information is a QGridLayout 
{ 
    SetFitMethod(0); 
    for(int i = 0; i < count; ++i) 
    { 
     QLabel * new_coeff = new QLabel(this); 
     new_coeff->setAlignment(Qt::AlignRight); 
     m_informational->addWidget(new_coeff, i+1, 0); 
     QLabel * param = new QLabel(this); 
     param->setAlignment(Qt::AlignLeft); 
     param->setText(QString("<b><i>x</i><sup>%2</sup></b>").arg(count-i-1)); 
     m_informational->addWidget(param, i+1, 1); 
     QSpacerItem * space = new QSpacerItem(0,0,QSizePolicy::Expanding); 
     m_informational->addItem(space, i+1, 1); 
    } 

    m_informational->setColumnStretch(0, 3); 
    m_informational->setColumnStretch(1, 1); 
    m_informational->setColumnStretch(2, 1); 
} 

的SetFitMethod更新QGridLayout尺寸(它是一个初始实体模型)

void SetFitMethod(int method) 
{ 
    ClearInformational(); 
    switch(method) 
    { 
    case 0: //Polynomial fit 
     QLabel * title = new QLabel(this); 
     title->setText("<b> <u> Coefficients </u> </b>"); 
     title->setAlignment(Qt::AlignHCenter); 
     m_informational->addWidget(title,0,0,1,3, Qt::AlignHCenter); 
    } 
} 

清算方法:

void ClearInformational() 
{ 
    while(m_informational->count()) 
    { 
     QLayoutItem * cur_item = m_informational->takeAt(0); 
     if(cur_item->widget()) 
      delete cur_item->widget(); 
     delete cur_item; 
    } 
} 
+0

后[从QGridLayout卸下小部件(http://stackoverflow.com/questions/5395266/removing-widgets-from-qgridlayout) – emkey08

回答

2

的问题是,QGridLayout::rowCount()实际上没有返回行,你可以看到的数字,它实际上返回QGridLayout已在内部分配给行的行数的数据(是的,这不是很明显,没有记录)。

要解决这个问题,你可以删除QGridLayout并重新创建它,或者如果你确信你的列数都不会改变,你可以做这样的事情:

int rowCount = m_informational->count()/m_informational->columnCount(); 
+0

没错的可能重复一些激烈的调试,我想到了这一点......我喜欢你的分裂解决方案,但它对我的工作非常具体。 – Constantin

0

好了,我的解决办法是也删除QGridLayoutClearInformational

0

我解决了这个通过创建一个QVBoxLayout(用于行),并在此范围内添加QHBoxLayout(用于列)。在QHBoxLayout中,我插入了我的小部件(在一行中)。通过这种方式,我能够很好地移除行 - 整体行数正常运行。除此之外,我还得到了一个插入方法,多亏了我可以插入新的行到特定的位置(一切正确地重新排序/重新编号)。

实施例(仅来自头):

QVBoxLayout *vBox= new QVBoxLayout(this); 

//creating row 1 
QHBoxLayout *row1 = new QHBoxLayout(); 
QPushButton *btn1x1 = new QPushButton("1x1"); 
QPushButton *btn1x2 = new QPushButton("1x2"); 
row1->addWidget(btn1x1); 
row1->addWidget(btn1x2); 
//adding to vBox - here you can use also insertLayout() for insert to specific location 
vBox->addlayout(row1); 

//creating row 2 
QHBoxLayout *row2 = new QHBoxLayout(); 
QPushButton *btn2x1 = new QPushButton("2x1"); 
QPushButton *btn2x2 = new QPushButton("2x2"); 
row2->addWidget(btn2x1); 
row2->addWidget(btn2x2); 
//adding to vBox - here you can use also insertLayout() for insert to specific location 
vBox->addlayout(row2);