Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

[SOLVED] how to get the QmodelIndex to a QString?



  • i am trying to get the current index of the listwidget but i am having problems. when i type
    @
    if (ui->userListWidget->currentIndex() == 0)@

    i get an error: no match for 'operator==' in 'QAbstractItemView::currentIndex() const() == 0'. candidates are: bool QModelIndex::operator==(const QModelIndex&) const

    when i type
    @
    QString temp = ui->userListWidget->currentIndex(); @

    to get the string, i get the error: conversion from 'QModelIndex' to non-scalar type 'QString' requested.

    how to get the QmodelIndex to a QString?

    [edit, please add @ code tags to make your post easier to read, Eddy]



  • "The docs":http://developer.qt.nokia.com/doc/qt-4.8/qabstractitemview.html#currentIndex say that currentIndex returns a QModelIndex

    So you could use it's "isValid member":http://developer.qt.nokia.com/doc/qt-4.8/qmodelindex.html#isValid



  • i am using...
    @bool QModelIndex::isValid () const@

    i am getting the error...
    no matching function for call to 'QTextEdit::append(bool)'

    i am trying to print the results to the textEdit



  • Please show us the relevant code. We don't have a cristal ball to see where that textEdit comes from ;)



  • ok Eddy. here is the code. thank you.

    @ QModelIndex temp = ui->userListWidget->currentIndex();
    textEdit[0]->append(temp.isValid());@



  • I don't quiet understand what you want to achieve generally speaking. But if it is your goal to show the row number of the selected item you could use "currentRow":http://developer.qt.nokia.com/doc/qt-4.8/qlistwidget.html#currentRow-prop.

    This gives you an int, which you can use in your textEdit.
    Is that what you want?



  • What is "the string"? What do you want to append to your text edit? If you have a snippet of code that does not work, then please tell your fellow forumistas what you want to achieve. It's hard to guess this from broken code. I do have a guess and I'm pretty sure I now what you want to do, but I want to hear that from you.



  • lets say there are three items in the listwidget. when a user clicks the second item, i want to get the index from it. so the code would return number 2. the listwidget.currentindex does return it but in a modelindex. i need the number to be in a string so that i can work with it.

    after reading Eddy's last post, i tested the currentRow and it is just what i was looking for. i marked this topic as solved.


Log in to reply