PDA

View Full Version : Connect Window with MainWindow different Files.



Mauricio
9th July 2014, 22:47
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


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

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...

d_stranz
10th July 2014, 03:08
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.

Mauricio
10th July 2014, 05:30
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


public:
void login();

signals:
userValidated();




then, in file cpp is this:

connectDialog.cpp


void connectDialog::login()
{
if (database.Users() == 0){
emit userValidated();
this.close();
}
else
ui->labelUserError->setVisible(true);
}


Now in my file MainWindow.h

MainWindow.h


public slots:
void showPrivileges();


and the cpp file is this:

MainWindow.cpp


void MainWindow::showPrivileges()
{
ui->label0->setVisible(true);
ui->label1->setEnabled(true);

}


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



MainWindow::MainWindow(QWidget *parent) :
QMainWindow(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..

Lesiok
10th July 2014, 07:31
connect uses pointer to the object not class name (connectDialog).

yeye_olive
10th July 2014, 10:31
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

signals:
void userValidated();

Mauricio
10th July 2014, 17:19
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/0B0gGbpM4URt6Tk44NmNjbllSSG8/edit?usp=sharing


thanks..

yeye_olive
10th July 2014, 17:47
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?

Lesiok
10th July 2014, 19:38
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.

Mauricio
10th July 2014, 21:05
Ok, gracias.

d_stranz
11th July 2014, 19:39
Ok, gracias.

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



void MainWindow::showLoginDialog()
{
connectDialog dlg;
dlg.exec();
}


If so, you need to change that to:



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.

Mauricio
13th July 2014, 21:59
:) Thanks d_stranz,, muchas gracias.. it's work..!