C++ 从QGridLayout中删除一行

C++ 从QGridLayout中删除一行,c++,qt,C++,Qt,总之,我正在维护一个QLabels的QGridLayout,它显示多项式的系数。我使用QList表示多项式 每次更新系数时,我都会更新标签。更改列表的大小时,我的方法不起作用QGridLayout::rowCount()未正确更新。我想知道是否有办法从QGridLayout中删除行 代码如下,使用更多(或更少)的QLabels更新QGridLayoutsize int count = coefficients->count(); //coefficients is a QList<

总之,我正在维护一个
QLabels
QGridLayout
,它显示多项式的系数。我使用
QList
表示多项式

每次更新系数时,我都会更新标签。更改列表的大小时,我的方法不起作用<代码>QGridLayout::rowCount()未正确更新。我想知道是否有办法从QGridLayout中删除行


代码如下,使用更多(或更少)的QLabels更新QGridLayoutsize

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);
}

好的,我的解决方案是在
ClearInformational

中删除
QGridLayout
,问题是
QGridLayout::rowCount()
实际上并不返回您可以看到的行数,而是返回
QGridLayout
内部为数据行分配的行数(是的,这不是很明显,也没有记录在案)

要解决这个问题,您可以删除
QGridLayout
并重新创建它,或者如果您确信列数不会改变,您可以执行以下操作:

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

我通过创建一个QVBoxLayout(用于行)解决了这个问题,并在其中添加了QHBoxLayout(用于列)。然后在QHBoxLayout中插入我的小部件(在一行中)。通过这种方式,我可以很好地删除行-总体行数正常工作。此外,我还获得了一个insert方法,借助该方法,我可以将新行插入特定位置(所有内容都正确地重新排序/编号)

示例(仅来自head):


是的,经过一些紧张的调试,我也发现了…我喜欢你的除法解决方案,但它对我的工作非常具体。可能是重复的
void ClearInformational()
{
    while(m_informational->count())
    {
        QLayoutItem * cur_item = m_informational->takeAt(0);
        if(cur_item->widget())
            delete cur_item->widget();
        delete cur_item;
    }
}
int rowCount = m_informational->count()/m_informational->columnCount();
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);