PDA

View Full Version : How to change the menubar after log in as super user.



adutzu89
20th January 2014, 13:45
Hello, I am writing an app which after the main window shows it has a menubar with the option to connect, with a "super" user which has some adittional menus/actions.
The thing is that I have different classes for different menubars and another class for creating the dialog.
The issue is that I cannot figure it out how to actually change the menubar.
Here is the main window header file:

#ifndef CAMER_H
#define CAMER_H

#include <QMainWindow>
#include <QSettings>
#include <QDialog>
#include <QDialogButtonBox>
#include <QLabel>
#include <QLineEdit>
#include <QGridLayout>
#include <QPushButton>
namespace Ui {
class camer;
}

class camer : public QMainWindow
{
Q_OBJECT

public:
explicit camer(QWidget *parent = 0);
~camer();
void setMeniuAdmin();
void setMeniuNeaut();
private:
Ui::camer *ui;
private slots:
void creareDialogConect();
public slots:
};

#endif // CAMER_H


here is the main window cpp file:

#include "camer.h"
#include "conn.h"
#include "FerDialConect.h"
#include "meniuNeautentificat.h"
#include "meniuAdmin.h"
camer::camer(QWidget *parent):QMainWindow(parent){
setMinimumSize(800,600);
setMeniuNeaut();
}

camer::~camer()
{
//settings saving function goes here
}


void camer::creareDialogConect(){
FerDialConect *fdc=new FerDialConect(this);
fdc->afisDial();
}

void camer::setMeniuNeaut(){
menNeaut *mn=new menNeaut(this);
setMenuBar(mn);
}

void camer::setMeniuAdmin(){
MeniuAdmin *mad=new MeniuAdmin(this);
setMenuBar(mad);
}


here are the dialog header and cpp files:

#ifndef FERDIALCONECT_H
#define FERDIALCONECT_H
#include <QDialog>
#include <QGridLayout>
#include <QLabel>
#include <QLineEdit>
#include <QDialogButtonBox>
#include <QMessageBox>
#include <QPushButton>
#include "camer.h"
class FerDialConect:public camer{
Q_OBJECT
public:
explicit FerDialConect(camer *parent = 0);
void afisDial();
QString util,parola;
private:
QGridLayout *amplasareDialCon,*amplasareDialCon1,*amplasareDia lCon2;
QLabel *etUtil,*etPar;
QLineEdit *txtUtil,*txtPar;
QPushButton *bCon,*bAnul;
QDialogButtonBox *cutieButCon;
QDialog *dialogConect;
private slots:
void con();
};

#endif // FERDIALCONECT_H



#include "FerDialConect.h"
#include "meniuAdmin.h"

FerDialConect::FerDialConect(camer *parent):camer(parent){
dialogConect=new QDialog(this);
amplasareDialCon=new QGridLayout;
amplasareDialCon1=new QGridLayout;
amplasareDialCon2=new QGridLayout;
etUtil=new QLabel(tr("Utilizator"));
etPar=new QLabel(tr("Parola"));
txtUtil=new QLineEdit(util);
txtPar=new QLineEdit(parola);
txtPar->setEchoMode(QLineEdit::Password);
cutieButCon=new QDialogButtonBox;
cutieButCon->setOrientation(Qt::Horizontal);
bCon=new QPushButton(tr("Conectare"));
bAnul=new QPushButton(tr("Anulare"));
cutieButCon->addButton(bCon,QDialogButtonBox::AcceptRole);
cutieButCon->addButton(bAnul,QDialogButtonBox::RejectRole);
connect(cutieButCon,SIGNAL(rejected()),dialogConec t,SLOT(reject()));
connect(cutieButCon,SIGNAL(accepted()),this,SLOT(c on()));
amplasareDialCon->addLayout(amplasareDialCon1,0,0);
amplasareDialCon->addLayout(amplasareDialCon2,1,0);
amplasareDialCon1->addWidget(etUtil,0,0);
amplasareDialCon1->addWidget(txtUtil,0,1);
amplasareDialCon1->addWidget(etPar,1,0);
amplasareDialCon1->addWidget(txtPar,1,1);
amplasareDialCon2->addWidget(cutieButCon,0,0);
}

void FerDialConect::afisDial(){
dialogConect->setLayout(amplasareDialCon);
dialogConect->setFixedSize(300,200);
dialogConect->exec();
}

void FerDialConect::con(){
util=txtUtil->text();
int marimeUtil=util.size();
parola=txtPar->text();
int marimePar=parola.size();
if(marimeUtil==0 && marimePar!=0){
QMessageBox::critical(this, tr("Eroare"), tr("Campul de utilizator este necompletat."));
return;
}
else if(marimeUtil!=0 && marimePar==0){
QMessageBox::critical(this, tr("Eroare"), tr("Campul de parola este necompletat."));
return;
}
else if(marimeUtil==0 && marimePar==0){
QMessageBox::critical(this, tr("Eroare"), tr("Campurile sunt necompletate."));
return;
}
else {
if(util=="admin" && parola=="camer"){
dialogConect->accept();
camer::setMeniuAdmin();
}

}
}


here is the default menubar files:

#ifndef MENIUNEAUTENTIFICAT_H
#define MENIUNEAUTENTIFICAT_H
#include <QMenuBar>
#include <QMenu>
#include <QAction>
#include "camer.h"

class menNeaut:public QMenuBar{
Q_OBJECT
public:
explicit menNeaut(camer *parent=0);
private:
QMenu *meniuStandard;
QAction *conectare,*inchideApp;
private slots:
};

