hi to all.
I've qt 5.3.1 & mysql client in centos 6.6 (x86) in one VM and second VM contains server Mysql.
in qt i want to call second form ( "qsqlquerymodelform.ui" ) from first form ("myappmanwindow.ui")
"qsqlquerymodelform.ui" file contains one tableviewLoginForm ( tableview object from components of Qt creator design page)
the first form is this :-
now i want to call form "qsqlquerymodelform.ui" in signal "click" of login button in form "myappmanwindow.ui".
And also i want to show "login table" from mysql server.
my first form is image :-
myppmainwindowform.jpg
myappmainwindow.h:-
source code :-
#ifndef MYAPPMAINWINDOW_H
#define MYAPPMAINWINDOW_H
#include <QMainWindow>
#include <QSqlDatabase>
#include <QSqlDriver>
#include <QMessageBox>
#include <QSqlQuery>
#include <QSqlRecord>
#include <QSqlQueryModel>
#include <QString>
#include "qsqlquerymodelform.h"
namespace Ui {
class MyAppMainWindow;
//class QSqlQueryModelForm;
}
//class QSqlQueryModelForm;
class MyAppMainWindow
: public QMainWindow//, public Ui:: QSqlQueryModelForm{
Q_OBJECT
public:
explicit MyAppMainWindow
(QWidget *parent
= 0);
~MyAppMainWindow();
private slots:
void on_pushButtonLogin_clicked();
private:
Ui::MyAppMainWindow *ui;
QSqlQueryModelForm *ui2;
};
#endif // MYAPPMAINWINDOW_H
#ifndef MYAPPMAINWINDOW_H
#define MYAPPMAINWINDOW_H
#include <QMainWindow>
#include <QSqlDatabase>
#include <QSqlDriver>
#include <QMessageBox>
#include <QSqlQuery>
#include <QSqlRecord>
#include <QSqlQueryModel>
#include <QString>
#include "qsqlquerymodelform.h"
namespace Ui {
class MyAppMainWindow;
//class QSqlQueryModelForm;
}
//class QSqlQueryModelForm;
class MyAppMainWindow : public QMainWindow//, public Ui:: QSqlQueryModelForm
{
Q_OBJECT
public:
explicit MyAppMainWindow(QWidget *parent = 0);
~MyAppMainWindow();
private slots:
void on_pushButtonLogin_clicked();
private:
Ui::MyAppMainWindow *ui;
QSqlDatabase *db;
QSqlQuery *query;
QSqlQueryModel *model;
QSqlQueryModelForm *ui2;
};
#endif // MYAPPMAINWINDOW_H
To copy to clipboard, switch view to plain text mode
file qsqlquerymodelform.cpp :-
#include "qsqlquerymodelform.h"
#include "ui_qsqlquerymodelform.h"
QSqlQueryModelForm
::QSqlQueryModelForm(QWidget *parent
) : ui(new Ui::QSqlQueryModelForm)
{
ui->setupUi(this);
//ui->tableViewLoginForm->setModel();
}
QSqlQueryModelForm::~QSqlQueryModelForm()
{
delete ui;
}
#include "qsqlquerymodelform.h"
#include "ui_qsqlquerymodelform.h"
QSqlQueryModelForm::QSqlQueryModelForm(QWidget *parent) :
QWidget(parent),
ui(new Ui::QSqlQueryModelForm)
{
ui->setupUi(this);
this->model = new QSqlQueryModel();
//ui->tableViewLoginForm->setModel();
}
QSqlQueryModelForm::~QSqlQueryModelForm()
{
delete ui;
}
To copy to clipboard, switch view to plain text mode
third file myappmainwindow.cpp :-
#include "myappmainwindow.h"
#include "ui_myappmainwindow.h"
#include <QtDebug>
#include <QSqlError>
#include "qsqlquerymodelform.h"
MyAppMainWindow
::MyAppMainWindow(QWidget *parent
) : ui(new Ui::MyAppMainWindow)//,ui2(new Ui::QSqlQueryModelForm)
{
ui->setupUi(this);
db->setDatabaseName("test");
db->setHostName("oracle");
db->setPort(3306);
db->setUserName("rahul");
db->setPassword("rahul");
ui2 = new QSqlQueryModelForm();
}
MyAppMainWindow::~MyAppMainWindow()
{
db->close();
delete ui;
}
void MyAppMainWindow::on_pushButtonLogin_clicked()
{
QString susername
= ui
->lineEditUserName
->text
();
QString spassword
= ui
->lineEditPassword
->text
();
if( db->open())
{
QString strquery
= "select * from tablelogin where user = \"" + susername.
trimmed() +"\" " +" and password = \""+ spassword.trimmed()+ "\";";
if(query->exec(strquery))
{
model->setQuery(strquery);
ui->tableViewLoginForm->setModel(model);
//ui2->ta
}
else
{
QMessageBox::critical(this,
"SQL QUERY", db
->lastError
().
text());
}
}
db->close();
}
#include "myappmainwindow.h"
#include "ui_myappmainwindow.h"
#include <QtDebug>
#include <QSqlError>
#include "qsqlquerymodelform.h"
MyAppMainWindow::MyAppMainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MyAppMainWindow)//,ui2(new Ui::QSqlQueryModelForm)
{
ui->setupUi(this);
db = new QSqlDatabase(QSqlDatabase::addDatabase("QMYSQL"));
db->setDatabaseName("test");
db->setHostName("oracle");
db->setPort(3306);
db->setUserName("rahul");
db->setPassword("rahul");
ui2 = new QSqlQueryModelForm();
query = new QSqlQuery(*db);
}
MyAppMainWindow::~MyAppMainWindow()
{
db->close();
delete ui;
}
void MyAppMainWindow::on_pushButtonLogin_clicked()
{
QString susername = ui->lineEditUserName->text();
QString spassword = ui->lineEditPassword->text();
if( db->open())
{
QString strquery = "select * from tablelogin where user = \"" + susername.trimmed() +"\" " +" and password = \""+
spassword.trimmed()+ "\";";
if(query->exec(strquery))
{
this->model = new QSqlQueryModel;
model->setQuery(strquery);
ui->tableViewLoginForm->setModel(model);
//ui2->ta
}
else
{
QMessageBox::critical(this, "SQL QUERY", db->lastError().text());
}
}
db->close();
}
To copy to clipboard, switch view to plain text mode
please help me to solve this problem.
Bookmarks