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
Code:
MainWindow* m;
m->ui.speichern_btn->setEnabled(TRUE);
login.h
Code:
public:
MainWindow* m;
Have somebody a idea
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.
Re: use button from another Window
Hi, i get no errors from compiler, but if i call my function the program crash..
login.cpp
Code:
MainWindow* m;
m->ui.speichern_btn->setEnabled(TRUE);
What could i try?
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:
Code:
signals:
void LoginOk();
In the login.cpp you must write when the SIGNAL are emitted, maybe like this:
Code:
connect(loginButton, SIGNAL(clicked()), this, SIGNAL(LoginOk()));
Now, MainWindow can recieve this SIGNAL and you can call a SLOT from MainWindow:
Code:
//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.
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:
Code:
#include "ui_login.h"
{
Q_OBJECT
public:
public slots:
void myfirstfunction();
bool verbinden();
private:
Ui::LoginDialog ui;
void init();
signals:
void LoginOk();
};
login.cpp:
Code:
#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
) {
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()
{
"Unable to find the user preferences file.\n"
"The factory default will be used instead.");
}
bool LoginDialog::verbinden()
{
QString host
= ui.
host_cb->currentText
();
if((user == ""))
{
QMessageBox::information(this,
"Login",
"Bitte Benutzername angeben");
return 0;
}
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:
Code:
#include "ui_mainwindow.h"
{
Q_OBJECT
public:
MainWindow();
Ui::MainWindow ui;
public slots:
void selectSprache();
void openLoginDialog();
void updateTable();
void enableButton();
private:
void init();
protected:
};
test.cpp:
Code:
#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
();
model->setTable(table);
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()
{
model.setTable("sprache_tbl");
QString name
= model.
record(1).
value("sprache").
toString();
name);
}
void MainWindow::enableButton()
{
"");
//ui.speichern_btn->setEnabled(TRUE);
}
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
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
Code:
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?
1 Attachment(s)
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)
Re: use button from another Window
A simple way to achieve (more or less) what you want is:
Code:
int main(int argc, char **argv){
LoginDialog logindlg;
if(!logindlg.exec()){
// 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.
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 :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 :)
Re: use button from another Window
change line 26 in login.cpp from
Code:
connect(ui.ok_btn, SIGNAL(clicked()), this, SLOT(LoginOk()));
into
Code:
connect(ui.ok_btn, SIGNAL(clicked()), this, SIGNAL(LoginOk()));
This works for me.
Re: use button from another Window
hi!
Thank you very much it works :)
Thanks all ;)