Results 1 to 8 of 8

Thread: displaying second form called by first form with database table of mysql.

  1. #1
    Join Date
    Jun 2014
    Posts
    47
    Thanks
    6
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default 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 :-

    myppmainwindowform.jpg

    myappmainwindow.h:-

    source code :-
    Qt Code:
    1. #ifndef MYAPPMAINWINDOW_H
    2. #define MYAPPMAINWINDOW_H
    3.  
    4. #include <QMainWindow>
    5. #include <QSqlDatabase>
    6. #include <QSqlDriver>
    7. #include <QMessageBox>
    8. #include <QSqlQuery>
    9. #include <QSqlRecord>
    10. #include <QSqlQueryModel>
    11. #include <QString>
    12.  
    13. #include "qsqlquerymodelform.h"
    14.  
    15. namespace Ui {
    16. class MyAppMainWindow;
    17. //class QSqlQueryModelForm;
    18. }
    19.  
    20. //class QSqlQueryModelForm;
    21.  
    22. class MyAppMainWindow : public QMainWindow//, public Ui:: QSqlQueryModelForm
    23. {
    24. Q_OBJECT
    25.  
    26. public:
    27. explicit MyAppMainWindow(QWidget *parent = 0);
    28. ~MyAppMainWindow();
    29.  
    30. private slots:
    31. void on_pushButtonLogin_clicked();
    32.  
    33. private:
    34. Ui::MyAppMainWindow *ui;
    35.  
    36. QSqlQuery *query;
    37. QSqlQueryModelForm *ui2;
    38.  
    39. };
    40.  
    41. #endif // MYAPPMAINWINDOW_H
    To copy to clipboard, switch view to plain text mode 
    file qsqlquerymodelform.cpp :-

    Qt Code:
    1. #include "qsqlquerymodelform.h"
    2. #include "ui_qsqlquerymodelform.h"
    3.  
    4. QSqlQueryModelForm::QSqlQueryModelForm(QWidget *parent) :
    5. QWidget(parent),
    6. ui(new Ui::QSqlQueryModelForm)
    7. {
    8. ui->setupUi(this);
    9.  
    10. this->model = new QSqlQueryModel();
    11.  
    12. //ui->tableViewLoginForm->setModel();
    13.  
    14. }
    15.  
    16. QSqlQueryModelForm::~QSqlQueryModelForm()
    17. {
    18. delete ui;
    19. }
    To copy to clipboard, switch view to plain text mode 

    third file myappmainwindow.cpp :-

    Qt Code:
    1. #include "myappmainwindow.h"
    2. #include "ui_myappmainwindow.h"
    3. #include <QtDebug>
    4. #include <QSqlError>
    5. #include "qsqlquerymodelform.h"
    6.  
    7. MyAppMainWindow::MyAppMainWindow(QWidget *parent) :
    8. QMainWindow(parent),
    9. ui(new Ui::MyAppMainWindow)//,ui2(new Ui::QSqlQueryModelForm)
    10. {
    11. ui->setupUi(this);
    12.  
    13. db = new QSqlDatabase(QSqlDatabase::addDatabase("QMYSQL"));
    14. db->setDatabaseName("test");
    15. db->setHostName("oracle");
    16. db->setPort(3306);
    17. db->setUserName("rahul");
    18. db->setPassword("rahul");
    19.  
    20. ui2 = new QSqlQueryModelForm();
    21.  
    22. query = new QSqlQuery(*db);
    23. }
    24.  
    25. MyAppMainWindow::~MyAppMainWindow()
    26. {
    27. db->close();
    28. delete ui;
    29. }
    30.  
    31. void MyAppMainWindow::on_pushButtonLogin_clicked()
    32. {
    33. QString susername = ui->lineEditUserName->text();
    34. QString spassword = ui->lineEditPassword->text();
    35.  
    36. if( db->open())
    37. {
    38. QString strquery = "select * from tablelogin where user = \"" + susername.trimmed() +"\" " +" and password = \""+
    39. spassword.trimmed()+ "\";";
    40.  
    41. if(query->exec(strquery))
    42. {
    43. this->model = new QSqlQueryModel;
    44. model->setQuery(strquery);
    45. ui->tableViewLoginForm->setModel(model);
    46. //ui2->ta
    47. }
    48. else
    49. {
    50. QMessageBox::critical(this, "SQL QUERY", db->lastError().text());
    51. }
    52.  
    53. }
    54.  
    55. db->close();
    56. }
    To copy to clipboard, switch view to plain text mode 

    please help me to solve this problem.
    Last edited by rahulvishwakarma; 10th November 2017 at 12:02.

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: displaying second form called by first form with database table of mysql.

    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.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  3. #3
    Join Date
    Jun 2014
    Posts
    47
    Thanks
    6
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default 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 )

  4. #4
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: displaying second form called by first form with database table of mysql.

    and.... what is the problem you are facing?
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  5. #5
    Join Date
    Jun 2014
    Posts
    47
    Thanks
    6
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default 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 :-

    Qt Code:
    1. #include "myappmainwindow.h"
    2. #include "ui_myappmainwindow.h"
    3.  
    4. MyappMainWindow::MyappMainWindow(QWidget *parent) :
    5. QMainWindow(parent),
    6. ui(new Ui::MyappMainWindow)
    7. {
    8. ui->setupUi(this);
    9.  
    10. db = new QSqlDatabase(QSqlDatabase::addDatabase("QMYSQL"));
    11. db->setHostName("oracle");
    12. db->setDatabaseName("test");
    13. db->setUserName("rahul");
    14. db->setPassword("rahul");
    15. db->setPort(3306);
    16.  
    17. query = new QSqlQuery(*db);
    18. }
    19.  
    20. MyappMainWindow::~MyappMainWindow()
    21. {
    22. db->close();
    23. delete ui;
    24. }
    25.  
    26. void MyappMainWindow::on_pushButtonLogin_clicked()
    27. {
    28. QString su = ui->lineEditUserName->text().trimmed();
    29. QString sp = ui->lineEditPassword->text().trimmed();
    30.  
    31. dlg = new sqlqueymodelDialog();
    32.  
    33. if(db->open())
    34. {
    35.  
    36. QString strquery = "select * form artist";
    37. dlg->model = new QSqlQueryModel();
    38. dlg->model->setQuery(strquery);
    39.  
    40. dlg->tableiew->setModel(model);//here program crashes
    41. dlg->show();
    42. dlg->exec();
    43. }
    44. }
    To copy to clipboard, switch view to plain text mode 

    this is myappmainwindow.h

    Qt Code:
    1. #ifndef MYAPPMAINWINDOW_H
    2. #define MYAPPMAINWINDOW_H
    3.  
    4. #include <QMainWindow>
    5. #include "sqlqueymodeldialog.h"
    6. #include <QtSql>
    7. #include <QMessageBox>
    8.  
    9. namespace Ui {
    10. class MyappMainWindow;
    11. }
    12.  
    13. class MyappMainWindow : public QMainWindow
    14. {
    15. Q_OBJECT
    16.  
    17. public:
    18. explicit MyappMainWindow(QWidget *parent = 0);
    19. ~MyappMainWindow();
    20.  
    21. private slots:
    22. void on_pushButtonLogin_clicked();
    23.  
    24. private:
    25. Ui::MyappMainWindow *ui;
    26. sqlqueymodelDialog * dlg;
    27. QSqlQuery *query;
    28. //QSqlQueryModel *model;
    29.  
    30. };
    31.  
    32. #endif // MYAPPMAINWINDOW_H
    To copy to clipboard, switch view to plain text mode 

    here is sqlquerymodel.h

    Qt Code:
    1. #ifndef SQLQUEYMODELDIALOG_H
    2. #define SQLQUEYMODELDIALOG_H
    3.  
    4. #include <QDialog>
    5. #include <QtSql>
    6. #include <QtWidgets>
    7.  
    8. namespace Ui {
    9. class sqlqueymodelDialog;
    10. }
    11.  
    12. class sqlqueymodelDialog : public QDialog
    13. {
    14. Q_OBJECT
    15.  
    16. public:
    17. explicit sqlqueymodelDialog(QWidget *parent = 0);
    18. ~sqlqueymodelDialog();
    19.  
    20. private:
    21. Ui::sqlqueymodelDialog *ui;
    22.  
    23. protected:
    24.  
    25. public:
    26. // QTableView *tableView;
    27.  
    28. };
    29.  
    30. #endif // SQLQUEYMODELDIALOG_H
    To copy to clipboard, switch view to plain text mode 

    now how can i access dlg(second dialog) gui component(for example tableviewloginform added using Qt Creator) from myappmainwoindow.cpp.
    Last edited by rahulvishwakarma; 30th November 2017 at 13:54.

  6. #6
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,229
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: displaying second form called by first form with database table of mysql.

    //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.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  7. #7
    Join Date
    Jun 2014
    Posts
    47
    Thanks
    6
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: displaying second form called by first form with database table of mysql.

    I added public function :-
    Qt Code:
    1. void setmodel(QSqlQueryModel *model);
    To copy to clipboard, switch view to plain text mode 

    in sqlquerymodelDialog.h

    and in CPP file
    ---
    i added :-
    Qt Code:
    1. void sqlqueymodelDialog::setmodel(QSqlQueryModel *model)
    2. {
    3. ui->tableviewforms->setModel(model);
    4. }
    To copy to clipboard, switch view to plain text mode 

    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 :
    Qt Code:
    1. void MyappMainWindow::on_pushButtonLogin_clicked()
    2. {
    3. QString su = ui->lineEditUserName->text().trimmed();
    4. QString sp = ui->lineEditPassword->text().trimmed();
    5.  
    6. if(db->open())
    7. {
    8. dlg = new sqlqueymodelDialog(this);
    9. model = new QSqlQueryModel();// QSqlQueryModel *model as private
    10. query = new QSqlQuery("select * from artist");
    11. model->setQuery(*query);
    12. dlg->setmodel(model);
    13.  
    14. // dlg->exec();
    15. dlg->show();
    16. dlg->activateWindow();
    17. }
    18. }
    To copy to clipboard, switch view to plain text mode 

  8. #8
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default 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
    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.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

Similar Threads

  1. "Acess style" form to manage a database table
    By graciano in forum Qt Programming
    Replies: 1
    Last Post: 25th February 2014, 16:07
  2. Replies: 3
    Last Post: 18th July 2013, 05:12
  3. Replies: 7
    Last Post: 23rd May 2012, 13:00
  4. How to hide from taskbar a form showed form mainwindow
    By tonnot in forum Qt Programming
    Replies: 1
    Last Post: 10th February 2011, 15:36
  5. View/edit database in a table and in a form
    By igorko in forum Qt Programming
    Replies: 1
    Last Post: 16th June 2006, 10:09

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.