1 Attachment(s)
displaying second form called by first form with database table of mysql.
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 :-
Attachment 12665
myappmainwindow.h:-
source code :-
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
file qsqlquerymodelform.cpp :-
Code:
#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;
}
third file myappmainwindow.cpp :-
Code:
#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();
}
please help me to solve this problem.
Re: displaying second form called by first form with database table of mysql.
Quote:
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.
Quote:
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.
Re: displaying second form called by first form with database table of mysql.
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 )
Re: displaying second form called by first form with database table of mysql.
and.... what is the problem you are facing?
Re: displaying second form called by first form with database table of mysql.
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 :-
Code:
#include "myappmainwindow.h"
#include "ui_myappmainwindow.h"
MyappMainWindow
::MyappMainWindow(QWidget *parent
) : ui(new Ui::MyappMainWindow)
{
ui->setupUi(this);
db->setHostName("oracle");
db->setDatabaseName("test");
db->setUserName("rahul");
db->setPassword("rahul");
db->setPort(3306);
}
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->setQuery(strquery);
dlg->tableiew->setModel(model);//here program crashes
dlg->show();
dlg->exec();
}
}
this is myappmainwindow.h
Code:
#ifndef MYAPPMAINWINDOW_H
#define MYAPPMAINWINDOW_H
#include <QMainWindow>
#include "sqlqueymodeldialog.h"
#include <QtSql>
#include <QMessageBox>
namespace Ui {
class MyappMainWindow;
}
{
Q_OBJECT
public:
explicit MyappMainWindow
(QWidget *parent
= 0);
~MyappMainWindow();
private slots:
void on_pushButtonLogin_clicked();
private:
Ui::MyappMainWindow *ui;
sqlqueymodelDialog * dlg;
//QSqlQueryModel *model;
};
#endif // MYAPPMAINWINDOW_H
here is sqlquerymodel.h
Code:
#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:
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.
Re: displaying second form called by first form with database table of mysql.
Quote:
//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.
Re: displaying second form called by first form with database table of mysql.
I added public function :-
in sqlquerymodelDialog.h
and in CPP file
---
i added :-
Code:
{
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 :
Code:
void MyappMainWindow::on_pushButtonLogin_clicked()
{
QString su
= ui
->lineEditUserName
->text
().
trimmed();
QString sp
= ui
->lineEditPassword
->text
().
trimmed();
if(db->open())
{
dlg = new sqlqueymodelDialog(this);
query
= new QSqlQuery("select * from artist");
model->setQuery(*query);
dlg->setmodel(model);
// dlg->exec();
dlg->show();
dlg->activateWindow();
}
}
Re: displaying second form called by first form with database table of mysql.
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
Quote:
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.