PDA

View Full Version : displaying second form called by first form with database table of mysql.



rahulvishwakarma
10th November 2017, 10:51
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 :-

12665

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;

QSqlDatabase *db;
QSqlQuery *query;
QSqlQueryModel *model;
QSqlQueryModelForm *ui2;

};

#endif // MYAPPMAINWINDOW_H

file qsqlquerymodelform.cpp :-



#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;
}


third file myappmainwindow.cpp :-



#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();
}


please help me to solve this problem.

high_flyer
12th November 2017, 00:35
please help me to solve this problem.

we would love to help you to solve your problem, for that however, you first need to explain what the problem is!
In your post you only explained what it is you want to do, but not what your problem is.


now i want to call form "qsqlquerymodelform.ui" in signal "click" of login button in form "myappmanwindow.ui".
I guess you mean in the slot 'on_pushButtonLogin_clicked()' and not in the signal 'click'.
Is your problem that you don't understand how signals and slots work?
Or that you don't know what QWidget::show() does?
If you know the answer to both questions, then I really am not sure what your problem might be.

rahulvishwakarma
12th November 2017, 13:39
thanx for reply, actually i want to open form2( QSqlQueryModelForm ) from myappmainwindow, using slot (void MyAppMainWindow :: on_pushButtonLogin_clicked()) in slot of button Login in form1( MyAppMainWindow )

high_flyer
12th November 2017, 21:35
and.... what is the problem you are facing?

rahulvishwakarma
30th November 2017, 12:49
sorry i lost my project when my system (computer) suddenly crashed. but same thing i made up and found this problem:
thsi is my myappmainwindow.cpp :-



#include "myappmainwindow.h"
#include "ui_myappmainwindow.h"

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

db = new QSqlDatabase(QSqlDatabase::addDatabase("QMYSQL"));
db->setHostName("oracle");
db->setDatabaseName("test");
db->setUserName("rahul");
db->setPassword("rahul");
db->setPort(3306);

query = new QSqlQuery(*db);
}

MyappMainWindow::~MyappMainWindow()
{
db->close();
delete ui;
}

void MyappMainWindow::on_pushButtonLogin_clicked()
{
QString su = ui->lineEditUserName->text().trimmed();
QString sp = ui->lineEditPassword->text().trimmed();

dlg = new sqlqueymodelDialog();

if(db->open())
{

QString strquery = "select * form artist";
dlg->model = new QSqlQueryModel();
dlg->model->setQuery(strquery);

dlg->tableiew->setModel(model);//here program crashes
dlg->show();
dlg->exec();
}
}


this is myappmainwindow.h



#ifndef MYAPPMAINWINDOW_H
#define MYAPPMAINWINDOW_H

#include <QMainWindow>
#include "sqlqueymodeldialog.h"
#include <QtSql>
#include <QMessageBox>

namespace Ui {
class MyappMainWindow;
}

class MyappMainWindow : public QMainWindow
{
Q_OBJECT

public:
explicit MyappMainWindow(QWidget *parent = 0);
~MyappMainWindow();

private slots:
void on_pushButtonLogin_clicked();

private:
Ui::MyappMainWindow *ui;
sqlqueymodelDialog * dlg;
QSqlDatabase *db;
QSqlQuery *query;
//QSqlQueryModel *model;

};

#endif // MYAPPMAINWINDOW_H


here is sqlquerymodel.h



#ifndef SQLQUEYMODELDIALOG_H
#define SQLQUEYMODELDIALOG_H

#include <QDialog>
#include <QtSql>
#include <QtWidgets>

namespace Ui {
class sqlqueymodelDialog;
}

class sqlqueymodelDialog : public QDialog
{
Q_OBJECT

public:
explicit sqlqueymodelDialog(QWidget *parent = 0);
~sqlqueymodelDialog();

private:
Ui::sqlqueymodelDialog *ui;

protected:
QSqlQueryModel *model;

public:
// QTableView *tableView;

};

#endif // SQLQUEYMODELDIALOG_H


now how can i access dlg(second dialog) gui component(for example tableviewloginform added using Qt Creator) from myappmainwoindow.cpp.

d_stranz
30th November 2017, 16:53
//here program crashes

The code you have posted won't even compile, much less run. Your SQL query has a typo ("form" instead of "from") so it won't execute anyway.

If you want to give a pointer to the model to your dialog, then simply implement a public method in the dialog class (setModel( QSqlQueryModel * ) or something like that) and call it from your main window class.

This is a basic C++ issue. Surely when you learned C++, you were taught about classes, encapsulation, member variables, and member functions. If you don't remember, then go back and study some more.

rahulvishwakarma
1st December 2017, 08:00
I added public function :-


void setmodel(QSqlQueryModel *model);


in sqlquerymodelDialog.h

and in CPP file
---
i added :-


void sqlqueymodelDialog::setmodel(QSqlQueryModel *model)
{
ui->tableviewforms->setModel(model);
}


in file myappmainwindows.cpp
----------------------------

this shows only sqlqueymodelDialog form but without having mysql database table in it's tableviewLoginForm( QTableview component);
i wanto show sqlsqlquerymodel.ui calling from myappaminwindows.cpp, with dataqbase query fired by myappaminwindows object like this :


void MyappMainWindow::on_pushButtonLogin_clicked()
{
QString su = ui->lineEditUserName->text().trimmed();
QString sp = ui->lineEditPassword->text().trimmed();

if(db->open())
{
dlg = new sqlqueymodelDialog(this);
model = new QSqlQueryModel();// QSqlQueryModel *model as private
query = new QSqlQuery("select * from artist");
model->setQuery(*query);
dlg->setmodel(model);

// dlg->exec();
dlg->show();
dlg->activateWindow();
}
}

high_flyer
1st December 2017, 15:27
Try getting the last error after setQuery(), may be this will give you a hint where the problem is.
http://doc.qt.io/qt-5/qsqlquerymodel.html#setQuery

lastError() can be used to retrieve verbose information if there was an error setting the query.

P.S
Your code leaks memory like a net trying to hold water.
Make sure you know what happens with each object you allocate on the heap and remember that any QObject can be assigned another QObject as parent which will ensure that when a parent is destroyed, all its children are destroyed as well saving you a lot of pointer house keeping.