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
Code:
void connectDialog::login()
{
if (database.Users() == 0){
<< here.. >>
this.close();
}
else
ui->labelUserError->setVisible(true);
}
File # 2
See the showPrivileges() function in MainWindow, this make two labels visible in MainWindow form.
MainWindow.cpp
Code:
void MainWindow::showPrivileges()
{
ui->label0->setVisible(true);
ui->label1->setEnabled(true);
}
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...
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.
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
Code:
public:
void login();
signals:
userValidated();
then, in file cpp is this:
connectDialog.cpp
Code:
void connectDialog::login()
{
if (database.Users() == 0){
emit userValidated();
this.close();
}
else
ui->labelUserError->setVisible(true);
}
Now in my file MainWindow.h
MainWindow.h
Code:
public slots:
void showPrivileges();
and the cpp file is this:
MainWindow.cpp
Code:
void MainWindow::showPrivileges()
{
ui->label0->setVisible(true);
ui->label1->setEnabled(true);
}
I add the coonect line in the body of constructor of MainWindow:
Code:
MainWindow
::MainWindow(QWidget *parent
) : ui(new Ui::MainWindow)
{
ui->setupUi(this);
connect(connectDialog,SIGNAL(userValidated()),this,SLOT(showPrivileges()));
}
but whe I build i get an error.. I don't fix that.
Thank you again..
Re: Connect Window with MainWindow different Files.
connect uses pointer to the object not class name (connectDialog).
Re: Connect Window with MainWindow different Files.
Quote:
Originally Posted by
Mauricio
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
Code:
signals:
void userValidated();
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..
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?
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.
Re: Connect Window with MainWindow different Files.
Re: Connect Window with MainWindow different Files.
Somewhere in your MainWindow code you do something like this, right?
Code:
void MainWindow::showLoginDialog()
{
connectDialog dlg;
dlg.exec();
}
If so, you need to change that to:
Code:
void MainWindow::showLoginDialog()
{
connectDialog dlg;
connect( &dlg, SIGNAL( userValidated() ), this, SLOT( showPrivileges() ) );
dlg.exec();
}
That's what the others mean when they say you have to make the connection using an instance of your connectDialog class.
Re: Connect Window with MainWindow different Files.
:) Thanks d_stranz,, muchas gracias.. it's work..!