-
Notifications
You must be signed in to change notification settings - Fork 77
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
QModelIndex::internalId() needed for trees #719
Comments
Thanks for taking the time to report this issue :-) We haven't wrapped those methods in CXX-Qt yet, we could add support for the internalPointer quite easily by the looks of things or if added support for Currently you could extend the You could define your own type to extend the #[repr(transparent)]
pub struct QModelIndexExtended(cxx_qt_lib::QModelIndex);
unsafe impl cxx::ExternType for QModelIndexExtended {
type Id = cxx::type_id!("QModelIndex");
type Kind = cxx::kind::Trivial;
} Then in your bridge define the type with additional methods unsafe extern "C++" {
include!("cxx-qt-lib/qmodelindex.h");
type QModelIndex = super::QModelIndexExtended;
type c_void;
// TODO: could build quintptr as a type then this would work too
// or create a trampoline from C++ which casts to a type CXX understands
// #[rust_name = "internal_id"]
// fn internalId(self: &QModelIndex) -> quintptr;
#[rust_name = "internal_pointer"]
unsafe fn internalPointer(self: &QModelIndex) -> *mut c_void;
} Then in a header that is included you would also need These are all normal CXX types so follow the normal CXX rules. But I also think that we should consider adding built in support for the |
Thanks for providing code, but unfortunetely I can not get it to compile.
Do you have any ideas? |
In a C++ header you need to put
using c_void = void; Then in the bridge you need to include it #[cxx_qt::bridge]
mod ffi {
...
unsafe extern "C++ {
include!("my_header.h");
}
...
} Then you need to make sure that the header is in the C++ include path, so in the fn main() {
CxxQtBuilder::new()
...
.cc_builder(|cc| {
// needs to point to the folder where the header is
cc.include("cpp");
})
...
.build();
} |
Unfortunately this currently requires the use of our own c_void type. See: dtolnay/cxx#1049 Closes KDAB#719
Unfortunately this currently requires the use of our own c_void type. See: dtolnay/cxx#1049 Closes KDAB#719
Unfortunately this currently requires the use of our own c_void type. See: dtolnay/cxx#1049 Closes KDAB#719
Unfortunately this currently requires the use of our own c_void type. See: dtolnay/cxx#1049 Closes KDAB#719
Unfortunately this currently requires the use of our own c_void type. See: dtolnay/cxx#1049 Closes #719
Possible tasks:
quintptr
so it can be used easily in bridges https://doc.qt.io/qt-6/qttypes.htmlc_void
as suggested in Passconst void*
to C++ binded function dtolnay/cxx#1049QModelIndex::internalId
Original description:
I am trying to create a tree-model to provide data to QML.
The normal way is to use QAbstractItemModel, but there is a problem:
The view might request an items parent by calling QModelIndex::parent().
In a list or table, where items never have parents, this would simply return an invalid QModelIndex().
The current item can then be found simply by it's row and column.
In a tree, the parent is needed as well, for which I need to know the current Item, for which I need the parent ...
That problem is usually solved by storing a pointer or id in the QModelIndex, which is set by QAbstractItemModel::createIndex() and retrieved by QModelIndex::internalId() (or internalPointer()) -- but cxx_qt does not provide access to these methods.
I thought about a workaround by following the parents up to the root level, remembering all the rows and then down again to find the right item, but as the documentation of QAbstractItemModel::parent states:
so that would not work.
The only other way I can think of, that might possibly work, is subclassing QModelIndex, which I'd like to avoid.
The text was updated successfully, but these errors were encountered: