Controls visibility.
-
Good afternoon, I have a simple question, I have a MainWindow where I have a file menu within a sub menu open when you click on this one unfold QDialog, where I have a button that just closes the dialog.
my question is how to disable the submenu to open the MainWindow, from QDialog, after closing it.I hope you can help me, any suggestions would appreciate it ...
Greetings. -
Could you please show how do you implement open and close of QDialog.
And where do you need to disable a sub-menu? In MainWindow or in QDialog.
-
First thing comes to mind is signal and slot mechanism, but i didn't understand your goals too. If you explain more we can give more useful advices.
-
I found this on the net, and I have managed to disable the menu of the mainwindow, from QDialog.
in the click event of the button of QDialog:
@static_cast<MainWindow*> (parent ()) -> activarMenu ();
accept (); @activarMenu (), is a public method that is in the mainwindows, all alone I send call from QDialog. my question is whether it is right to do that.
this is the code mainwindow.h
@#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include "frmlogin.h"
namespace Ui {
class MainWindow;
}class MainWindow : public QMainWindow
{
Q_OBJECTpublic:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();private slots:
void on_actionIniciar_sesion_triggered();
private:
Ui::MainWindow *ui;
frmLogin *login;
public:
void activarMenu();
};#endif // MAINWINDOW_H@
this is the code mainwindow.cpp:
@#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow){
ui->setupUi(this);
}MainWindow::~MainWindow(){
delete ui;
}void MainWindow::on_actionIniciar_sesion_triggered(){
login=new frmLogin(this);
login->setFixedSize(341,106);
login->exec();
}
void MainWindow::activarMenu(){
ui->actionIniciar_sesion->setEnabled(false);}@
this is the code frmlogin.h:
@#ifndef FRMLOGIN_H
#define FRMLOGIN_H
#include <QDialog>
namespace Ui {
class frmLogin;
}
class frmLogin : public QDialog{
Q_OBJECTpublic:
explicit frmLogin(QWidget *parent = 0);
~frmLogin();protected slots:
void on_pushButton_clicked();
};#endif // FRMLOGIN_H@
this the code frmlogin.cpp
@#include "frmlogin.h"
#include "ui_frmlogin.h"
#include <QMessageBox>
#include "mainwindow.h"
frmLogin::frmLogin(QWidget *parent) : QDialog(parent),ui(new Ui::frmLogin){
ui->setupUi(this);
ui->Password->setEchoMode(QLineEdit::Password);
}frmLogin::~frmLogin(){
delete ui;
}
void frmLogin::on_pushButton_clicked(){
if(ui->Usuario->text()=="lincoln"&&ui->Password->text()=="miclave"){
static_cast<MainWindow*>(parent())->activarMenu();
accept();
}
}
@ -
Hi,
That's completely unclean. static_cast doesn't make any check so you may be casting to the wrong class.
And since you are calling that login dialog from MainWindow you should just check whether the return value of exec is accepted or rejected and enable/disable the menu based on that. frmDialog doesn't need to know anything about the MainWindow, it's not it's job.
-
then that would make the job so, what is the best way?
-
It's there:
[quote author="SGaist" date="1400572157"]
And since you are calling that login dialog from MainWindow you should just check whether the return value of exec is accepted or rejected and enable/disable the menu based on that. [/quote]