PDA

View Full Version : Using QCompleter with QTableWidget



SIFE
7th December 2011, 13:22
I have this class:

#include <QTableWidgetItem>
#include <QSignalMapper>
#include <QCompleter>
#include <QSqlError>
#include <QSqlTableModel>

#include "bill.h"

#include <QMessageBox>

PrintBill::PrintBill(QWidget *parent) : QDialog(parent),
ui(new Ui::PrintBill)
{
ui->setupUi(this);

ui->billTableWidget->setSelectionBehavior (QAbstractItemView::SelectItems );
ui->billTableWidget->setSelectionMode( QAbstractItemView::ExtendedSelection );
ui->billTableWidget->setRowCount(1);
ui->billTableWidget->setColumnCount (5);
ui->billTableWidget->setHorizontalHeaderLabels (labels);
ui->billTableWidget->setColumnWidth (0, 63);
ui->billTableWidget->setColumnWidth (1, 400);
ui->billTableWidget->setColumnWidth(2, 100);
ui->billTableWidget->setColumnWidth(3, 63);
ui->billTableWidget->setColumnWidth(4, 85);

QLineEdit *product = new QLineEdit;
ui->billTableWidget->setCellWidget(0, 1, product);
sigMapper = new QSignalMapper(this);
connect(product, SIGNAL(textEdited(QString )), sigMapper, SLOT(map()));
sigMapper->setMapping(product, txt);
connect(sigMapper, SIGNAL(mapped(QString )), this, SLOT(suggestItem()));

}

void PrintBill::printBill()
{
}

void PrintBill::suggestItem()
{
QLineEdit *product = static_cast<QLineEdit *>(ui->billTableWidget->cellWidget(0, 1));

/* sqlQuery = "SELECT Name FROM Items WHERE Name LIKE '%" + product->text() + "%'";
query.exec(sqlQuery);
rec = query.record();

QMessageBox msg;
msg.setText(product->text() + QString::number(query.numRowsAffected()));
//msg.exec();
while(query.next())
{
items << query.value(rec.indexOf("Name")).toString();
msg.setText( query.value(rec.indexOf("Name")).toString());
msg.exec();
}
*/
QSqlTableModel *model = new QSqlTableModel();
QCompleter suggestions(items);
suggestions.setCaseSensitivity(Qt::CaseInsensitive );
suggestions.setModel(model);
suggestions.setCompletionColumn(1);
suggestions.setCompletionMode(QCompleter::PopupCom pletion);
model->setTable("Items");
model->select();
model->setFilter("Name LIKE '%" + product->text() + "%'");

product->setCompleter(&suggestions);
}

PrintBill::~PrintBill()
{
delete ui;
}
Now, if I type any thing, no data appear in the QLineEdit, the connection is steal open. I tried also query.numRowsAffected() but always return -1.