wxDataViewListCtrl: How to officially get the row number of a new item

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Post Reply
jpo234
Experienced Solver
Experienced Solver
Posts: 70
Joined: Tue Feb 25, 2020 11:34 am

wxDataViewListCtrl: How to officially get the row number of a new item

Post by jpo234 »

Hello all,
if I add a new item to a wxDataViewListCtrl with wxDataViewListCtrl::AppendItem, how am I supposed to get its row number? Unfortunately all the insert or append methods seem to return void...
Right now I'm using wxDataViewListCtrl::GetItemCount, but that doesn't feel right. After all, the documentation doesn't even guarantee that the row numbering actually starts with zero. Without that guarantee I can't reliably get the row number from the count.

Regards
Jorg
User avatar
doublemax@work
Super wx Problem Solver
Super wx Problem Solver
Posts: 474
Joined: Wed Jul 29, 2020 6:06 pm
Location: NRW, Germany

Re: wxDataViewListCtrl: How to officially get the row number of a new item

Post by doublemax@work »

wxDataViewListCtrl::ItemToRow()
https://docs.wxwidgets.org/trunk/classw ... ad30be241f

There is also RowToItem()
jpo234
Experienced Solver
Experienced Solver
Posts: 70
Joined: Tue Feb 25, 2020 11:34 am

Re: wxDataViewListCtrl: How to officially get the row number of a new item

Post by jpo234 »

doublemax@work wrote: Tue May 25, 2021 11:33 am wxDataViewListCtrl::ItemToRow()
https://docs.wxwidgets.org/trunk/classw ... ad30be241f

There is also RowToItem()
That's not really helpful, because it begs the question: How do I get that wxDataViewItem? The only way seems to be via RowToItem() which brings me back to the original question.
ONEEYEMAN
Part Of The Furniture
Part Of The Furniture
Posts: 7459
Joined: Sat Apr 16, 2005 7:22 am
Location: USA, Ukraine

Re: wxDataViewListCtrl: How to officially get the row number of a new item

Post by ONEEYEMAN »

Hi,
How do you add the row/item to the control?

Thank you.
jpo234
Experienced Solver
Experienced Solver
Posts: 70
Joined: Tue Feb 25, 2020 11:34 am

Re: wxDataViewListCtrl: How to officially get the row number of a new item

Post by jpo234 »

ONEEYEMAN wrote: Tue May 25, 2021 12:13 pm Hi,
How do you add the row/item to the control?

Thank you.
With wxDataViewListCtrl::AppendItem.
ONEEYEMAN
Part Of The Furniture
Part Of The Furniture
Posts: 7459
Joined: Sat Apr 16, 2005 7:22 am
Location: USA, Ukraine

Re: wxDataViewListCtrl: How to officially get the row number of a new item

Post by ONEEYEMAN »

Hi,
Can you give the exact code?

Thank you.
jpo234
Experienced Solver
Experienced Solver
Posts: 70
Joined: Tue Feb 25, 2020 11:34 am

Re: wxDataViewListCtrl: How to officially get the row number of a new item

Post by jpo234 »

ONEEYEMAN wrote: Tue May 25, 2021 12:53 pm Hi,
Can you give the exact code?

Thank you.
Sure. Nothing really special:

Code: Select all

    void AppendMsg(const TextMessageWrapper::pointer_type &msgptr) {
        auto tp_to_str = [](const std::chrono::system_clock::time_point &tp) -> std::string {
            std::stringstream ss;
#if 0
                    using namespace date;
                    local_time<std::chrono::seconds> ltp = tp;
                    ss << format("%D %T %Z", floor<std::chrono::seconds>(tp));
                    return ss.str();
#else
            std::time_t tt = std::chrono::system_clock::to_time_t(tp);
            std::tm tm     = *std::localtime(&tt);
            ss << std::put_time(&tm, "%X");
            return ss.str();
#endif
        };
        wxVector<wxVariant> data;
        data.push_back(wxVariant(wxString::FromUTF8(msgptr->getMessage()->get_subject())));
        data.push_back(wxVariant(std::to_string(msgptr->getMessage()->get_message_id())));
        data.push_back(wxVariant(ack_to_str(msgptr->is_acked())));
        data.push_back(wxVariant(tp_to_str(msgptr->get_rx_time())));
        data.push_back(wxVariant(tp_to_str(msgptr->getMessage()->get_timestamp())));

        m_owner->m_msg_data_view->AppendItem(data, msgptr->get_local_id());
        msgptr->set_dataview_row(m_owner->m_msg_data_view->GetItemCount() - 1);
    }

This is what I'm doing right now. What seems iffy is obtaining the row index via GetItemCount(). I need this for future updates of the is_acked column. m_msg_data_view is:

Code: Select all

wxDataViewListCtrl* m_msg_data_view;
User avatar
doublemax@work
Super wx Problem Solver
Super wx Problem Solver
Posts: 474
Joined: Wed Jul 29, 2020 6:06 pm
Location: NRW, Germany

Re: wxDataViewListCtrl: How to officially get the row number of a new item

Post by doublemax@work »

Sorry, i naturally assumed that AppendItem() returns a wxDataViewItem. Apparently it doesn't and looking through the API i don't see any other way than your solution.
jpo234
Experienced Solver
Experienced Solver
Posts: 70
Joined: Tue Feb 25, 2020 11:34 am

Re: wxDataViewListCtrl: How to officially get the row number of a new item

Post by jpo234 »

doublemax@work wrote: Tue May 25, 2021 2:23 pm Sorry, i naturally assumed that AppendItem() returns a wxDataViewItem. Apparently it doesn't and looking through the API i don't see any other way than your solution.
I have raised the topic in the wx-users list: https://groups.google.com/g/wx-users/c/ ... 6FXa82AgAJ
Post Reply