Results 1 to 12 of 12

Thread: use button from another Window

  1. #1
    Join Date
    Jan 2006
    Posts
    273
    Thanks
    42
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default use button from another Window

    Hello everybody!

    QT: 4.1.1

    I have 2 Windows: Mainwindow and Login.
    I would like to enable a button in the Mainwindow if the login was ok.
    My sitiation:
    1. start program (MainWindow)
    2. open Login (Modal)
    3. click on OK, if connection was ok, i need to enable a button in the MainWindow..

    I tryied this:

    login.cpp
    Qt Code:
    1. MainWindow* m;
    2. m->ui.speichern_btn->setEnabled(TRUE);
    To copy to clipboard, switch view to plain text mode 

    login.h
    Qt Code:
    1. public:
    2. LoginDialog(QWidget *parent = 0);
    3. MainWindow* m;
    To copy to clipboard, switch view to plain text mode 

    Have somebody a idea
    Think DigitalGasoline

  2. #2
    Join Date
    Jan 2006
    Location
    Alingsås, Sweden
    Posts
    437
    Thanks
    3
    Thanked 39 Times in 39 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: use button from another Window

    It seems that you declare a local main window in login.cpp, and this overrides the class' public member with the same name. Declare it once and it will probably work.

  3. #3
    Join Date
    Jan 2006
    Posts
    273
    Thanks
    42
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default Re: use button from another Window

    Hi, i get no errors from compiler, but if i call my function the program crash..

    login.cpp
    Qt Code:
    1. MainWindow* m;
    2. m->ui.speichern_btn->setEnabled(TRUE);
    To copy to clipboard, switch view to plain text mode 

    What could i try?
    Think DigitalGasoline

  4. #4
    Join Date
    Feb 2006
    Location
    Berlin
    Posts
    4
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: use button from another Window

    Hi,

    if I understand you rigth, you create the instance of LoginDialog in MainWindow.

    Then you can add a SIGNAL in login.h, something like this one:
    Qt Code:
    1. signals:
    2. void LoginOk();
    To copy to clipboard, switch view to plain text mode 

    In the login.cpp you must write when the SIGNAL are emitted, maybe like this:
    Qt Code:
    1. connect(loginButton, SIGNAL(clicked()), this, SIGNAL(LoginOk()));
    To copy to clipboard, switch view to plain text mode 

    Now, MainWindow can recieve this SIGNAL and you can call a SLOT from MainWindow:

    Qt Code:
    1. //creating an instance of LoginDialog, maybe in the ctr of MainWindow
    2. LoginDialog *loginDialog = new LoginDialog();
    3. loginDialog->show();
    4. connect(loginDialog, SIGNAL(LoginOk()), this, SLOT(anySlotYouveCreated()));
    To copy to clipboard, switch view to plain text mode 

    In the SLOT you can add the code to enabe the button.

    Hopefully I understand you right, first time I can help/answer.
    Last edited by meissner; 1st March 2006 at 03:32.

  5. #5
    Join Date
    Jan 2006
    Posts
    273
    Thanks
    42
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default Re: use button from another Window

    Hi, interesting way..but it happens nothing if i click my ok_btn

    And i want to show my Login not by starting my app. i want over my menu, see code:
    Have you a idea why not works?

    login.h:
    Qt Code:
    1. #include "ui_login.h"
    2.  
    3.  
    4.  
    5.  
    6. class LoginDialog : public QDialog
    7. {
    8. Q_OBJECT
    9.  
    10. public:
    11. LoginDialog(QWidget *parent = 0);
    12.  
    13.  
    14. public slots:
    15. void myfirstfunction();
    16. bool verbinden();
    17.  
    18.  
    19. private:
    20. Ui::LoginDialog ui;
    21. void init();
    22.  
    23. signals:
    24. void LoginOk();
    25.  
    26.  
    27.  
    28.  
    29.  
    30.  
    31. };
    To copy to clipboard, switch view to plain text mode 

    login.cpp:
    Qt Code:
    1. #include "login.h"
    2. #include "test.h"
    3.  
    4.  
    5.  
    6. #include <QMessageBox>
    7. #include <QSqlDatabase>
    8. #include <QSqlQuery>
    9. #include <QSqlError>
    10. #include <QTextEdit>
    11. #include <QStatusBar>
    12. #include <QAbstractItemView>
    13. #include <QEvent>
    14. #include <QTreeWidgetItem>
    15. #include <QTreeWidget>
    16. #include <QtGui/QTreeWidget>
    17. #include <QSqlQueryModel>
    18. #include <QSqlTableModel>
    19.  
    20. LoginDialog::LoginDialog(QWidget *parent)
    21. : QDialog(parent)
    22. {
    23. ui.setupUi(this);
    24.  
    25.  
    26. connect(ui.ok_btn, SIGNAL(clicked()), this, SLOT(LoginOk()));
    27. connect(ui.ok_btn, SIGNAL(clicked()), this, SLOT(verbinden()));
    28. connect(ui.cancel_btn, SIGNAL(clicked()), this, SLOT(close()));
    29. }
    30.  
    31. void LoginDialog::myfirstfunction()
    32. {
    33. QMessageBox::information(this, "Application name",
    34. "Unable to find the user preferences file.\n"
    35. "The factory default will be used instead.");
    36.  
    37. }
    38. bool LoginDialog::verbinden()
    39. {
    40.  
    41. QString user = ui.user_le->text();
    42. QString pass = ui.pass_le->text();
    43. QString host = ui.host_cb->currentText();
    44. QMessageBox::information(this,"Login", user);
    45. QMessageBox::information(this,"Login", pass);
    46. QMessageBox::information(this,"Login", host);
    47. if((user == ""))
    48. {
    49. QMessageBox::information(this,"Login", "Bitte Benutzername angeben");
    50. return 0;
    51. }
    52.  
    53. QSqlDatabase db = QSqlDatabase::addDatabase("QODBC");
    54. db.setHostName(host);
    55. db.setDatabaseName("DRIVER={SQL Server};SERVER="+host+";DATABASE=inventar;UID="+user+";PWD="+pass+"");
    56. db.setUserName(user);
    57. db.setPassword(pass);
    58. if(!db.open())
    59. {
    60. QMessageBox::information(this,"",db.lastError().text());
    61. return false;
    62. }
    63. else
    64. {
    65. //MainWindow* m;
    66. //m->ui.speichern_btn->setEnabled(TRUE);
    67.  
    68.  
    69. this->close();
    70. return true;
    71. }
    72.  
    73. }
    To copy to clipboard, switch view to plain text mode 

    test.h:
    Qt Code:
    1. #include "ui_mainwindow.h"
    2.  
    3.  
    4.  
    5. class MainWindow : public QMainWindow
    6. {
    7. Q_OBJECT
    8.  
    9. public:
    10. MainWindow();
    11. Ui::MainWindow ui;
    12.  
    13.  
    14.  
    15. public slots:
    16. void selectSprache();
    17. void openLoginDialog();
    18. void updateTable();
    19. void enableButton();
    20.  
    21.  
    22.  
    23. private:
    24.  
    25. void init();
    26.  
    27. protected:
    28.  
    29.  
    30.  
    31.  
    32. };
    To copy to clipboard, switch view to plain text mode 

    test.cpp:
    Qt Code:
    1. #include "test.h"
    2. #include "login.h"
    3.  
    4. #include <QMessageBox>
    5. #include <QSqlDatabase>
    6. #include <QSqlQuery>
    7. #include <QSqlError>
    8. #include <QTextEdit>
    9. #include <QStatusBar>
    10. #include <QAbstractItemView>
    11. #include <QEvent>
    12. #include <QTreeWidgetItem>
    13. #include <QTreeWidget>
    14. #include <QtGui/QTreeWidget>
    15. #include <QSqlQueryModel>
    16. #include <QSqlTableModel>
    17. #include <QSqlRecord>
    18.  
    19.  
    20.  
    21.  
    22. MainWindow::MainWindow()
    23.  
    24. {
    25. ui.setupUi(this);
    26.  
    27. LoginDialog* logindialog = new LoginDialog();
    28. logindialog->show();
    29. connect(logindialog, SIGNAL(LoginOk()), this, SLOT(enableButton()));
    30. connect(ui.actionverbinden, SIGNAL(triggered()), this, SLOT(openLoginDialog()));
    31. connect(ui.speichern_btn, SIGNAL(clicked()), this, SLOT(selectSprache()));
    32. connect(ui.update_btn, SIGNAL(clicked()), this, SLOT(updateTable()));
    33. init();
    34.  
    35. }
    36.  
    37.  
    38. void MainWindow::selectSprache()
    39. {
    40. QString table = ui.tabelle_le->text();
    41. model->setTable(table);
    42. model->setEditStrategy(QSqlTableModel::OnManualSubmit);
    43. model->select();
    44.  
    45.  
    46. ui.tableView->setModel(model);
    47. ui.tableView->show();
    48.  
    49. }
    50.  
    51.  
    52. void MainWindow::openLoginDialog()
    53. {
    54. LoginDialog l(this);
    55. l.exec();
    56.  
    57. }
    58. void MainWindow::init()
    59. {
    60. ui.speichern_btn->setEnabled(FALSE);
    61. }
    62. void MainWindow::updateTable()
    63. {
    64. model.setTable("sprache_tbl");
    65. QString name = model.record(1).value("sprache").toString();
    66. QMessageBox::information(this, "Application name",
    67. name);
    68. }
    69.  
    70. void MainWindow::enableButton()
    71. {
    72. QMessageBox::information(this, "Application name",
    73. "");
    74. //ui.speichern_btn->setEnabled(TRUE);
    75. }
    To copy to clipboard, switch view to plain text mode 
    Think DigitalGasoline

  6. #6
    Join Date
    Jan 2006
    Location
    Sofia, Bulgaria
    Posts
    24
    Thanks
    1
    Qt products
    Qt3
    Platforms
    Unix/X11

    Default Re: use button from another Window

    1. Subclass QApplication
    2. Add login() and cancel() signals to the login widget.
    3. Connect the login() and cancel() signals of your login widget to the QApplication.
    4. On the slot that handles login() and cancel() widgets, delete the login widget and setMainWidget to an instance your main window.
    5. Call again exec() to the subclassed QApplication (internally, from the login slot handler).
    6. Have fun

  7. #7
    Join Date
    Jan 2006
    Location
    Mountain View, CA
    Posts
    279
    Thanked 42 Times in 37 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: use button from another Window

    Quote Originally Posted by raphaelf
    Hello everybody!

    QT: 4.1.1

    I have 2 Windows: Mainwindow and Login.
    I would like to enable a button in the Mainwindow if the login was ok.
    My sitiation:
    1. start program (MainWindow)
    2. open Login (Modal)
    3. click on OK, if connection was ok, i need to enable a button in the MainWindow..

    I tryied this:

    login.cpp
    Qt Code:
    1. MainWindow* m;
    2. m->ui.speichern_btn->setEnabled(TRUE);
    To copy to clipboard, switch view to plain text mode 
    Why would you expect this to work? You didn't assign 'm' to anything yet, so it points to garbage - you can't call any methods on it. What are you trying to do here? Do you already have a MainWindow created somewhere, or are you trying to create one at this point?
    Save yourself some pain. Learn C++ before learning Qt.

  8. #8
    Join Date
    Jan 2006
    Posts
    273
    Thanks
    42
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default Re: use button from another Window

    Hi everybody,
    I tryed the interesting example from meissner and it happens nothing if i click on my ok_btn from login.
    It should call a Function of my MainWindow and show a Message. But no message apears
    If somebody have interest to help me, please have a look in my zip file

    The second problem i have is: I would like to show login by clicking on connect from main menu (It works allready, but i have implement the example from meissner)
    Attached Files Attached Files
    Think DigitalGasoline

  9. #9
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: use button from another Window

    A simple way to achieve (more or less) what you want is:

    Qt Code:
    1. int main(int argc, char **argv){
    2. QApplication app(argc, argv);
    3. LoginDialog logindlg;
    4. if(!logindlg.exec()){
    5. // login failed
    6. QMessageBox::critical(0, "Error", "Login failed");
    7. return 2;
    8. }
    9. MyMainWindow mw;
    10. mw.show();
    11. return app.exec();
    12. }
    To copy to clipboard, switch view to plain text mode 

    All you have to do now is to make sure your login dialog is accepted upoin successfull login and rejected otherwise. Of course you can put that code somewhere else and do different things when dialog is rejected.

  10. #10
    Join Date
    Jan 2006
    Posts
    273
    Thanks
    42
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default Re: use button from another Window

    Hi Wysota, thanks for your example..
    I think i have to explain again what i really want. Its sometime not easy to bring this in text

    Ok..
    I have 2 Dialogs..In my case a LoginDialog and MainWindow.
    I open my LoginDialog (Modal). I have there a button "ok_btn".
    I want to click this button and call a public slot from MainWindow.

    Sorry if i have not explain correctly..
    I hope i could explain better now
    Think DigitalGasoline

  11. #11
    Join Date
    Feb 2006
    Location
    Berlin
    Posts
    4
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: use button from another Window

    change line 26 in login.cpp from
    Qt Code:
    1. connect(ui.ok_btn, SIGNAL(clicked()), this, SLOT(LoginOk()));
    To copy to clipboard, switch view to plain text mode 
    into
    Qt Code:
    1. connect(ui.ok_btn, SIGNAL(clicked()), this, SIGNAL(LoginOk()));
    To copy to clipboard, switch view to plain text mode 

    This works for me.

  12. #12
    Join Date
    Jan 2006
    Posts
    273
    Thanks
    42
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default Re: use button from another Window

    hi!
    Thank you very much it works

    Thanks all
    Think DigitalGasoline

Similar Threads

  1. how to disable X button to close th window
    By raghvendramisra in forum Qt Programming
    Replies: 2
    Last Post: 5th February 2009, 20:01
  2. How to disable Maximise button in Output window
    By durgarao in forum Qt Tools
    Replies: 1
    Last Post: 2nd January 2009, 12:55
  3. Window title bar and "X" button
    By markcole in forum Qt Programming
    Replies: 3
    Last Post: 20th December 2007, 20:58
  4. Maximize Window button of Manin Window
    By sabeesh in forum Qt Programming
    Replies: 1
    Last Post: 1st August 2007, 08:20
  5. Window without a exit button
    By jbpvr in forum Qt Programming
    Replies: 4
    Last Post: 12th March 2007, 23:54

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.