Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delete A Row From QGridLayout

Tags:

c++

qt

All, I am maintaining a QGridLayout of QLabels which show the coefficients of a polynomial. I represent my polynomial using QList<double>.

Each time I update my coefficients, I update my labels. When changing the size of the list, my method does not works well. QGridLayout::rowCount() doesn't update correctly. I am wondering if there's a way to remove rows from a QGridLayout.


Code follows, updating the QGridLayout size with more (or less) 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);
}

The SetFitMethod (it's an initial mockup)

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

The Clearing Method:

void ClearInformational()
{
    while(m_informational->count())
    {
        QLayoutItem * cur_item = m_informational->takeAt(0);
        if(cur_item->widget())
            delete cur_item->widget();
        delete cur_item;
    }
}
like image 211
Constantin Avatar asked Nov 15 '12 20:11

Constantin


People also ask

How do you delete a row in a DataSet?

When using a DataSet or DataTable in conjunction with a DataAdapter and a relational data source, use the Delete method of the DataRow to remove the row. The Delete method marks the row as Deleted in the DataSet or DataTable but does not remove it.

How to remove widgets from layout Qt?

By doing: layout()->removeAt(widget); delete widget; If you use takeAt(index) in a QLayout (or its children), it gives you a QLayoutItem. To access the widget inside, just use widget().

How to remove a widget from a grid pyqt5?

Removing widgets from layout We use convenient function removeWidget() from Qt for the purpose. First, we get the item at index 0 in the grid using itemAt(0) then after we can remove it from the layout using removeItem() .


1 Answers

The issue is that QGridLayout::rowCount() doesn't actually return the number of rows that you can see, it actually returns the number of rows that QGridLayout has internally allocated for rows of data (yes, this isn't very obvious and isn't documented).

To get around this you can either delete the QGridLayout and recreate it, or if you're convinced that your column count won't change, you can do something like this:

int rowCount = m_informational->count()/m_informational->columnCount();
like image 174
RA. Avatar answered Sep 29 '22 10:09

RA.