首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从QGridLayout中删除A行

从QGridLayout中删除A行
EN

Stack Overflow用户
提问于 2012-11-15 20:52:02
回答 3查看 6.5K关注 0票数 8

所有,我保持一个QGridLayout of QLabels,它显示了多项式的系数。我用QList<double>表示多项式。

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

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

代码语言:javascript
复制
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 (它是一个初始的模型)

代码语言:javascript
复制
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);
    }
}

清算方法:

代码语言:javascript
复制
void ClearInformational()
{
    while(m_informational->count())
    {
        QLayoutItem * cur_item = m_informational->takeAt(0);
        if(cur_item->widget())
            delete cur_item->widget();
        delete cur_item;
    }
}
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2012-11-15 21:45:34

问题是,QGridLayout::rowCount()实际上并不返回您可以看到的行数,而是返回QGridLayout内部为数据行分配的行数(是的,这不是很明显,也没有文档化)。

为了解决这个问题,您可以删除QGridLayout并重新创建它,或者如果您确信您的列计数不会改变,您可以这样做:

代码语言:javascript
复制
int rowCount = m_informational->count()/m_informational->columnCount();
票数 7
EN

Stack Overflow用户

发布于 2013-04-24 07:07:32

我通过创建一个QVBoxLayout (行)来解决这个问题,其中我添加了QHBoxLayout (对于列)。然后在QHBoxLayout中插入我的小部件(在一行中)。通过这种方式,我能够很好地删除行--总的行计数是正常工作的。除此之外,我还得到了一个insert方法,由于该方法,我能够将新行插入到特定的位置(一切都被正确地重新排序/重新编号)。

示例(仅来自头部):

代码语言:javascript
复制
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);
票数 1
EN

Stack Overflow用户

发布于 2012-11-15 21:43:57

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13405997

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档