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

Searching for an element in qtableview in qt



  • Hello Guys!
    Yes still the qtableview. :(
    So, i have a qtableview with a table, i am just show some columns not all. I creat a QlineEdit where i start typing it will start searching for a compare text in a special column (if just first characters is the same its okay not problem if there are something after these). But i dont know what is happening because sometimes working sometimes jump the selecton to the wrong places....
    here is my "searching code"

    for (int i =0;i<model->rowCount();i++)
        {
            if(src.toUpper() == view->model()->index(i,model->fieldIndex(relevantCol)).data().toString().toUpper().mid(0,src.count()))
            {
                view->setCurrentIndex(view->model()->index( i, model->fieldIndex(relevantCol)));
    
                catchIt = true;
                break;
            }
        }
    

    src is a qstring what is in the qLineEdit. the model is QSqlTableModel.
    relevantCol is the name of the colum what is relevant.
    I checked that with qDebug() when catch the first good compare that every time is ok. But the selection is not.
    Any ideas what could be the problem?
    Sorry if its a lame question. After it i will have another question about qtableview but my promise it will be the last one! :)


  • Lifetime Qt Champion

    Hi,

    It looks like you are trying to re-invent QSortFilterProxyModel.



  • Sorry i am really lame in QT in this time so can u give me an example for this solution? If u have enough of time and mood for it.


  • Lifetime Qt Champion

    There are two examples linked in the details of the class. The basic filter model example shows a simple implementation.



  • @SGaist I saw these examples but I didn't get smarter. :( My table is a qtableview and my model is a QSqlTableModel furthermore
    I just jump to the row (with row selection) not hide the others row.
    I checked that with qDebug() when catch the first good compare that every time is ok so the if section is okay. But the selection is not always especially with more word after number (for example: Something 1...)
    Its possible i am blind, but these examples are really different than mine. :(
    Any other idea or something else help? Sorry if i am too lame :(


  • Lifetime Qt Champion

    My bad ! I misunderstood your goal.

    Did you already check the model's match function ?



  • @SGaist Yeah thank you i did check this match function but if i understand well its match the cell contents with something. But this is working in my code too :( the problem is the selection in qtableview is gone sometimes wrong row. I checked in qDebug() the row number is good (so the i is the correct row number). Sorry i am a little lame and my engish skills too. i try the show it with pictures so:
    2020-11-17_22h30_56.png
    its correct yet. ( but i dont understand why not the first correct line is selected btw..)
    2020-11-17_22h31_47.png
    Its totaly not correct.
    2020-11-17_22h36_32.png
    Its correct again (and the first correct line) and it will remain correct until the end..
    So i dont know.. any idea whats happening?
    btw I think the problem is in this line:

    view->setCurrentIndex(view->model()->index( i, model->fieldIndex(relevantCol)));
    

    relevantCol is a qstring that contains the name of the important column.
    model QSqlTableModel, and the view is QTableView. :(
    Sorry if i am incomprehensible :(
    Yeah and the i number is always correct



  • @Kaguro
    Given that all you wish to do is move the current index --- which should move the currently selected item --- to a new row, not perform filtering. One thing: you use both model-> and view-model()->. Are those two the same?



  • @JonB i dont really know butview->model() has no setCurrentIndex function. Or this has something function that do this also?



  • Some Update:
    If i change this line:

    view->setCurrentIndex(view->model()->index( i, model->fieldIndex(relevantCol)));
    

    into this:

    view->setCurrentIndex(view->model()->index( i, 0));
    

    The previous picture what i added become correct so:
    2020-11-18_09h22_20.png
    2020-11-18_09h22_36.png
    2020-11-18_09h25_04.png
    but after this, not correct again:
    2020-11-18_09h25_24.png
    I think for some reason it start looking for the first column after the space, but why?



  • @Kaguro said in Searching for an element in qtableview in qt:

    i dont really know butview->model() has no setCurrentIndex function. Or this has something function that do this also?

    If i change this line:
    view->setCurrentIndex(view->model()->index( i, model->fieldIndex(relevantCol)));

    into this:
    view->setCurrentIndex(view->model()->index( i, 0));

    The previous picture what i added become correct so:

    This indicates that the model you are using in model->fieldIndex(relevantCol) to find the column is not the same model as the view->model() you are using to read the data from. It's worrying that you do not know which is what!

    Break your statements down so you always check for errors, e.g.

    view->setCurrentIndex(view->model()->index( i, model->fieldIndex(relevantCol)));
    // Line above does not check for errors
    int fieldIndex = model->fieldIndex(relevantCol);
    Q_ASSERT(fieldIndex >= 0);
    QModelIndex index = view->model()->index( i, fieldIndex);
    Q_ASSERT(index.isValid());
    view->setCurrentIndex(index);
    

    You have two places where you were using model versus view->model(). Sort these out, the statements should be using the same model throughout the statements. Then re-paste the code you are now using.

    Put in qDebug() statements. We want to see what strings are being compared, what the row number is, etc. That is what is necessary when debugging behaviour we don't understand.



  • @JonB I think i used your suggestions now and the debug output is:
    5 -->fieldIndex
    QModelIndex(0,5,0x0,QSqlTableModel(0x1dcf8880)) -->index
    "CO" --> modal adat
    "AK" -->searching string
    5 -->fieldIndex
    QModelIndex(1,5,0x0,QSqlTableModel(0x1dcf8880)) -->index
    "AK" --> modal adat
    "AK" -->searching string

    The code:

    for (int i =0;i<model->rowCount();i++)
        {
            view->setCurrentIndex(view->model()->index( i, model->fieldIndex(relevantCol)));
            // Line above does not check for errors
            int fieldIndex = model->fieldIndex(relevantCol);
            Q_ASSERT(fieldIndex >= 0);
            QModelIndex index = view->model()->index( i, fieldIndex);
            Q_ASSERT(index.isValid());
            view->setCurrentIndex(index);
    
            qDebug()<<fieldIndex<< "-->fieldIndex";
            qDebug()<<index<< "-->index";
            qDebug()<<index.data().toString().toUpper().mid(0,src.count())<<"-->" <<"modal adat";
            qDebug()<<src.toUpper()<<"-->searching string";
    
            if(src.toUpper() == view->model()->index(i,model->fieldIndex(relevantCol)).data().toString().toUpper().mid(0,src.count()))
            {
                view->setCurrentIndex(view->model()->index( i, model->fieldIndex(relevantCol)));
                catchIt = true;
                break;
            }
        }
    

    and the Giu:
    2020-11-18_10h55_06.png

    Or i missunderstand you? :(



  • @Kaguro
    Just looking at your code, I wrote

    You have two places where you were using model versus view->model(). Sort these out, the statements should be using the same model throughout the statements.

    Yet your code still uses both. You simply have not acted on what I said.

    Furthermore, you have simply copied & pasted my code where I tell you in a comment what is wrong, and you still copy the wrong code, pasting verbatim. You need to understand what you are doing and what people are saying.



  • @JonB okay sorry for that , i understand you partly. But i checked with Qdebug everything. Everything is the same the model the index the searching string the resoult strings everything. Just one thing what is not correct every time just sometimes... and that is the selection row. So the output and everything is okay just my blue row selection is not for everything.
    I used an eventfilter on the QtableView, when i start typeing the QLineEdit on the top will show and serch the text in the "MEGNEV" column textes.
    And i changed it now to just QLineedit Textchanged event searching for it. And it works. So something went wrong when i tried it from eventfilter in Qtableview. The code was this:

    bool TableEditor::eventFilter(QObject *obj, QEvent *event)
    {
                search->show();
                QString content = search->text();
    
                if(keyEvent->key()==Qt::Key_Backspace)
                {
                    content = search->text().remove(content.count()-1,1);
                    if(search->text().count()==1 || search->text().isEmpty())
                        search->hide();
                }
                else
                   content = search->text()+keyEvent->text();
    
                search->setText(content);
                SearchAlgorithm(content);
            }
    
        }
    
        return QObject::eventFilter(obj, event);
    }
    

    And this was the install:

    this ->view->viewport()->installEventFilter(this);
    

    So i would like to achive it from the eventfilter, but i saw now just with the QLineEdit textChanged its working perfectly.
    I tried to guess what is the problem by myself.
    Thanks your hellping @JonB ! And really sorry for that i made it so hard. :(
    Thanks your helping again! And your patience!


Log in to reply