[英]Delete A Row From QGridLayout
全部,我正在維護QGridLayout
的QLabels
,它顯示了多項式的系數。 我使用QList<double>
表示我的多項式。
每次更新系數時,我都會更新標簽。 當改變列表的大小,我的方法沒有效果很好。 QGridLayout::rowCount()
無法正確更新。 我想知道是否有辦法從QGridLayout中刪除行。
代碼如下,用更多(或更少)的QLabels
更新QGridLayout
大小
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;
}
}
問題是QGridLayout::rowCount()
實際上並沒有返回你可以看到的行數,它實際上返回了QGridLayout
為數據行內部分配的行數(是的,這不是很明顯,沒有記錄)。
要解決此問題,您可以刪除QGridLayout
並重新創建它,或者如果您確信您的列數不會更改,您可以執行以下操作:
int rowCount = m_informational->count()/m_informational->columnCount();
好吧,我的解決方案是刪除QGridLayout
中的ClearInformational
我通過創建一個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);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.