2012-11-15 99 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);