wxGrid析构函数在

时间:2015-09-17 21:22:01

标签: wxwidgets wxgrid

我是wxWidgets的新手,虽然我已经能够使应用程序运行起来并且运行得相当顺利,直到这一点。对于主窗口,我在wxPanel中使用wxGrid。一切顺利,直到我关闭程序。

提前感谢任何见解。

网格是从wxPanel派生的类的成员:

class FormDataView
        : public wxPanel
    {

public:
    FormDataView(wxWindow* parent);
    virtual ~FormDataView();

private:
        wxGrid* grid_;
    }

并在构造函数中创建。网格的数据来自另一个线程,因此我创建了一个实际写入数据的自定义事件。

wxDEFINE_EVENT(FORMDATAVIEW_UPDATE, wxThreadEvent);


FormDataView::FormDataView(wxWindow* parent)
        :   wxPanel(parent,wxID_ANY )
    {

    wxBoxSizer* mbox = new wxBoxSizer(wxVERTICAL);
    grid_ = new wxGrid(this, wxID_ANY );
    grid_->CreateGrid(0, 0);


    mbox->Add(grid_,wxSizerFlags(1).Expand());


    Bind(FORMDATAVIEW_UPDATE, &FormDataView::onDataUpdate, this);

    }


///
/// This function is called by a child thread when data is received.
///
void
FormDataView::onDataReceived(IFORMATTERBASE_PFONDATARECEIVED_ARGS)
    {
    newHeaders_ = headers;
    newData_ = data;

    wxThreadEvent* evt = new wxThreadEvent(FORMDATAVIEW_UPDATE);
    evt->SetString("Yo.");
    wxQueueEvent(this, evt);

    }


///
/// Called by the event loop. This function puts the data
/// into the grid.
///
void 
FormDataView::onDataUpdate(wxThreadEvent& evt)
    {
    FormatterStringList& headers = newHeaders_;
    FormatterStringList& data = newData_;

    if (grid_->GetNumberRows() <= 0)
        {

        wxGridCellAttr* attr = new wxGridCellAttr();
        attr->SetReadOnly(true);
        attr->SetAlignment(wxALIGN_CENTRE, wxALIGN_CENTRE);
        for (size_t i = 0; i<headers.size(); ++i)
            {

            if (grid_->GetNumberCols() <= 0)
                grid_->InsertCols();
            else
                grid_->AppendCols();


            grid_->SetColLabelValue(i, headers[i].data());
            grid_->SetColAttr(i, attr);
            }
        }


    // suspend redrawing while we add data.
    grid_->BeginBatch();

    // insert a new row at the top of the table
    grid_->InsertRows(
        0, // position
        1, // number of rows to insert
        true); // update labels (not current used)


    for (size_t i = 0; i<headers.size(); ++i)
        {

        if (data.size() < i)
            {
            grid_->SetCellValue(0, i, "");
            }
        else
            {
            grid_->SetCellValue(0, i, data[i].data());
            }
        }

    // resume redrawing.
    grid_->EndBatch();
    }

一切运行正常,但当我关闭时,我收到以下消息。我已经指出了断点发生的行。是否有一些用于清除我应该遵循的网格数据的序列不足?

wxGrid::CellSpan
wxGrid::GetCellSize( int row, int col, int *num_rows, int *num_cols ) const
{
    wxGridCellAttr *attr = GetCellAttr(row, col);
    attr->GetSize( num_rows, num_cols );
    attr->DecRef();

>>>>>>>    if ( *num_rows == 1 && *num_cols == 1 )
        return CellSpan_None; // just a normal cell

    if ( *num_rows < 0 || *num_cols < 0 )
        return CellSpan_Inside; // covered by a multi-span cell

    // this cell spans multiple cells to its right/bottom
    return CellSpan_Main;
}

1 个答案:

答案 0 :(得分:0)

问题出在我创建列属性的位置。我为每个列重用了相同的列属性实例,但每列都需要有自己的实例。

在:

if (grid_->GetNumberRows() <= 0)
    {

    ///
    /// NO! The columns will share the same cell attribute
    /// instance.
    ///
    wxGridCellAttr* attr = new wxGridCellAttr();
    attr->SetReadOnly(true);
    attr->SetAlignment(wxALIGN_CENTRE, wxALIGN_CENTRE);
    for (size_t i = 0; i<headers.size(); ++i)
        {

        if (grid_->GetNumberCols() <= 0)
            grid_->InsertCols();
        else
            grid_->AppendCols();


        grid_->SetColLabelValue(i, headers[i].data());
        grid_->SetColAttr(i, attr);
        }
    }

正确:

if (grid_->GetNumberRows() <= 0)
    {
    for (size_t i = 0; i<headers.size(); ++i)
        {

        if (grid_->GetNumberCols() <= 0)
            grid_->InsertCols();
        else
            grid_->AppendCols();


        grid_->SetColLabelValue(i, headers[i].data());

        ///
        /// Each column will have its own cell attribute.
        /// Supposedly, the column will take ownership of this
        /// instance.
        ///
        wxGridCellAttr* attr = new wxGridCellAttr();
        attr->SetReadOnly(true);
        attr->SetAlignment(wxALIGN_CENTRE, wxALIGN_CENTRE);
        grid_->SetColAttr(i, attr);
        }
    }