Results 1 to 8 of 8

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

Threaded View

Previous Post Previous Post   Next Post Next Post
  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 11:02.

Similar Threads

  1. "Acess style" form to manage a database table
    By graciano in forum Qt Programming
    Replies: 1
    Last Post: 25th February 2014, 15:07
  2. Replies: 3
    Last Post: 18th July 2013, 04:12
  3. Replies: 7
    Last Post: 23rd May 2012, 12: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, 14: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, 09: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.