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

Three problems with QCompleter and a QStandardItemModel.



  • Hi Everyone,

    I have three problems with the QCompleter which I will describe in three different sections quickly, Hopefully someone can help me sort them out.

    I am using Qt 5.2.0 on OSX 10.9.2.

    The data I am working with is essentially;
    [code]
    "MSFT", "Microsoft Corporation"
    "AMD", "Advanced Micro Devices, Inc."
    "KORS", "Michael Kors Holdings Limited"
    "MU", "Micron Technology Inc."
    "NEM", "Newmont Mining Corporation"
    "GDX", "Market Vectors Gold Miners ETF"
    "NUGT", "Direxion Daily Gold Miners Bull 3X Shrs"
    "GIS", "General Mills, Inc."
    "MCHP", "Microchip Technology Inc."
    "SDT", "SandRidge Mississippian Trust I"
    [/code]

    And the search string is Mic which filters the list down accordingly.

    Problem 1:

    I have a QStandardItemModel set up like 'Symbol','Name' using the data above. I.e;

    [code]
    while(it.hasNext()){
    it.next();
    standardlist->setItem(standardlist->rowCount(), 0, new QStandardItem(it.value().property("symbol").toString()));
    standardlist->setItem(standardlist->rowCount(), 1, new QStandardItem(it.value().property("name").toString()));
    }
    [/code]
    ('it' is a QScriptValueIterator parsing a JSON response and standardlist is a QStandardItemModel.)

    If I assign the model to the QCompleter but set the completion column to 1 the dropdown shows up as empty. If I cycle through the (invisible) choices with the cursor the correct text is displayed in the LineEdit the QCompleter is assigned to.

    [img]http://websolutions.nu/screenshot1.png[/img]

    I can get around this by swapping the items around and keeping the completion column at 0.
    Eg.
    [code]
    standardlist->setItem(standardlist->rowCount(), 1, new QStandardItem(it.value().property("symbol").toString()));
    standardlist->setItem(standardlist->rowCount(), 0, new QStandardItem(it.value().property("name").toString()));
    [/code]
    (Note the index of the items)

    Not sure if this is desired behaviour.

    Problem 2:
    When the options are being displayed by the QCompleter. If I press the cursor up and cycle backwards through the list the options get selected out of order.
    For example if there are 5 options and I press up once I get the last option 5 (because we looped the list) if I press up again I get option 2, press up again I get option 4 up again I get option 1 and so on.

    Problem 3:

    This is the road block I am experiencing.
    When a selection is made from the QCompleter I need to get the symbol of the Company name that was selected. To do this I am getting the index from the slot:
    [code]connect(symbolcompleter, SIGNAL(activated(QModelIndex)), this, SLOT(getindex(QModelIndex)));[/code]

    and using the code:
    [code]
    void GChartMenu::getindex(QModelIndex index)
    {
    qDebug() << "GetIndex";
    qDebug() << "Completer index:" << index.row() << index.column();
    QModelIndex index_table_model = qobject_cast<QAbstractProxyModel*> (
    symbolcompleter->completionModel())->mapToSource(index);
    qDebug() << "Model index:" << index_table_model.row();
    QModelIndex mi = standardlist->index(index_table_model.row(), 1);
    QVariant PK = standardlist->data(mi);

    if(PK.isValid()){
        qDebug() << "Key:" << PK.toString();
    }
    

    }
    [/code]

    I can never get the contents of column 1 (QmodelIndex mi... line) but if I change this to;
    [code]
    QModelIndex mi = standardlist->index(index_table_model.row(), 0);
    [/code]

    I can get the correct company name every time, so I am certain my indices are correct. I have also piped them out using qDebug statements and verified the indices are correct by eye.

    Appreciate the suggestions as I have run out of ideas.



  • Anyone have any ideas?


Log in to reply