Results 1 to 2 of 2

Thread: How would I open a new window from a button in the dialog using Qt Creator?

  1. #1
    Join Date
    Jan 2015
    Posts
    1
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default How would I open a new window from a button in the dialog using Qt Creator?

    I'm new to qt programming.so please go easy on me. I have one dialog designed in Qt5.4, A modal main Window or a dialog is to be opened when a button is pressed.but i can not find the setModal object to set the Qwindow and when i press the button the dialog dose not hid and neither the Qwindow or dialog will open. here are my codes

    Login.h

    Qt Code:
    1. #ifndef LOGIN_H
    2. #define LOGIN_H
    3.  
    4. #include <QDialog>
    5. #include <QCoreApplication>
    6. #include <QApplication>
    7. #include <QtSql/QSql>
    8. #include <QtSql/QSqlDatabase>
    9. #include <QtSql/QSqlDriver>
    10. #include <QtSql/QSqlQuery>
    11. #include <QDebug>
    12. #include "next_page.h"
    13. #include "main_page.h"
    14.  
    15. namespace Ui {
    16. class Login;
    17. }
    18.  
    19. class Login : public QDialog
    20. {
    21. Q_OBJECT
    22.  
    23. public:
    24. // function for database connetion
    25. QSqlDatabase MyDB; //object for connection
    26. /*
    27.   void CloseConncetion()
    28.   {
    29.   MyDB.close();
    30.   MyDB.removeDatabase(QSqlDatabase::defaultConnection);
    31.   }
    32. */
    33. void CloseConncetion()
    34. {
    35. QString connection;
    36. connection = MyDB.connectionName();
    37. MyDB=QSqlDatabase();
    38. MyDB.removeDatabase(connection);
    39. }
    40.  
    41.  
    42. bool OpenConncetion(){
    43. QSqlDatabase MyDB = QSqlDatabase::addDatabase("QMYSQL");
    44. MyDB .setHostName("localhost");
    45. MyDB.setDatabaseName("project");
    46. MyDB.setUserName("lord-ivan");
    47. MyDB.setPassword("Ir0ngo@t");
    48.  
    49. if (!MyDB.open())
    50. {
    51. qDebug()<<("Datebase not Connected");
    52.  
    53. return false;
    54.  
    55. }
    56. else
    57. {
    58. qDebug()<<("Datebase Connected");
    59.  
    60. return true;
    61. }
    62.  
    63. }
    64. public:
    65. explicit Login(QWidget *parent = 0);
    66. ~Login();
    67.  
    68. private slots:
    69. void on_pushButton_Login_clicked();
    70.  
    71. void on_pushButton_clicked();
    72.  
    73. private:
    74. Ui::Login *ui;
    75. };
    76.  
    77. #endif // LOGIN_H
    To copy to clipboard, switch view to plain text mode 

    Login.cpp

    Qt Code:
    1. #include "login.h"
    2. #include "ui_login.h"
    3.  
    4. Login::Login(QWidget *parent) :
    5. QDialog(parent),
    6. ui(new Ui::Login)
    7. {
    8. ui->setupUi(this);
    9.  
    10.  
    11. if (!OpenConncetion())
    12. ui->label_Status->setText("Datebase not Connected");
    13. else
    14. ui->label_Status->setText("Datebase Connected");
    15.  
    16. }
    17.  
    18. Login::~Login()
    19. {
    20. delete ui;
    21. }
    22.  
    23. void Login::on_pushButton_Login_clicked()
    24. {
    25.  
    26.  
    27. QString username,password;
    28.  
    29. username=ui->lineEdit_UserName->text();
    30. password=ui->lineEdit_PassWord->text();
    31.  
    32. if(!OpenConncetion())
    33. {
    34. qDebug()<<"Datebase not Connected";
    35. return;
    36. }
    37.  
    38. QSqlQuery query;
    39.  
    40. if(query.exec("SELECT * FROM Users where UserName ='"+username+"' and Password ='"+password+"'")) // query for input
    41. {
    42. int count=0;
    43. while(query.next())
    44. {
    45.  
    46. count++;
    47. }
    48. if (count==1)
    49. {
    50. ui->label_Status->setText("Username and password is correct");
    51.  
    52. CloseConncetion(); // it is important to call this funcion to open the connection.
    53.  
    54. this->hide();
    55. Next_Page page;
    56. page.setModal(true);
    57. page.exec();
    58.  
    59. }
    60. if (count>1)
    61. ui->label_Status->setText("Duplicate Username and password is correct");
    62. if (count<1)
    63. ui->label_Status->setText("Username and password is not correct");
    64.  
    65.  
    66. }
    67.  
    68.  
    69.  
    70. }
    71.  
    72. void Login::on_pushButton_clicked()
    73. {
    74.  
    75.  
    76.  
    77. QString username,password;
    78.  
    79. username=ui->lineEdit_UserName->text();
    80. password=ui->lineEdit_PassWord->text();
    81.  
    82. if(!OpenConncetion())
    83. {
    84. qDebug()<<"Datebase not Connected";
    85. return;
    86. }
    87.  
    88. QSqlQuery query;
    89.  
    90. if(query.exec("SELECT * FROM Users where UserName ='"+username+"' and Password ='"+password+"'")) // query for input
    91. {
    92. int count=0;
    93. while(query.next())
    94. {
    95.  
    96. count++;
    97. }
    98. if (count==1)
    99. {
    100. ui->label_Status->setText("Username and password is correct");
    101.  
    102. CloseConncetion(); // it is important to call this funcion to open the connection.
    103.  
    104. this->hide();
    105. Main_Page Mpage;
    106. Mpage.window();
    107.  
    108. Mpage.show();
    109.  
    110. }
    111. if (count>1)
    112. ui->label_Status->setText("Duplicate Username and password is correct");
    113. if (count<1)
    114. ui->label_Status->setText("Username and password is not correct");
    115.  
    116.  
    117. }
    118. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How would I open a new window from a button in the dialog using Qt Creator?

    You usually don't need to set the modality of a dialog.
    show() brings it up as a non-modal dialog and exec() brings it up as modal.

    From a quick look your Next_Page window should open, but of course Main_Page will not (Mpage doesn't live long enough).

    Cheers,
    _

Similar Threads

  1. Replies: 3
    Last Post: 15th March 2012, 15:30
  2. Replies: 2
    Last Post: 26th April 2011, 11:44
  3. Replies: 3
    Last Post: 23rd December 2010, 06:55
  4. Replies: 0
    Last Post: 10th September 2010, 13:23
  5. Replies: 0
    Last Post: 9th May 2010, 20:49

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.