PDA

View Full Version : QCcompleter , colum probelm



deepinlife
9th April 2008, 05:36
guys i want to make a lineEdit complete its field from a QTableModel colum , the problem that it completes from other colum not the one i choose


QCompleter* cc = new QCompleter( this ) ;
cc->setModel( model );
cc->setCompletionColumn(2);
cc->setCompletionMode(QCompleter::PopupCompletion);
cc->setCaseSensitivity( Qt::CaseInsensitive );
lineEdit->setCompleter(cc);


shouldn't it display colum 2 in the model ?
it is displaying the colum 0 , the primary key!

deepinlife
9th April 2008, 15:20
i m wrong about something ,
the completer works fine , but the list which is popued up , works not fine.
it works on the first coulm not on the second one..
so the completer wroks fine but teh QListView which is created when "QCompleter::PopupCompletion" don't work fine?!!!

wysota
9th April 2008, 15:46
Could you provide a minimal compilable example reproducing the problem?

deepinlife
9th April 2008, 16:28
look i have a lineEdit.
i want the line edit complete the words when the user type just the first letter of teh word.So i will use the QCompleter class to make this process.i want the words to be completed from a table in mysql server database.
the words is in the colum number 2 in the table.
i setup the completer class to use the table model , colum 2..
that works fine , now when the user type the first letter of the word the completer class suggest the rest of the word by searching the table in the colum i specified.But the problem arise when the Qcompleter display a list of strings to the user to choose his word from..instead of using the strings found in the colum 2 ,which i told about later , the list display strings from another colum.
i don't know how to explain more than this:o

wysota
9th April 2008, 16:38
i don't know how to explain more than this:o

Provide a compilable piece of code reproducing the problem. Without seeing the actual code which I can run, I have no way of telling what you did wrong (if you did something wrong at all). The snippet you provided is too small to have the full picture.

deepinlife
9th April 2008, 17:24
MainWindow.h


#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QWidget>
#include <QMainWindow>

class QWidget;
class QLineEdit;
class QSqlTableModel;

class MyMainWindow :public QMainWindow
{
public:
MyMainWindow( QWidget* parent = 0);
QLineEdit* le ;
QSqlTableModel* model;
};
#endif



MainWindow.cpp


#include "MainWindow.h"
#include <QLineEdit>
#include <QCompleter>
#include <QSqlTableModel>
#include <QSqlDatabase>

MyMainWindow::MyMainWindow(QWidget* parent )
{
QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
db.setHostName("localhost");
db.setDatabaseName("any");
db.setUserName("any");
db.setPassword("any");

db.open();

le = new QLineEdit( this );
model = new QSqlTableModel();
model->setTable("Items");
model->select();

QCompleter* cc = new QCompleter( this ) ;
cc->setModel( model );
cc->setCompletionColumn(2);
cc->setCompletionMode(QCompleter::PopupCompletion);
// cc->setCompletionMode(QCompleter::InlineCompletion);
cc->setCaseSensitivity( Qt::CaseInsensitive );
le->setCompleter(cc);
}



main.cpp


#include <QApplication>
#include "MainWindow.h"

int main( int argc , char* argv[])
{
QApplication app(argc , argv );

// connectdb();
MyMainWindow* mainWindow= new MyMainWindow;
mainWindow->show();

return app.exec();
}



to run this code u need mysql server , u need to create the database with anyname , and a table called Items..
well hope that helps

wysota
9th April 2008, 18:10
I can reproduce this error on my installation. It seems that setCompletionColumn() doesn't set a proper model column on the popup. A solution is to call:

((QListView*)cc->popup())->setModelColumn(2);
The funny thing is, that it should work without it just fine...

This is the code that gets called:

void QCompleter::setCompletionColumn(int column)
{
Q_D(QCompleter);
if (d->column == column)
return;
#ifndef QT_NO_LISTVIEW
if (QListView *listView = qobject_cast<QListView *>(d->popup))
listView->setModelColumn(column);
#endif
d->column = column;
d->proxy->invalidate();
}
So there are three explanations:
1. popup is not a QListView
2. d->column == column
3. QT_NO_LISTVIEW is defined

First two assumptions are false (popup is a list view and d->column != column) and the third seems very unlikely. So there has to be a fourth explanation - something overrides or doesn't allow to change the model column for the listview.

deepinlife
10th April 2008, 07:46
well thanks alot .
it now works , the colum i want appear , but something starnge is happening..
when the list popup , it don't handle keyboard evens well..
for example when i write the first letter , and the list is poped up , i try to move by the keyboard down , it just moves to one item then refuse to go down anymore..
the same happens when i try to go upwards , the pagedown and pageup don't work at all ?