MainWindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QWidget>
#include <QMainWindow>
{
public:
MyMainWindow
( QWidget* parent
= 0);
};
#endif
#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
To copy to clipboard, switch view to plain text mode
MainWindow.cpp
#include "MainWindow.h"
#include <QLineEdit>
#include <QCompleter>
#include <QSqlTableModel>
#include <QSqlDatabase>
MyMainWindow
::MyMainWindow(QWidget* parent
){
db.setHostName("localhost");
db.setDatabaseName("any");
db.setUserName("any");
db.setPassword("any");
db.open();
model->setTable("Items");
model->select();
cc->setModel( model );
cc->setCompletionColumn(2);
cc
->setCompletionMode
(QCompleter::PopupCompletion);
// cc->setCompletionMode(QCompleter::InlineCompletion);
cc->setCaseSensitivity( Qt::CaseInsensitive );
le->setCompleter(cc);
}
#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);
}
To copy to clipboard, switch view to plain text mode
main.cpp
#include <QApplication>
#include "MainWindow.h"
int main( int argc , char* argv[])
{
// connectdb();
MyMainWindow* mainWindow= new MyMainWindow;
mainWindow->show();
return app.exec();
}
#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 copy to clipboard, switch view to plain text mode
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
Bookmarks