#endif // MENIUNEAUTENTIFICAT_H



#include "meniuNeautentificat.h"
menNeaut::menNeaut(camer *parent):QMenuBar(parent){
meniuStandard=new QMenu("Meniu");
conectare=new QAction(tr("Conectare"),this);
connect(conectare,SIGNAL(triggered()),parent,SLOT( creareDialogConect()));
inchideApp=new QAction(tr("Inchide"),this);
connect(inchideApp,SIGNAL(triggered()),parent,SLOT (inchideApplicatie()));
meniuStandard->addAction(conectare);
meniuStandard->addAction(inchideApp);
addMenu(meniuStandard);
}


here are the files for the second menu(its just a basic setup):

#ifndef MENIUADMIN_H
#define MENIUADMIN_H
#include <QMenuBar>
#include "camer.h"
#include <QMenu>
#include <QAction>

class MeniuAdmin:public QMenuBar{
Q_OBJECT
public:
explicit MeniuAdmin(camer *parent=0);
private:
QMenu *meniu;
QAction *dateP;
private slots:
};

#endif // MENIUADMIN_H



#include "meniuAdmin.h"
MeniuAdmin::MeniuAdmin(camer *parent):QMenuBar(parent){
meniu=new QMenu(tr("Meniu"),this);
dateP=new QAction(tr("Date Personale"),this);
meniu->addAction(dateP);
addMenu(meniu);
}

anda_skoa
20th January 2014, 13:53
I have no idea what your actual problem is, but you have same very strange code in FerDialConect::con()



if(util=="admin" && parola=="camer"){
dialogConect->accept();
camer c;
c.setMeniuAdmin();
}

This creates a new "camer" main window, tells it to show the admin menu and then destroys it (camer goes out of scope).
It also calls accept() on another dialog.

Cheers,
_

adutzu89
20th January 2014, 14:02
I am trying to use
camer::setMeniuAdmin() ,
dialogConect->accept() is called because I created it inside FerDialConect class(because I made it a child of camer class).

I've changed my code from

camer c;
c.setMeniuAdmin();
to

camer::setMeniuAdmin()

but still no response as I get cannot call void camer::setMeniuAdmin without object.

anda_skoa
20th January 2014, 16:43
dialogConect->accept() is called because I created it inside FerDialConect class(because I made it a child of camer class).

I see. Usually a dialog is implemented inside its own class and simply use from elsewhere.



I've changed my code from

camer c;
c.setMeniuAdmin();
to

camer::setMeniuAdmin()

but still no response as I get cannot call void camer::setMeniuAdmin without object.

This is strange, the class FerDialConect is derived from camer so that should work. Try just the method


setMeniuAdmin();


Cheers,
_

adutzu89
20th January 2014, 20:59
Thank you for taking the time to help, it does not work with no error whatsoever, not even at runtime.
I've tried even connecting signals of
accept() and
reject() buttons from dialog to
setMeniuAdmin()(after moving the function to public slots).
The function works as I made a central widget,added 2 butons to switch between the menubars but that was only in camer class.
It must be my parent-child relationship, but I cannot figure it out.

anda_skoa
20th January 2014, 21:49
You could make a ZIP file of your project and attach it.

Cheers,
_

adutzu89
21st January 2014, 09:15
Ok I will uploaded it. Thank you.
PS: There are some elements that I added to test functionality in camer.cpp. And didn't had time to clean the project before uploading it, have a exam that I need to go, also the I am using Qt 5.2.

anda_skoa
21st January 2014, 12:42
First check if you really want FerDialConect to be of type "camer", i.e. a main window.
The slot that creates it is called creareDialogConect which would indicate that FerDialConect is a dialog.

If you want to change the menu of the camer instance that is created in main(), then you have to call its setMeniuAdmin().

For example after fdc->afisDial() returns or, after making setMeniuAdmin() a slot, have FerDialConect emit a signal and connect it to the setMeniuAdmin().

Cheers,
_

adutzu89
22nd January 2014, 10:49
First check if you really want FerDialConect to be of type "camer", i.e. a main window.
The slot that creates it is called creareDialogConect which would indicate that FerDialConect is a dialog.

If you want to change the menu of the camer instance that is created in main(), then you have to call its setMeniuAdmin().

For example after fdc->afisDial() returns or, after making setMeniuAdmin() a slot, have FerDialConect emit a signal and connect it to the setMeniuAdmin().

Cheers,
_

Changed the FerDialConect from type "camer" to dialog(as it was originally, I've just tried multiple "solutions" that came to mind).

I've made setMeniuAdmin() a slot, made a signal that would emit after the condition was met and conected them using

camer c;
connect(this,SIGNAL(semnalSuper()),&c,SLOT(setMeniuAdmin()));

where semnalSuper is the signal created inside FerDialconect class.
But again nothing.
Tell me am I connecting it wrong?
I've tried also connecting using the "newer" connecting method:

connect(this,&FerDialConect::semnalSuper,&c,&camer::setMeniuAdmin);

The signal emits, as I tried creating a test slot inside FerDialConect which showed a QMessageBox when the signal is emited and works.

I will try remaking this test with QMessageBox with slot beeing in camer class, to see if it works.

Added after 44 minutes:

Finally solved it by connecting from creareDialogConect() and not from FerDialCon constructor how I usually connected, after creating the dialog.

Thank you for all your help.

anda_skoa
22nd January 2014, 12:30
Your other attempt didn't work because you were connecting to a local instance of camer. It went out of scope as soon as your dialog's constructor ended.

Cheers,
_