【问题标题】:Delete A Row From QGridLayout从 QGridLayout 中删除一行
【发布时间】:2013-04-25 03:19:54
【问题描述】:

所有,我正在维护 QLabelsQGridLayout,它显示多项式的系数。我使用QList<double> 表示我的多项式。

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


代码如下,将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(它是一个初始模型)

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

【问题讨论】:

标签: c++ qt


【解决方案1】:

问题在于 QGridLayout::rowCount() 实际上并没有返回您可以看到的行数,它实际上返回了 QGridLayout 内部为数据行分配的行数(是的,这不是很明显且没有记录)。

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

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

【讨论】:

  • 是的,经过一番激烈的调试,我也发现了这一点......我喜欢你的除法解决方案,但它对我的工作非常具体。
【解决方案2】:

我通过创建 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);

【讨论】:

    【解决方案3】:

    好吧,我的解决方案是同时删除 ClearInformational 中的 QGridLayout

    【讨论】:

      猜你喜欢
      • 2011-07-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-05-03
      • 1970-01-01
      • 2018-12-26
      • 2013-04-29
      • 2016-01-05
      相关资源
      最近更新 更多