PDA

View Full Version : use button from another Window



raphaelf
28th February 2006, 20:40
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


MainWindow* m;
m->ui.speichern_btn->setEnabled(TRUE);


login.h


public:
LoginDialog(QWidget *parent = 0);
MainWindow* m;


Have somebody a idea

e8johan
28th February 2006, 21:26
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.

raphaelf
28th February 2006, 21:42
Hi, i get no errors from compiler, but if i call my function the program crash..

login.cpp


MainWindow* m;
m->ui.speichern_btn->setEnabled(TRUE);


What could i try?

meissner
1st March 2006, 03:27
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:

signals:
void LoginOk();

In the login.cpp you must write when the SIGNAL are emitted, maybe like this:

connect(loginButton, SIGNAL(clicked()), this, SIGNAL(LoginOk()));

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


//creating an instance of LoginDialog, maybe in the ctr of MainWindow
LoginDialog *loginDialog = new LoginDialog();
loginDialog->show();
connect(loginDialog, SIGNAL(LoginOk()), this, SLOT(anySlotYouveCreated()));

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

Hopefully I understand you right, first time I can help/answer.

raphaelf
1st March 2006, 10:24
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:


#include "ui_login.h"




class LoginDialog : public QDialog
{
Q_OBJECT

public:
LoginDialog(QWidget *parent = 0);


public slots:
void myfirstfunction();
bool verbinden();


private:
Ui::LoginDialog ui;
void init();

signals:
void LoginOk();






};


login.cpp:


#include "login.h"
#include "test.h"



#include <QMessageBox>
#include <QSqlDatabase>
#include <QSqlQuery>
#include <QSqlError>
#include <QTextEdit>
#include <QStatusBar>
#include <QAbstractItemView>
#include <QEvent>
#include <QTreeWidgetItem>
#include <QTreeWidget>
#include <QtGui/QTreeWidget>
#include <QSqlQueryModel>
#include <QSqlTableModel>

LoginDialog::LoginDialog(QWidget *parent)
: QDialog(parent)
{
ui.setupUi(this);


connect(ui.ok_btn, SIGNAL(clicked()), this, SLOT(LoginOk()));
connect(ui.ok_btn, SIGNAL(clicked()), this, SLOT(verbinden()));
connect(ui.cancel_btn, SIGNAL(clicked()), this, SLOT(close()));
}

void LoginDialog::myfirstfunction()
{
QMessageBox::information(this, "Application name",
"Unable to find the user preferences file.\n"
"The factory default will be used instead.");

}
bool LoginDialog::verbinden()
{

QString user = ui.user_le->text();
QString pass = ui.pass_le->text();
QString host = ui.host_cb->currentText();
QMessageBox::information(this,"Login", user);
QMessageBox::information(this,"Login", pass);
QMessageBox::information(this,"Login", host);
if((user == ""))
{
QMessageBox::information(this,"Login", "Bitte Benutzername angeben");
return 0;
}

QSqlDatabase db = QSqlDatabase::addDatabase("QODBC");
db.setHostName(host);
db.setDatabaseName("DRIVER={SQL Server};SERVER="+host+";DATABASE=inventar;UID="+user+";PWD="+pass+"");
db.setUserName(user);
db.setPassword(pass);
if(!db.open())
{
QMessageBox::information(this,"",db.lastError().text());
return false;
}
else
{
//MainWindow* m;
//m->ui.speichern_btn->setEnabled(TRUE);


this->close();
return true;
}

}


test.h:


#include "ui_mainwindow.h"



class MainWindow : public QMainWindow
{
Q_OBJECT

public:
MainWindow();
Ui::MainWindow ui;



public slots:
void selectSprache();
void openLoginDialog();
void updateTable();
void enableButton();



private:

void init();

protected:




};


test.cpp:


#include "test.h"
#include "login.h"

#include <QMessageBox>
#include <QSqlDatabase>
#include <QSqlQuery>
#include <QSqlError>
#include <QTextEdit>
#include <QStatusBar>
#include <QAbstractItemView>
#include <QEvent>
#include <QTreeWidgetItem>
#include <QTreeWidget>
#include <QtGui/QTreeWidget>
#include <QSqlQueryModel>
#include <QSqlTableModel>
#include <QSqlRecord>




MainWindow::MainWindow()

{
ui.setupUi(this);

LoginDialog* logindialog = new LoginDialog();
logindialog->show();
connect(logindialog, SIGNAL(LoginOk()), this, SLOT(enableButton()));
connect(ui.actionverbinden, SIGNAL(triggered()), this, SLOT(openLoginDialog()));
connect(ui.speichern_btn, SIGNAL(clicked()), this, SLOT(selectSprache()));
connect(ui.update_btn, SIGNAL(clicked()), this, SLOT(updateTable()));
init();

}


void MainWindow::selectSprache()
{
QString table = ui.tabelle_le->text();
QSqlTableModel *model = new QSqlTableModel;
model->setTable(table);
model->setEditStrategy(QSqlTableModel::OnManualSubmit);
model->select();


ui.tableView->setModel(model);
ui.tableView->show();

}


void MainWindow::openLoginDialog()
{
LoginDialog l(this);
l.exec();

}
void MainWindow::init()
{
ui.speichern_btn->setEnabled(FALSE);
}
void MainWindow::updateTable()
{
QSqlTableModel model;
model.setTable("sprache_tbl");
QString name = model.record(1).value("sprache").toString();
QMessageBox::information(this, "Application name",
name);
}

void MainWindow::enableButton()
{
QMessageBox::information(this, "Application name",
"");
//ui.speichern_btn->setEnabled(TRUE);
}

gadnio
1st March 2006, 10:38
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

Chicken Blood Machine
1st March 2006, 20:00
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


MainWindow* m;
m->ui.speichern_btn->setEnabled(TRUE);


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?

raphaelf
2nd March 2006, 16:14
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)

wysota
2nd March 2006, 16:59
A simple way to achieve (more or less) what you want is:


int main(int argc, char **argv){
QApplication app(argc, argv);
LoginDialog logindlg;
if(!logindlg.exec()){
// login failed
QMessageBox::critical(0, "Error", "Login failed");
return 2;
}
MyMainWindow mw;
mw.show();
return app.exec();
}

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.

raphaelf
2nd March 2006, 18:46
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 :rolleyes:

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 :)

meissner
2nd March 2006, 19:16
change line 26 in login.cpp from

connect(ui.ok_btn, SIGNAL(clicked()), this, SLOT(LoginOk()));
into

connect(ui.ok_btn, SIGNAL(clicked()), this, SIGNAL(LoginOk()));

This works for me.

raphaelf
2nd March 2006, 20:31
hi!
Thank you very much it works :)

Thanks all ;)