Results 1 to 11 of 11

Thread: Connect Window with MainWindow different Files.

  1. #1
    Join Date
    Jun 2014
    Posts
    8
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Connect Window with MainWindow different Files.

    Hello.


    I wanna make a connection between two functions in different files with differents Dialogs (QDialog and QMainWindow).

    File # 1.

    The function database.Users() establish a conection with a mysql server, and if the user is Ok, then return 0.
    The next step is call the function showPrivileges() on MainWindow.cpp file.

    connectDialog.cpp
    Qt Code:
    1. void connectDialog::login()
    2. {
    3. if (database.Users() == 0){
    4. << here.. >>
    5. this.close();
    6. }
    7. else
    8. ui->labelUserError->setVisible(true);
    9. }
    To copy to clipboard, switch view to plain text mode 


    File # 2
    See the showPrivileges() function in MainWindow, this make two labels visible in MainWindow form.

    MainWindow.cpp
    Qt Code:
    1. void MainWindow::showPrivileges()
    2. {
    3. ui->label0->setVisible(true);
    4. ui->label1->setEnabled(true);
    5.  
    6. }
    To copy to clipboard, switch view to plain text mode 

    I wanna call the function void MainWindow::showPrivileges() in real time, execution time..
    From the if sentence in void connectDialog::login() function before of the line this.close();


    I don't know how do this. Please help me.


    Thanks...
    Last edited by Mauricio; 9th July 2014 at 21:52.

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

    Default Re: Connect Window with MainWindow different Files.

    This is what signals and slots are for.

    In the connectDialog class, define a signal. Call it "userValidated()" or whatever you want. Change your MainWindow::showPrivileges() function to be a slot.

    In your MainWindow code that displays the connectDialog, connect the dialog's userValidate() signal to the MainWindow's showPrivileges() slot.

    Replace "<< here.. >>" with "emit userValidated();" and you're done.

  3. #3
    Join Date
    Jun 2014
    Posts
    8
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Connect Window with MainWindow different Files.

    Hi d_stranz thank you for your time.


    I did this but not work. I have an Error.

    Like you said me, in my class connectDialog I add the line signals: at end.:

    connectDialog.h
    Qt Code:
    1. public:
    2. void login();
    3.  
    4. signals:
    5. userValidated();
    To copy to clipboard, switch view to plain text mode 


    then, in file cpp is this:

    connectDialog.cpp
    Qt Code:
    1. void connectDialog::login()
    2. {
    3. if (database.Users() == 0){
    4. emit userValidated();
    5. this.close();
    6. }
    7. else
    8. ui->labelUserError->setVisible(true);
    9. }
    To copy to clipboard, switch view to plain text mode 

    Now in my file MainWindow.h

    MainWindow.h
    Qt Code:
    1. public slots:
    2. void showPrivileges();
    To copy to clipboard, switch view to plain text mode 

    and the cpp file is this:

    MainWindow.cpp
    Qt Code:
    1. void MainWindow::showPrivileges()
    2. {
    3. ui->label0->setVisible(true);
    4. ui->label1->setEnabled(true);
    5.  
    6. }
    To copy to clipboard, switch view to plain text mode 

    I add the coonect line in the body of constructor of MainWindow:

    Qt Code:
    1. MainWindow::MainWindow(QWidget *parent) :
    2. QMainWindow(parent),
    3. ui(new Ui::MainWindow)
    4. {
    5. ui->setupUi(this);
    6.  
    7. connect(connectDialog,SIGNAL(userValidated()),this,SLOT(showPrivileges()));
    8. }
    To copy to clipboard, switch view to plain text mode 

    but whe I build i get an error.. I don't fix that.

    Thank you again..

  4. #4
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,536
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Connect Window with MainWindow different Files.

    connect uses pointer to the object not class name (connectDialog).

  5. #5
    Join Date
    Oct 2009
    Posts
    483
    Thanked 97 Times in 94 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Connect Window with MainWindow different Files.

    Quote Originally Posted by Mauricio View Post
    but whe I build i get an error.. I don't fix that.
    How informative. Care to tell us what error you get? Here is a guess: your signal declaration should read
    Qt Code:
    1. signals:
    2. void userValidated();
    To copy to clipboard, switch view to plain text mode 

  6. #6
    Join Date
    Jun 2014
    Posts
    8
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Connect Window with MainWindow different Files.

    Thank you.

    Hi Lesiok, I don't understand, How I can use a pointer in MainWindow.cpp file for this?
    please can you show me an example.


    Hi yeye_olive this is the error.

    https://drive.google.com/file/d/0B0g...it?usp=sharing


    thanks..

  7. #7
    Join Date
    Oct 2009
    Posts
    483
    Thanked 97 Times in 94 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Connect Window with MainWindow different Files.

    Great, so you send a screenshot with error messages at lines 23 in connectDialog.h and 29 in mainWindow.cpp. What are we supposed to do? You alone know what is to be found at those lines since you forgot to show us the complete code. I am tired of this guessing game. If you want help, provide the information up front. By the way, don't you think my previous post could help you fix the error in connectDialog.h?

  8. #8
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,536
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Connect Window with MainWindow different Files.

    You first need to create an object then you can connect the signals. I do not know the architecture of your application so I can not say anything more.

  9. #9
    Join Date
    Jun 2014
    Posts
    8
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Connect Window with MainWindow different Files.

    Ok, gracias.

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

    Default Re: Connect Window with MainWindow different Files.

    Ok, gracias.
    Somewhere in your MainWindow code you do something like this, right?

    Qt Code:
    1. void MainWindow::showLoginDialog()
    2. {
    3. connectDialog dlg;
    4. dlg.exec();
    5. }
    To copy to clipboard, switch view to plain text mode 

    If so, you need to change that to:

    Qt Code:
    1. void MainWindow::showLoginDialog()
    2. {
    3. connectDialog dlg;
    4. connect( &dlg, SIGNAL( userValidated() ), this, SLOT( showPrivileges() ) );
    5. dlg.exec();
    6. }
    To copy to clipboard, switch view to plain text mode 

    That's what the others mean when they say you have to make the connection using an instance of your connectDialog class.

  11. #11
    Join Date
    Jun 2014
    Posts
    8
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Connect Window with MainWindow different Files.

    Thanks d_stranz,, muchas gracias.. it's work..!

Similar Threads

  1. Replies: 11
    Last Post: 6th October 2013, 19:40
  2. How to connect functions of QML files?
    By Yonetici in forum Qt Quick
    Replies: 2
    Last Post: 20th July 2012, 19:47
  3. connect mainwindow with self-made widgets
    By pinkiP in forum Qt Programming
    Replies: 11
    Last Post: 10th July 2012, 10:16
  4. Replies: 0
    Last Post: 9th May 2010, 20:49
  5. Replies: 1
    Last Post: 11th September 2007, 13:34

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.