PDA

View Full Version : Undo the this->setupId(this)



valerianst
1st July 2013, 11:30
Hi there,

I'm implementing a login/logout mechanism and I have a really strange problem.

I have created a main window with a constructor of this type:

MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui_(new Ui::MainWindow)
{
// call a login dialog

// the login emits a signal with the login status
// if ok start the application and create all the interface (calling a function that does the ui_->setupUi(this); )
}


when the user wants to log out he goes on the logout button.
A function that cleans everything (for the menu I use menubar()->clean() ) is called and then the login dialog is created again.
If the user now logs, the same function that does the ui_->setupUi(this); is called but the menu actions now are not connected

If instead of using the designer I create the menu and the actions manually, everything works.


I have discovered that the problem is due to a "double call" to ui_->setupUi(this);

I have also tried to put a flag and try to not to call it if the user logs in the second time, but if I don't call that function the interface will not be showed.

I would need a function to disconnect all the actions of the menu (like a ui_->undoSetupUi(this) )

Any hints?

Cheers!

Santosh Reddy
1st July 2013, 12:45
A simple and quick solution will be to delete MainWindow, and create a new fresh copy again.

valerianst
1st July 2013, 14:26
Thank you for your suggestion.

Yes, I have already thought about it but I would like to solve the real problem since it may happen again in the future...

Santosh Reddy
1st July 2013, 16:43
I have discovered that the problem is due to a "double call" to ui_->setupUi(this);
Calling setupUi() second time should not cause any problem.


I would need a function to disconnect all the actions of the menu (like a ui_->undoSetupUi(this) )
To remove all the action from menu bar use
1. QMenuBar::clear();
or
2. QMainWindow::setMenuBar(new QMenuBar());

valerianst
1st July 2013, 16:53
Calling setupUi() second time should not cause any problem.


To remove all the action from menu bar use
1. QMenuBar::clear();
or
2. QMainWindow::setMenuBar(new QMenuBar());



Thank you again for your suggestions.

In my case the double setupUI causes that problem. I have already tried both clear() and the QMainWindow::setMenuBar(new QMenuBar()); but it doesn't work anyway.


Maybe the problem is something else, like the setupUI that doesn't update the UI with the correct actions..

anda_skoa
1st July 2013, 18:27
Why not just hide the main window while the login dialog is shown?
Or disable the main window while the dialog is up?

Cheers,
_

valerianst
1st July 2013, 18:32
Yes, in theory I could do that, but I think it is not the right way to solve this problem (since it might happen in the future).

Santosh Reddy
1st July 2013, 21:51
Are there any warnings in the Application output when use logs in the second time?

I suspect the problem is with connectSlotsByName().

Try inhering the Ui form class instead of member variable.

ChrisW67
1st July 2013, 23:13
I am with andy_skoa. Simple solution for a simple problem rather than trying to make a complex and awkward solution work.

Another option would be to make the QMainWindow central widget a QStackedWidget with the login UI on one page and the logged in UI on the other... Then just flip between them.

valerianst
2nd July 2013, 09:24
Nope, it seems everything ok.

Do you mean there is a bug in that Qt function?


I've tried to inherit but I got some errors, I'll try to solve them and I'll let you know.


I am with andy_skoa. Simple solution for a simple problem rather than trying to make a complex and awkward solution work.

Another option would be to make the QMainWindow central widget a QStackedWidget with the login UI on one page and the logged in UI on the other... Then just flip between them.

Hi, yes, maybe you're right but now, I am just interested to know why it doesn't work more than how to do it in another way.

Thank you anyway, any point of view is welcomed :)

anda_skoa
2nd July 2013, 15:07
I am with andy_skoa. Simple solution for a simple problem rather than trying to make a complex and awkward solution work.


Indeed. Recreating widget content is almost never a good solution. The only use case where this is needed is when element existence depends on runtime value, e.g. generating n lines of an input form for n entries.

Content specified by UI file is usually static and only created once.

Cheers,
_

valerianst
2nd July 2013, 15:30
Ok, I agree. Maybe mine is not the better solution.

In fact, maybe I'll change the design anyway.... but I still would like to know why it doesn't work :)